Skip to content

Commit

Permalink
New feature: 'scheduler.version()' => show version of the module
Browse files Browse the repository at this point in the history
  • Loading branch information
darkterra committed Mar 11, 2019
1 parent 3f37da2 commit 44f4ae3
Showing 1 changed file with 35 additions and 20 deletions.
55 changes: 35 additions & 20 deletions lib/scheduler.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
'use strict';

const helper = require('./helper');
const mongo = require('mongodb');
const objectId = mongo.ObjectId;
const events = require('events');
const parser = require('cron-parser');
const helper = require('./helper');
const mongo = require('mongodb');
const objectId = mongo.ObjectId;
const events = require('events');
const parser = require('cron-parser');
const { readFile } = require('fs');
const { join } = require('path');

const collectionName = 'scheduled_events';

Expand Down Expand Up @@ -91,7 +93,7 @@ function Scheduler(connection, options = {}) {
if(cb) {
cb();
}

setTimeout(() => {
if (event.cron) {
collection.findOneAndUpdate(lookup, { $set: { status: 'ready', 'conditions.after': parser.parseExpression(event.cron).next() }}, { upsert: true }, manageResult);
Expand All @@ -116,15 +118,15 @@ function Scheduler(connection, options = {}) {
if(helper.shouldExit(err, result)) {
return self.emit('error', helper.buildError(err, result));
}

const event = helper.buildEvent(result.value);
if (!event) {
return;
}
if (!event.storage.collection) {
return emit(event, null, poll);
}

db.collection(event.storage.collection, (err, childColl) => {
if(err) {
return self.emit('error', err, event);
Expand All @@ -141,7 +143,7 @@ function Scheduler(connection, options = {}) {
if (!doc) {
return poll();
}

emit(event, doc);
});
}
Expand All @@ -161,13 +163,13 @@ function Scheduler(connection, options = {}) {
});
});
}

function whenReady(op) {
return function() {
if(ready) {
return op.apply(self, arguments);
}

const args = arguments;
const id = setInterval(() => {
if (!ready) {
Expand All @@ -179,39 +181,39 @@ function Scheduler(connection, options = {}) {
}, 10);
};
}

function initialize() {
poll();
setInterval(poll, options.pollInterval || 60000);
}

function schedule(details, cb) {
const info = helper.buildSchedule(details), callback = cb || function() {};
const collection = db.collection(collectionName);

collection.findOneAndReplace(info.query, info.doc, { upsert: true }, callback);
}

function list(cb) {
const collection = db.collection(collectionName);

collection.find({}).toArray(cb);
}

function findByName(name, cb) {
const collection = db.collection(collectionName);
const filter = { event: name };

collection.findOne(filter, cb);
}

function findByStorageId(id, name, cb) {
const collection = db.collection(collectionName);
const filter = { 'storage.id': id.toString(), event: name };

collection.findOne(filter, cb);
}

function remove(name, id, after, cb) {
if (name || id || after) {
const event = {};
Expand All @@ -236,7 +238,7 @@ function Scheduler(connection, options = {}) {
throw 'Mongo-Scheduler-More: Bad parameters';
}
}

function updateStatus(status) {
return (event, cb) => {
const collection = db.collection(collectionName);
Expand All @@ -249,15 +251,28 @@ function Scheduler(connection, options = {}) {
});
};
}


function version() {
readFile(join(__dirname, '..', 'package.json'), (err, data) => {
if (err) {
throw err;
}
if (data) {
console.log(`${JSON.parse(data).version}`);
process.exit(0);
}
});
}

this.schedule = whenReady(schedule);
this.list = whenReady(list);
this.findByName = whenReady(findByName);
this.findByStorageId = whenReady(findByStorageId);
this.remove = whenReady(remove);
this.enable = whenReady(updateStatus('ready'));
this.disable = whenReady(updateStatus('disabled'));

this.version = version;

if(!options.doNotFire) {
whenReady(initialize)();
}
Expand Down

0 comments on commit 44f4ae3

Please sign in to comment.