New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More

cron-group

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cron-group

manage a group of cron workers

2.0.0
latest
Version published
Weekly downloads
2
-50%
Maintainers
1
Weekly downloads
 
Created

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

FAQs

Package last updated on 03 Sep 2020

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