diff options
Diffstat (limited to 'src/services')
-rw-r--r-- | src/services/events/event.model.js | 82 | ||||
-rw-r--r-- | src/services/events/event.schema.js | 22 | ||||
-rw-r--r-- | src/services/events/event.service.js | 71 | ||||
-rw-r--r-- | src/services/events/log.service.js (renamed from src/services/logs/log.service.js) | 2 | ||||
-rw-r--r-- | src/services/events/models.js | 7 | ||||
-rw-r--r-- | src/services/index.js | 2 | ||||
-rw-r--r-- | src/services/logs/log.model.js | 7 | ||||
-rw-r--r-- | src/services/logs/log.schema.js | 7 |
8 files changed, 17 insertions, 183 deletions
diff --git a/src/services/events/event.model.js b/src/services/events/event.model.js deleted file mode 100644 index 660cb1a..0000000 --- a/src/services/events/event.model.js +++ /dev/null @@ -1,82 +0,0 @@ -const cron = require('cron'); -const { model } = require('mongoose'); -const schema = require('./event.schema.js'); -const LogModel = require('../logs/log.model.js'); - -const CronJob = cron.CronJob; - -schema.methods.log = function(message) { - const dateOpts = { timeStyle: 'medium', dateStyle: 'short' }; - const timestamp = new Date().toLocaleString('en', dateOpts); - console.log(`[${timestamp}] ${this.name}: ${message}`); - - return LogModel.create({ - eventId: this._id, - message - }); -}; - -schema.methods.start = function() { - this.log('Event started') - this.lastRunAt = new Date(); - this.status = 'running'; - return this.save(); -}; - -schema.methods.complete = function() { - this.log('Event complete') - this.status = 'complete'; - return this.save(); -}; - -schema.methods.fail = function(error) { - this.log(error); - this.error = error; - this.status = 'failed'; - return this.save(); -}; - -schema.methods.computeNextRunAt = function() { - const job = new CronJob(this.schedule); - const nextRunAt = job.nextDates(); - return new Date(nextRunAt); -}; - -schema.pre('save', function(next) { - this.nextRunAt = this.computeNextRunAt(); - next(); -}); - -schema.statics.findMissedEvents = async function () { - return this.find({ - nextRunAt: { - // TODO: skip single-fire events - $lt: new Date() - }, - }); -}; - -schema.statics.findNextEvents = function(limit = 10) { - return this.find( - { - nextRunAt: { - $gt: new Date() - }, - }, - null, - { - sort: { - nextRunAt: 1 - }, - limit - } - ) -}; - -const Model = model('Event', schema); - - -module.exports = Model; - - - diff --git a/src/services/events/event.schema.js b/src/services/events/event.schema.js deleted file mode 100644 index aff19f1..0000000 --- a/src/services/events/event.schema.js +++ /dev/null @@ -1,22 +0,0 @@ -const { Schema } = require('mongoose'); - -module.exports = new Schema({ - name: { - type: String, - required: true, - unique: true - }, - schedule: { - type: String, - required: true - }, - status: { - type: String, - default: 'notStarted' - }, - error: String, - context: {}, - nextRunAt: Date, - lastRunAt: Date -}, { timestamps: true }); - diff --git a/src/services/events/event.service.js b/src/services/events/event.service.js index f2185a6..45aebd3 100644 --- a/src/services/events/event.service.js +++ b/src/services/events/event.service.js @@ -1,71 +1,16 @@ -const { Types } = require('mongoose'); -const { Service } = require('feathers-mongoose'); -const _ = require('lodash'); -const cron = require('cron'); -const Bluebird = require('bluebird'); - -const Model = require('./event.model.js'); +const { Scheduler } = require('mongo-cronjob'); +const service = require('feathers-mongoose'); const handleAttendClassJob = require('../../handlers'); -const CronJob = cron.CronJob; +const Model = require('./models.js').Event; const handleTestJob = () => new Promise(res => setTimeout(res, 10000)); -class Events extends Service { - async setup(app, path) { - this.jobs = []; - await this.rescheduleMissedEvents(); - - const job = new CronJob('*/10 * * * * *', () => this.updateJobs()); - job.start(); - } - - startAllJobs() { - this.jobs.forEach(job => job.start()); - } - - stopAllJobs() { - this.jobs.forEach(job => job.stop()); - } - - async rescheduleMissedEvents() { - const missedEvents = await this.Model.findMissedEvents(); - return Bluebird.map(missedEvents, event => event.save()); - } - - async updateJobs() { - // Reschedule missed events before we stop jobs to avoid - // accidentally stopping the job that has not triggered yet - // (if event schedule resonates with updateJobs schedule) - await this.rescheduleMissedEvents(); - - this.stopAllJobs(); - - const events = await this.Model.findNextEvents(); - if (!events.length) console.log('WARNING: no upcoming events'); - this.jobs = events.map(event => new CronJob( - event.schedule, - () => this.run(event._id) - )); - - this.startAllJobs(); - } - - async run(id) { - const event = await this.Model.findById(id); - - try { - event.start(); - await handleAttendClassJob(event); - return event.complete(); - } catch (error) { - event.fail(error); - } - } -} - - module.exports = app => { - app.use('/events', new Events({ Model })); + app.use('/events', service({ Model })); + + const scheduler = new Scheduler(Model); + scheduler.registerHandler('class', handleAttendClassJob); + scheduler.registerHandler('test', handleTestJob); }; diff --git a/src/services/logs/log.service.js b/src/services/events/log.service.js index 899ec86..62d96a9 100644 --- a/src/services/logs/log.service.js +++ b/src/services/events/log.service.js @@ -1,5 +1,5 @@ const service = require('feathers-mongoose'); -const Model = require('./log.model.js'); +const Model = require('./models.js').Log; const LogService = service({ Model }); diff --git a/src/services/events/models.js b/src/services/events/models.js new file mode 100644 index 0000000..72b10d7 --- /dev/null +++ b/src/services/events/models.js @@ -0,0 +1,7 @@ +const mongoose = require('mongoose'); +const { Client } = require('mongo-cronjob'); + +const client = new Client(mongoose.connection); + +module.exports = client; + diff --git a/src/services/index.js b/src/services/index.js index e02a4b8..c7a6cc5 100644 --- a/src/services/index.js +++ b/src/services/index.js @@ -1,6 +1,6 @@ const Users = require('./users/user.service.js'); const Events = require('./events/event.service.js'); -const Logs = require('./logs/log.service.js'); +const Logs = require('./events/log.service.js'); const Auth = require('./auth/auth.service.js'); module.exports = app => { diff --git a/src/services/logs/log.model.js b/src/services/logs/log.model.js deleted file mode 100644 index 64b7da6..0000000 --- a/src/services/logs/log.model.js +++ /dev/null @@ -1,7 +0,0 @@ -const { model } = require('mongoose'); -const schema = require('./log.schema.js'); - -const Model = model('Log', schema); - - -module.exports = Model; diff --git a/src/services/logs/log.schema.js b/src/services/logs/log.schema.js deleted file mode 100644 index bb8d4a5..0000000 --- a/src/services/logs/log.schema.js +++ /dev/null @@ -1,7 +0,0 @@ -const { Schema, Types } = require('mongoose'); - -module.exports = new Schema({ - eventId: Types.ObjectId, - message: String -}, { timestamps: true }); - |