npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cron-emitter

v1.2.2

Published

An event emitter for registering scheduled events in crontab format

Downloads

16

Readme

npm version Build Status Code Climate Test Coverage Dependency Status

cron-emitter

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.

Table of contents

Install

$ npm install --save cron-emitter

Crontab syntax

See cron-parser for a simple introduction to the crontab syntax. If you're on a Linux or OS X computer type

$ man crontab

Usage

Creating an object

const CronEmitter = require('cron-emitter');
const emitter     = new CronEmitter();

Adding an event

emitter.add('0 */30 * * * *', 'every_thirty_minutes');

Options

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'
});

API

new CronEmitter()

Return a new CronEmitter object

cronEmitter.add(crontab, name, options) ⇒ CronEmitter

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 |

cronEmitter.remove(name) ⇒ CronEmitter

Remove an event from the list of events

| Param | Type | Description | | --- | --- | --- | | name | string | of the event to remove. |

cronEmitter.getEventList() ⇒ object

Returns object with all the registered emitters

  • Kind: instance method of CronEmitter
  • Returns: object - the list of events.

Example

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.");
});