JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 32
  • Score
    100M100P100Q77992F
  • License MIT

Persistent event scheduler using mongo as storage

Package Exports

  • mongo-scheduler-more

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (mongo-scheduler-more) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

mongo-scheduler-more CircleCI

CircleCI Badge
NPM version NPM downloads
Nodei.co badge

Persistent event scheduler using mongodb as storage

Provide the scheduler with some storage and timing info and it will emit events with the corresponding document at the right time

This module, extend the original mongo-sheduler and work with up to date dependencies plus possibilities to remove an event recorded in database. You can also use the same event name multiple times, as long as the "id" and / or "after" is different, otherwise it will update the document.

With this module, increase the performance of your Node.JS application ! You can completely replace your data scanning system in Data Base and more. Node.JS is EventDriven, exploit this power within your application!

Installation

npm install mongo-scheduler-more

Usage

Initialization

const Scheduler = require('mongo-scheduler-more')
const scheduler = new Scheduler(connection, options)

Arguments

  • connectionString <String or Object> - mongodb connections string (i.e.: "mongodb://localhost:27017/scheduler-db") or a mongoose connection object
  • options <Object> - Options object

Valid Options

  • dbname <String> - You can set (and overright) the name of DataBase to use
  • pollInterval <Number> - Frequency in ms that the scheduler should poll the db. Default: 60000 (1 minute)
  • doNotFire <bool> - If set to true, this instance will only schedule events, not fire them. Default: false

schedule()

Schedules an event.

const event = {name: 'breakfast' collection: 'meals', after: new Date(), data: 'Fry'}
scheduler.schedule(event)

Arguments

  • event<Object> - Event details
  • [callback] \Function> - callabck

Event Fields

  • name <String> - Name of event that should be fired
  • [cron] <String> - A cron string representing a frequency this should fire on
  • [collection] <Object> - Info about the documents this event corresponds to
  • [id] <ObjectId> - Value of the _id field of the document this event corresponds to
  • [after] <Date> - Time that the event should be triggered at, if left blank it will trigger the next time the scheduler polls
  • [query] <Object> - a MongoDB query expression to select records that this event should be triggered for
  • [data] <Object|Primitive> - Extra data to attach to the event

scheduler.on

Event handler.

scheduler.on('breakfast', (meal, event) => {
  console.log(event.data + " the " + meal.ingredients)
  // Assuming the document {ingredients: "Bacon and Eggs"} is in the meals collection
  // prints "Fry the Bacon and Eggs"
})

Arguments

  • eventName <String> - Name of event
  • handler <Function> - handler

scheduler.list

List all events.

scheduler.list((err, events) => {
  // Do something with events
})

Arguments

  • handler <Function> - handler

scheduler.findByName

Find an event by name.

scheduler.findByName('breakfast', (err, event) => {
  // Do something with event
})

Arguments

  • eventName <String> - Name of event
  • handler <Function> - handler

scheduler.findByStorageId

Find an event by id in storage object and by name.

const event = {name: 'breakfast' id: '5a5dfd6c4879489ce958df0c', after: new Date()}
scheduler.schedule(event)

scheduler.findByStorageId('5a5dfd6c4879489ce958df0c', 'breakfast', (err, event) => {
  // Do something with event
})

Arguments

  • storageID <ObjectId> - Value of the _id field in storage object
  • eventName <String> - Name of event
  • handler <Function> - handler

scheduler.remove

Remove an event.

scheduler.remove('breakfast', null, null, (err, event) => {
  // Event has been removed
})

Arguments

  • eventName <String> - Name of event
  • [id] <String> - Id of event
  • [after] <String> - After of event (date)
  • handler <Function> - handler

scheduler.enable

Enable scheduler.


scheduler.disable

Disable scheduler.


Error handling

If the scheduler encounters an error it will emit an 'error' event. In this case the handler, will receive two arguments: the Error object, and the event doc (if applicable).

License

MIT License