This is an event emitter that uses crontab instructions to register events to be emitted at regular intervals, or at certain specific times in the future. This module uses cron-parser to parse the crontab instructions. See also node-cron for a similar project with a more traditional node.js callback approach.
The main difference between this Library and node-cron is that this library aims to implement the event listener pattern, to achieve capabilities found in the Observer Pattern and the PUB/SUB Pattern
This enables you to write highly decoupled, functional and reactive code with lower complexity per function than callback driven code. You can delegate responsibility for dealing with events to as many different subsystems as we want, all listening to the same event notifications.
$ npm install --save cron-emitter
See cron-parser for a simple introduction to the crontab syntax. If you're on a Linux or OS X computer type
$ man crontab
const CronEmitter = require('cron-emitter');
const emitter = new CronEmitter();
emitter.add('0 */30 * * * *', 'every_thirty_minutes');
CronEmitter exposes the same options as cron-parser to provide a start date and an end date for when events should be emitted:
emitter.add('0 0 */2 * * *', 'every_two_hours', {
currentDate: new Date(),
endDate: '2017-05-31'
});
Return a new CronEmitter object
Adds a new event to the list of events to be emitted. CronEmitter exposes the same options as cron-parser to provide a start date and an end date for when events should be emitted:
- Kind: instance method of
CronEmitter
- Returns:
CronEmitter
- A reference to self. - See: https://www.npmjs.com/package/cron-parser
Param | Type | Description |
---|---|---|
crontab | string |
the crontab declaration |
name | string |
the name of the event you want to emit. |
options | object |
an object with options to cron-parser |
Remove an event from the list of events
- Kind: instance method of
CronEmitter
- Returns:
CronEmitter
- self when successful - Throws:
- TypeError
Param | Type | Description |
---|---|---|
name | string |
of the event to remove. |
Returns object with all the registered emitters
- Kind: instance method of
CronEmitter
- Returns:
object
- the list of events.
const CronEmitter = require('../lib/cron-emitter');
const emitter = new CronEmitter();
const now = () => (new Date()).toJSON();
const increment = (counter = 0) => (counter + 1);
emitter.add('*/3 * * * * *', 'every_three_seconds');
emitter.add('*/10 * * * * *', 'every_ten_seconds');
emitter.add('0 * * * * *', 'every_minute');
emitter.add('0 */5 * * * *', 'every_five_minutes');
emitter.add('0 */30 * * * *', 'every_thirty_minutes');
emitter.add('* * * * * *', 'every_second_stop', {
endDate: new Date(Date.now() + 10500)
});
console.log(now(), "==> Done setting up events");
emitter.on('ended', (name) => {
console.log(now(), `==> ENDED: event series "${name}" has ended.`);
switch (name) {
case 'every_second_stop':
emitter.add(
'*/2 * * * * *', 'every_two_seconds',
{endDate: new Date(Date.now() + 10500)}
);
break;
}
});
emitter.on('every_three_seconds', () => {
console.log(now(), "==> EVENT: Got every three seconds event.");
});
emitter.on('every_ten_seconds', () => {
console.log(now(), "==> EVENT: Got ten seconds event.");
});
emitter.on('every_minute', () => {
console.log(now(), "==> EVENT: A minute has passed");
});
emitter.on('every_five_minutes', () => {
console.log(now(), "==> EVENT: Five minutes has passed");
});
emitter.on('every_thirty_minutes', () => {
console.log(now(), "==> EVENT: Thirty minutes has passed");
});
const increment = (counter = 0) => (counter + 1);
let counter = 0;
emitter.on("every_second_stop", () => {
counter = increment(counter);
console.log(now(), "==> EVENT: got every second event: ", counter);
});
emitter.on('every_two_seconds', () => {
console.log(now(), "==> EVENT: got every two seconds event.");
});