Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@loopback/cron

Package Overview
Dependencies
Maintainers
0
Versions
63
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@loopback/cron

Schedule tasks using cron-like syntax

  • 0.12.7
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2.1K
decreased by-14.98%
Maintainers
0
Weekly downloads
 
Created
Source

@loopback/cron

This module contains a component to provide integration with Cron so that applications can schedule jobs using cron based schedule.

Stability: ⚠️Experimental⚠️

Experimental packages provide early access to advanced or experimental functionality to get community feedback. Such modules are published to npm using 0.x.y versions. Their APIs and functionality may be subject to breaking changes in future releases.

Installation

npm install --save @loopback/cron

Basic use

Register the CronComponent

The component should be loaded in the constructor of your custom Application class.

Start by importing the component class:

import {CronComponent} from '@loopback/cron';

In the constructor, add the component to your application:

this.component(CronComponent);

Register cron jobs

We can create an instance of CronJob and bind it to the application context so it can be managed by the CronComponent life cycles including start and stop.

import {CronJob, asCronJob} from '@loopback/cron';

// Create an cron job
const job = new CronJob({
  cronTime: '*/1 * * * * *', // Every one second
  onTick: () => {
    // Do the work
  },
  start: true, // Start the job immediately
});

// Bind the cron job as an extension for the scheduler
app.bind('cron.jobs.job1').to(job).apply(asCronJob);

It's also possible to extend CronJob.

import {CronJob, cronJob, CronJobConfig} from '@loopback/cron';
import {config, Provider, createBindingFromClass} from '@loopback/core';

@cronJob()
class MyCronJob extends CronJob {
  constructor() {
    super({
      name: 'job-B',
      onTick: () => {
        // do the work
      },
      cronTime: startIn(50),
      start: false,
    });
  }
}

const jobBinding = createBindingFromClass(MyCronJob);
app.add(jobBinding);

Alternatively, we can also define a provider class:

import {CronJob, cronJob, CronJobConfig} from '@loopback/cron';
import {config, Provider, createBindingFromClass} from '@loopback/core';

@cronJob()
class CronJobProvider implements Provider<CronJob> {
  constructor(@config() private jobConfig: CronJobConfig = {}) {}
  value() {
    const job = new CronJob({
      // ... default config
      ...this.jobConfig,
    });
    return job;
  }
}

const jobBinding = createBindingFromClass(CronJobProvider);
app.add(jobBinding);
const now = new Date();
now.setMilliseconds(now.getMilliseconds() + 10);
const jobConfig: CronJobConfig = {
  name: 'job-B', // Name the job
  cronTime: sendAt(now), // Start the job in 10 ms
  start: false,
};
app.configure(jobBinding.key).to(jobConfig);

Please refer to cron api for more details about options to create a job.

By default, all cron jobs will be started and stopped by CronComponent when the application is started or stopped.

To start a cron job after application starts, we can either set start to true for the job or use the following code to start non-running jobs:

import {CronBindings} from '@loopback/cron';

const component = await app.get(CronBindings.COMPONENT);
await component.start();

Error handling

The CronJob class from @loopback/cron is a subclass of the CronJob from cron module to provide better error handling and troubleshooting.

To catch errors thrown from the job's onTick or other callback methods:

job.onError(err => {
  // process the error
});

Contributions

Tests

Run npm test from the root folder.

Contributors

See all contributors.

License

MIT

Keywords

FAQs

Package last updated on 15 Oct 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc