forked from jespinoza711/mongo-scheduler
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
214 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,86 @@ | ||
var _ = require('lodash'), | ||
parser = require('cron-parser') | ||
'use strict'; | ||
|
||
const _ = require('lodash'); | ||
const parser = require('cron-parser'); | ||
|
||
function SchedulerError(message) { | ||
this.name = "SchedulerError" | ||
this.message = message || "Unexpected Scheduler Error" | ||
this.name = "SchedulerError"; | ||
this.message = message || "Unexpected Scheduler Error"; | ||
} | ||
SchedulerError.prototype = new Error() | ||
|
||
SchedulerError.prototype = new Error(); | ||
SchedulerError.prototype.constructor = SchedulerError; | ||
|
||
function translateQueryfields(queryfields) { | ||
return _.map(queryfields.split(' '), function(field) { | ||
if(field === 'collection' || field === 'id') | ||
return 'storage.' + field | ||
else if(field === 'query' || field === 'after') | ||
return 'conditions.' + field | ||
else if(field === 'name') return 'event' | ||
else return field | ||
}) | ||
return _.map(queryfields.split(' '), (field) => { | ||
if(field === 'collection' || field === 'id') { | ||
return 'storage.' + field; | ||
} | ||
else if(field === 'query' || field === 'after') { | ||
return 'conditions.' + field; | ||
} | ||
else if(field === 'name') { | ||
return 'event'; | ||
} | ||
else { | ||
return field; | ||
} | ||
}); | ||
} | ||
|
||
module.exports = { | ||
buildSchedule: function(details) { | ||
var storage = _.extend({}, _.pick(details, 'collection', 'id')) | ||
var conditions = _.extend({}, _.pick(details, 'query', 'after')) | ||
var options = _.defaults(details.options || {}, { | ||
buildSchedule: (details) => { | ||
const storage = _.extend({}, _.pick(details, 'collection', 'id')); | ||
const conditions = _.extend({}, _.pick(details, 'query', 'after')); | ||
const options = _.defaults(details.options || {}, { | ||
emitPerDoc: false, | ||
queryFields: 'name collection id' | ||
}) | ||
queryFields: 'name collection id after' | ||
}); | ||
|
||
var doc = { | ||
const doc = { | ||
status: details.status || 'ready', | ||
event: details.name, | ||
storage: storage, | ||
conditions: conditions, | ||
storage, | ||
conditions, | ||
data: details.data, | ||
options: options | ||
} | ||
options | ||
}; | ||
|
||
var queryFields = translateQueryfields(options.queryFields) | ||
var query = _.transform(queryFields, function(memo, f) { | ||
memo[f] = _.get(doc, f) | ||
}, {}) | ||
const queryFields = translateQueryfields(options.queryFields); | ||
const query = _.transform(queryFields, function(memo, f) { | ||
memo[f] = _.get(doc, f); | ||
}, {}); | ||
|
||
if (!!details.cron) { | ||
doc.cron = details.cron | ||
doc.conditions.after = parser.parseExpression(details.cron).next() | ||
doc.cron = details.cron; | ||
doc.conditions.after = parser.parseExpression(details.cron).next(); | ||
} | ||
|
||
return { doc: doc, query: query } | ||
return { doc, query }; | ||
}, | ||
|
||
buildEvent: function(doc) { | ||
if (!doc) return; | ||
buildEvent: (doc) => { | ||
if (!doc) { | ||
return; | ||
} | ||
|
||
doc.conditions.query = doc.conditions.query || {} | ||
if (typeof doc.conditions.query === 'string') | ||
doc.conditions.query = JSON.parse(doc.conditions.query) | ||
if(doc.storage && doc.storage.id) | ||
_.extend(doc.conditions.query, {_id: doc.storage.id}) | ||
return doc | ||
doc.conditions.query = doc.conditions.query || {}; | ||
|
||
if (typeof doc.conditions.query === 'string') { | ||
doc.conditions.query = JSON.parse(doc.conditions.query); | ||
} | ||
if(doc.storage && doc.storage.id) { | ||
_.extend(doc.conditions.query, {_id: doc.storage.id}); | ||
} | ||
|
||
return doc; | ||
}, | ||
|
||
shouldExit: function(err, result) { | ||
return !!err || !!(result.lastErrorObject && result.lastErrorObject.err) | ||
return !!err || !!(result.lastErrorObject && result.lastErrorObject.err); | ||
}, | ||
|
||
buildError: function(err, result) { | ||
return err || new SchedulerError(result.lastErrorObject.err) | ||
return err || new SchedulerError(result.lastErrorObject.err); | ||
} | ||
} | ||
}; |
Oops, something went wrong.