npm

Need private packages and team management tools?Check out npm Orgs. »

mongo-scheduler-more

2.2.1 • Public • Published

mongo-scheduler-more

NPM version NPM downloads
CircleCI Badge Codacy Grade Badge Codacy Coverage Badge
David Dependencies Badge David Dev Dependencies Badge
Nodei.co badge

/!\ The version 2.0.0 has undergone a big upgrade /!\

  • Several methods have changed signatures! Be careful when updating to properly adapt all the methods you use.
  • New methods.
  • Updated deprecated methods of the mongodb driver.
  • Faster
  • More robust
  • A brand new documentation (below)

If you encounter problems, do not hesitate to create an issue (and / or pull requests) on the project github. If you like mongo-scheduler-more, do not hesitate to leave a star on the project github :)


Description

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 . 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!

You can visit my blog https://darkterra.fr/ for use case :)

Installation

npm install mongo-scheduler-more

Usage

Initialization

const msm = require('mongo-scheduler-more');
const scheduler = new msm('mongodb://localhost:27017/scheduler-db', options);

Arguments

  • connection <String> or <Object>
Type Description Optional
String or Object Use for initiate the connexion with MongoDB, you can use an classical connexion string or a mongoose connection object. false
  • options <Object>
Name Type Description Driver Option Optional
dbname String You can set (and overright) the name of DataBase to use. (only if you use the connexion string) false true
pollInterval Number Frequency in ms that the scheduler should poll the db. Default: 60000 (1 minute). false true
doNotFire Bool If set to true, this instance will only schedule events, not fire them. Default: false. false true
useNewUrlParser Bool If set to false, the mongo driver use the old parser. Default: true. true true
loggerLevel String The logging level (error / warn / info / debug). true true
logger Object Custom logger object. true true
validateOptions Bool Validate MongoClient passed in options for correctness. Default: false (only if you use the connection string) true true
auth Object { user: 'your_ddb_user', password: 'your_ddb_password'}. true true
authMechanism String Mechanism for authentication: MDEFAULT, GSSAPI, PLAIN, MONGODB-X509, or SCRAM-SHA-1 true true

schedule()

schedule method allows to create event (stored in MongoDB) that will trigger according to the conditions described below :

Schedules the most basic event:
const moment = require('moment');
const event  = { name: 'basicUsage', after: moment().add(1, 'hours').toDate()};
scheduler.schedule(event, (err, result) => {
  if (err) {
    console.error(err);
  }
});
// This event should trigger the "scheduler.on('basicUsage', callback);" in one hour

If is your first scheduling event, it's create the scheduled_events collection with your first event stored.

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 stored in mongodb.

Arguments

  • Event <Object>
Name Type Description Optional
name String Name of event that should be fired. false
after Date Time that the event should be triggered at, if left blank it will trigger the next time the scheduler polls. true
id ObjectId or String _id field of the document this event corresponds to. true
cron String (Override 'after'). A cron string representing a frequency this should fire on. Ex: cron: '0 0 23 * * *', see: cron-parser. true
endDate Date (Only if the cron option is use). Set a deadline to stop the infinite triggering of the cron option. true
collection Object Name of the collection to use for the query parameter (just below). true
query Object A MongoDB query expression to select document that this event should be triggered (only if the collection property is set) for. Ex: { payement: true }, see: document-query-filter. true
data Object or Primitive Extra data to attach to the event. true
  • callback <Function>
Name Type Description Optional
err String or Object Tell you what wrong when the module try to create or update a schedule event true
result Object The collection result callback. Contain 2 properties : lastErrorObject, value true

Schedules an event with data (stored directly inside the event object):
const moment = require('moment');
const event  = { 
  name: 'timeToCheckLicenceKey',
  after: moment().add(1, 'years').toDate(),
  data: 'First year offert ;)'
};
 
