Socket
Socket
Sign inDemoInstall

cron-group

Package Overview
Dependencies
3
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    cron-group

manage a group of cron workers


Version published
Maintainers
1
Install size
7.09 MB
Created

Readme

Source

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

Keywords

FAQs

Last updated on 03 Sep 2020

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc