diff options
author | Eugene Sokolov <eug-vs@keemail.me> | 2020-12-04 01:33:37 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-04 01:33:37 +0300 |
commit | fac35a9a0ad1628cf31380bf29c251f6c51f13f1 (patch) | |
tree | 0aa142dce4a5d8dc4e90b090007cfbba3b4fc0fb /src | |
parent | 778f614a8eb753529c02d71e49486846391f0a3f (diff) | |
parent | 8ef929e503b00d93c5fee4ab996aeaa9abf283d3 (diff) | |
download | bsu-fantom-master.tar.gz |
Refactor/mongo cronjob
Diffstat (limited to 'src')
-rw-r--r-- | src/connectDb.js | 6 | ||||
-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 |
9 files changed, 18 insertions, 188 deletions
diff --git a/src/connectDb.js b/src/connectDb.js index c7ad13b..416ae1d 100644 --- a/src/connectDb.js +++ b/src/connectDb.js @@ -32,9 +32,5 @@ const connectDb = () => new Promise ((resolve, reject) => { }); }); -const getConnection = () => connection; -module.exports = { - connectDb, - getConnection -}; +module.exports = connectDb; 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 }); - |