scheduler.schedule(event);
//
// This event (timeToCheckLicenceKey) should trigger in one year with extra data value
Schedules an event with id (stored in the storage event object):
const moment = require('moment');
const event  = {
  name: 'abandonedShoppingCart',
  id: '5a5dfd6c4879489ce958df0c',
  after: moment().add(15, 'minutes').toDate()
};
scheduler.schedule(event);
//
// This event trigger in 15 mins and allow my server to "remember" the shoppingCart _id: ('5a5dfd6c4879489ce958df0c')
// and let my server handle with to check if we need to remove this shopping cart
Schedules an event with collection, query and cron :
const moment = require('moment');
const event  = {
  name: 'creditCardCheck',
  collection: 'users',
  query: {},
  cron: '0 0 23 * * *'
};
scheduler.schedule(event);
//
// This event is triggered daily at 23h00:00 and allows you to retrieve the list
// of credit cards that expires in a month. The server only has to send emails to users
Schedules an event with collection, query and cron with an end date :
const moment = require('moment');
const event  = {
  name: 'creditCardCheck',
  collection: 'users',
  query: {},
  cron: '0 0 10 * * *',
  endDate: moment().add(5, 'years').toDate()
};
scheduler.schedule(event);
//
// This event is triggered daily at 10h00:00 and allows you to retrieve the list
// of credit cards that expires in a month. The server only has to send emails to users

scheduleBulk()

scheduleBulk method allows to create multiple events at one time (stored in MongoDB) that will trigger according to the conditions described below :

Schedules the most basic event:
const events = [{ 
  name: 'event-to-bulk', 
  after: moment().add(15, 'm').toDate()
}, {
  name: 'event-to-bulk',
  after: moment().add(25, 'm').toDate()
}, {
  name: 'event-to-bulk',
  after: moment().add(8, 'm').toDate()
}, {
  name: 'event-to-bulk',
  after: moment().add(66, 'm').toDate()
}, {
  name: 'event-to-bulk',
  after: moment().add(5000, 'm').toDate()
}, {
  name: 'event-to-bulk',
  data: 'this is hacked scheduler !!!',
  after: moment().add(5000, 'm').toDate()  // This event has the same name and after value, so it will update the event just above
}];
 
scheduler.scheduleBulk(events, (err, result) => {
  if (err) {
    console.error(err);
  }
});
// This event should trigger the "scheduler.on('event-to-bulk', callback);" 8 min, and in 15 min, and in 15 min, and in 66 min, and in 5000 min

If is your first scheduling event, it's create the scheduled_events collection with your first event stored.

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 stored in mongodb.

Arguments

  • Events [<Object>]
Name Type Description Optional
name String Name of event that should be fired. false
after Date Time that the event should be triggered at, if left blank it will trigger the next time the scheduler polls. true
id ObjectId or String _id field of the document this event corresponds to. true
cron String (Override 'after'). A cron string representing a frequency this should fire on. Ex: cron: '0 0 23 * * *', see: cron-parser. true
endDate Date (Only if the cron option is use). Set a deadline to stop the infinite triggering of the cron option. true
collection Object Name of the collection to use for the query parameter (just below). true
query Object A MongoDB query expression to select document that this event should be triggered (only if the collection property is set) for. Ex: { payement: true }, see: document-query-filter. true
data Object or Primitive Extra data to attach to the event. true
  • callback <Function>
Name Type Description Optional
err String or Object Tell you what wrong when the module try to create or update a schedule event true
result Object The collection result callback. true

scheduler.on

on method allows to listen trigger events (stored in MongoDB) described below :

Most basic event handler:
function callback (event) {
  console.log(`This is my basicUsage event content: ${event}`);
}
 
scheduler.on('basicUsage', callback);

Arguments

  • name <String>
Type Description Optional
String Name of listened event false
  • callback <Function>
Name Type Description Optional
event Object This is the original event stored into MongoDB when you use the scheduler.schedule() function true
result Object or Array If you use the properties collection and query, you get the result here true
Event handler and data property:
function callback (event) {
  console.log(`This is my timeToCheckLicenceKey event content: ${event}`);
  
  // Do what you whant with this datas
}
 
