Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
@loopback/cron
Advanced tools
Readme
This module contains a component to provide integration with
Cron so that applications can schedule
jobs using cron
based schedule.
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.
npm install --save @loopback/cron
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);
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();
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
});
Run npm test
from the root folder.
See all contributors.
MIT
FAQs
Schedule tasks using cron-like syntax
The npm package @loopback/cron receives a total of 1,510 weekly downloads. As such, @loopback/cron popularity was classified as popular.
We found that @loopback/cron demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 10 open source maintainers collaborating on the project.
Did you know?
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.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.