diff options
Diffstat (limited to 'src/services/events/event.model.js')
-rw-r--r-- | src/services/events/event.model.js | 21 |
1 files changed, 11 insertions, 10 deletions
diff --git a/src/services/events/event.model.js b/src/services/events/event.model.js index ef86572..b8a13bc 100644 --- a/src/services/events/event.model.js +++ b/src/services/events/event.model.js @@ -1,5 +1,4 @@ const cron = require('cron'); -const Bluebird = require('bluebird'); const { model } = require('mongoose'); const schema = require('./event.schema.js'); @@ -11,26 +10,27 @@ schema.methods.computeNextRunAt = function() { return new Date(nextRunAt); }; +schema.methods.setStatus = function(status) { + this.status = status;; + this.save(); +}; + schema.pre('save', function(next) { this.nextRunAt = this.computeNextRunAt(); next(); }); -schema.statics.rescheduleOldEvents = async function () { - console.log('Reschedule old events'); - const oldEvents = await this.find({ +schema.statics.findMissedEvents = async function () { + return this.find({ nextRunAt: { // TODO: skip single-fire events $lt: new Date() }, }); - - // Saving events triggers computing new nextRunAt - return Bluebird.map(oldEvents, event => event.save()); }; -schema.statics.findNextEvent = function () { - return this.findOne( +schema.statics.findNextEvents = function(limit = 10) { + return this.find( { nextRunAt: { $exists: 1, @@ -41,7 +41,8 @@ schema.statics.findNextEvent = function () { { sort: { nextRunAt: 1 - } + }, + limit } ) }; |