Skip to content

dvpnt/cron-group

Repository files navigation

cron-group

Build Status Coverage Status NPM Version

Manage a group of cron workers. Based on node-cron.

Install

$ npm i cron-group

API

constructor([options])

  • options - optional
    • timezone - pass it to CronJob constructor

add({name, schedule, worker})

  • name - name of job
  • schedule - schedule in cron format
  • worker - 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 job
  • result - result returned from worker function
  • runAt - when job is run
  • completedAt - whe job is complete

on('error', {name, err})

  • name - name of job
  • err - 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();

License

The MIT License