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
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!
You can visit my blog https://darkterra.fr/ for use case :)
Installation
npm install mongo-scheduler-more
Usage
Initialization
const Scheduler = require('mongo-scheduler-more');
const scheduler = new Scheduler(connection, options);Arguments
- connection <String or Object> - mongodb connections string (i.e.: "mongodb://localhost:27017/scheduler-db") or a mongoose connection object.
- options <Object> - Options object.
Valid Options Object
- dbname <String> - You can set (and overright) the name of DataBase to use (optional if you precise the dbname in connection string).
- 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 - useNewUrlParser <Bool> -
Driver Option- If set to false, the mongo driver use the old parser.Default: true - loggerLevel <String> -
Driver Option- The logging level (error/warn/info/debug). (optional) - logger <Object> -
Driver Option- Custom logger object. (optional) - validateOptions <Bool> -
Driver Option- Validate MongoClient passed in options for correctness.Default: false(only if you use the connection string) - auth <Object> -
Driver Option- { user: 'your_ddb_user', password: 'your_ddb_password'}. (optional) - authMechanism <String> -
Driver Option- Mechanism for authentication: MDEFAULT, GSSAPI, PLAIN, MONGODB-X509, or SCRAM-SHA-1. (optional)
schedule()
Schedules an event.
const event = { name: 'abandonedShoppingCart', 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 (Override 'after'). (optional)
- collection <Object> - Info about the documents this event corresponds to. (optional)
- id <ObjectId> - Value of the _id field of the document this event corresponds to. (optional)
- after <Date> - Time that the event should be triggered at, if left blank it will trigger the next time the scheduler polls. (optional)
- query <Object> - a MongoDB query expression to select records that this event should be triggered for. (optional)
- data <Object|Primitive> - Extra data to attach to the event. (optional)
scheduler.on
Event handler.
scheduler.on('abandonedShoppingCart', (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('abandonedShoppingCart', (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: 'abandonedShoppingCart' id: '5a5dfd6c4879489ce958df0c', after: new Date() };
scheduler.schedule(event);
scheduler.findByStorageId('5a5dfd6c4879489ce958df0c', 'abandonedShoppingCart', (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('abandonedShoppingCart', null, null, (err, event) => {
// Event has been removed
});Arguments
- eventName <String> - Name of event.
- id <String> - Id of event. (optional)
- after <String> - After of event (date). (optional)
- 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