scheduler.on('timeToCheckLicenceKey', callback);
// This handler will be fired in one year and the event object contain the "data" property
Event handler with the id property:
function callback (event) {
  console.log(`This is my abandonedShoppingCart event content: ${event}`);
  
  // Do what you whant with this datas
}
 
scheduler.on('abandonedShoppingCart', callback);
// This handler will be fired in 15 min and the event object contain the "id" property
Event handler with result:
function callback (event, result) {
  console.log(`This is my creditCardCheck event content: ${event}`);
  console.log(`And this is the result of the query saved when the event is declared: ${result}`);
  
  // Do what you whant with this datas
}
 
scheduler.on('creditCardCheck', callback);
// Every days at 23h00:00, this event is trigger with the result query !

scheduler.list

list method allows to list all events (stored in MongoDB) :

const options = {};
scheduler.list(options, (err, events) => {
  // Do something with events, by default return by the date and time they were added to the db
});

Arguments

  • options <Object>
Name Type Description Optional
bySchedule Bool Return list of events by schedule time (after property) true
asc Int 1 return ascendant schedule time. -1 return descendant schedule time Default: 1 true
query Object Filter the results like with valid mongodb query. For more infos take a look here true
  • callback <Function>
Name Type Description Optional
err String or Object Tell you what wrong when the module try list all events true
result [Object] List of object true

scheduler.findByName

findByName method allows to get the first event by name (stored in MongoDB) :

scheduler.findByName({ name: 'abandonedShoppingCart' }, (err, event) => {
  // Do something with event
});

Arguments

  • name <String>
Name Type Description Optional
name String Name of listened event false
  • callback <Function>
Name Type Description Optional
err String or Object Tell you what wrong when the module try trigger the event true
event Object This is the original event stored into MongoDB when you use the scheduler.schedule() function true

scheduler.findByStorageId

findByStorageId method allows to get the first event by id

_ /!\ Be careful, this is not the id of the event itself, but the id stored in the id property (stored in MongoDB) :_

const params = { id: '5a5dfd6c4879489ce958df0c', name: 'abandonedShoppingCart' };
scheduler.findByStorageId(params, (err, event) => {
  // Do something with event
});

Arguments

  • params <Object>
Name Type Description Optional
id ObjectId or String The id searched (remember, this id is not the event itself id) false
name String Name of listened event true
  • callback <Function>
Name Type Description Optional
event Object This is the original event stored into MongoDB when you use the scheduler.schedule() function true
result Object or Array If you use the properties collection and query, you get the result here. true

scheduler.remove

remove method allows to remove events :

const params = { name: 'abandonedShoppingCart' };
scheduler.remove(params, (err, event) => {
  // Event has been removed
});
// Remove every events find with the name = 'abandonedShoppingCart'

Arguments

  • params <Object>
Name Type Description Optional
name String Name of listened event false
id ObjectId or String The id searched (remember, this id is not the event itself id) true
eventId ObjectId or String This is the event id itself (you can use the 'list' method to get the event id) true
after Date Remove only the events who have the exacte same date true
  • callback <Function>
Name Type Description Optional
event Object This is the original event stored into MongoDB when you use the scheduler.schedule() function true
result Object or Array If you use the properties collection and query, you get the result here true

scheduler.purge

purge method allows to remove ALL events :

const params = { force: true };
scheduler.purge(params, (err, event) => {
  // Event has been removed
});
// Remove every events

Arguments

  • params <Object>
Name Type Description Optional
force Bool It's a simple crazy guard, just not to delete all the events stored inadvertently false
  • callback <Function>
Name Type Description Optional
event Object This is the original event stored into MongoDB when you use the scheduler.schedule() function true
result Object or Array If you use the properties collection and query, you get the result here true

scheduler.enable

enable method allows to enable scheduler :

scheduler.enable();

scheduler.disable

disable method allows to disable scheduler :

scheduler.disable();

scheduler.version

version this method show the actual version of mongo-scheduler-more :

scheduler.version();
// Show in the console the actual version of mongo-scheduler-more

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

install

npm i mongo-scheduler-more

Downloadsweekly downloads

7

version

2.2.1

license

MIT

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability