Manage a group of cron workers. Based on node-cron.
$ npm i cron-group
options
- optionaltimezone
- pass it to CronJob constructor
name
- name of jobschedule
- schedule in cron formatworker
- worker function
Add cron job to group without starting.
name
- name of job to run
Run specified by name job.
Enable cron for all added jobs.
Disable cron for all added jobs, and wait until all jobs is complete.
CronGroup is subclass of EventEmitter, so it fires some events.
name
- name of jobresult
- result returned from worker functionrunAt
- when job is runcompletedAt
- whe job is complete
name
- name of joberr
- error object caught in worker function
const CronGroup = require('cron-group');
const group = new CronGroup({
timezone: 'Europe/Moscow'
});
group.add({
name: 'foo',
schedule: '* * * * * *',
worker: new Promise((resolve) => setTimeout(resolve, 3000))
});
group.add({
name: 'bar',
schedule: '30 * * * * *',
worker: new Promise((resolve) => setTimeout(resolve, 1000))
});
group.on('run', ({name, cause}) => {
console.log(`${name} is run by ${cause}`);
});
group.on('complete', ({name, runAt, completedAt}) => {
const prettyTime = Math.floor((completedAt - runAt) / 1000);
console.log(`${name} successfully completed in ${prettyTime}s`);
});
group.on('error', ({name, err}) => {
console.error(`${name} is completed with error\n${err.stack || err}`);
});
group.start();