cron-group
v2.0.0
Published
manage a group of cron workers
Downloads
21
Maintainers
Readme
cron-group
Manage a group of cron workers. Based on node-cron.
Install
$ npm i cron-group
API
constructor([options])
options
- optionaltimezone
- pass it to CronJob constructor
add({name, schedule, worker})
name
- name of jobschedule
- schedule in cron formatworker
- worker function
Add cron job to group without starting.
run(name)
name
- name of job to run
Run specified by name job.
start()
Enable cron for all added jobs.
stop()
Disable cron for all added jobs, and wait until all jobs is complete.
Events
CronGroup is subclass of EventEmitter, so it fires some events.
on('complete', {name, result, runAt, completedAt})
name
- name of jobresult
- result returned from worker functionrunAt
- when job is runcompletedAt
- whe job is complete
on('error', {name, err})
name
- name of joberr
- error object caught in worker function
Usage example
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();