diff options
author | eug-vs <eug-vs@keemail.me> | 2020-11-28 01:11:54 +0300 |
---|---|---|
committer | eug-vs <eug-vs@keemail.me> | 2020-11-28 01:11:54 +0300 |
commit | 2a1b70edbdef4ab934d0091a92c14327dde8a256 (patch) | |
tree | 3c77e7fc98f23fc437a641da3a496bdc29c66f42 | |
parent | 86f62a65c4f9ceac086b16fb290544caf2ed1da5 (diff) | |
download | bsu-fantom-2a1b70edbdef4ab934d0091a92c14327dde8a256.tar.gz |
feat: save logs to database
-rw-r--r-- | src/services/events/event.model.js | 48 | ||||
-rw-r--r-- | src/services/events/event.schema.js | 1 | ||||
-rw-r--r-- | src/services/events/event.service.js | 27 | ||||
-rw-r--r-- | src/services/events/log.model.js | 7 | ||||
-rw-r--r-- | src/services/events/log.schema.js | 7 |
5 files changed, 59 insertions, 31 deletions
diff --git a/src/services/events/event.model.js b/src/services/events/event.model.js index b8a13bc..9ecacb7 100644 --- a/src/services/events/event.model.js +++ b/src/services/events/event.model.js @@ -1,20 +1,57 @@ const cron = require('cron'); const { model } = require('mongoose'); const schema = require('./event.schema.js'); +const LogModel = require('./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.run = async function(handler) { + try { + this.start(); + await handler(this); + return this.complete(); + } catch (error) { + this.fail(error); + } +}; + schema.methods.computeNextRunAt = function() { const job = new CronJob(this.schedule); const nextRunAt = job.nextDates(); return new Date(nextRunAt); }; -schema.methods.setStatus = function(status) { - this.status = status;; - this.save(); -}; - schema.pre('save', function(next) { this.nextRunAt = this.computeNextRunAt(); next(); @@ -33,7 +70,6 @@ schema.statics.findNextEvents = function(limit = 10) { return this.find( { nextRunAt: { - $exists: 1, $gt: new Date() }, }, diff --git a/src/services/events/event.schema.js b/src/services/events/event.schema.js index 2250700..aff19f1 100644 --- a/src/services/events/event.schema.js +++ b/src/services/events/event.schema.js @@ -14,6 +14,7 @@ module.exports = new Schema({ type: String, default: 'notStarted' }, + error: String, context: {}, nextRunAt: Date, lastRunAt: Date diff --git a/src/services/events/event.service.js b/src/services/events/event.service.js index 4d87c6c..227480d 100644 --- a/src/services/events/event.service.js +++ b/src/services/events/event.service.js @@ -42,37 +42,14 @@ class Events extends Service { this.stopAllJobs(); const events = await this.Model.findNextEvents(); - if (!events.length) this.log('No upcoming events.'); + if (!events.length) console.log('WARNING: no upcoming events'); this.jobs = events.map(event => new CronJob( event.schedule, - () => this.run(event._id) + () => event.run(handleTestJob) )); this.startAllJobs(); } - - log(message) { - const dateOpts = { timeStyle: 'medium', dateStyle: 'short' }; - const timestamp = new Date().toLocaleString('en', dateOpts); - return console.log(`[${timestamp}] ${message}`); - } - - async run(id) { - const event = await this.Model.findById(id); - event.lastRunAt = new Date(); - event.setStatus('running') - - try { - this.log(`Event ${event.name} started`) - await handleTestJob(event); - this.log(`Event ${event.name} ended`) - } catch (error) { - event.setStatus('failed') - } - if (event.status === 'running') { - event.setStatus('complete') - } - } } diff --git a/src/services/events/log.model.js b/src/services/events/log.model.js new file mode 100644 index 0000000..64b7da6 --- /dev/null +++ b/src/services/events/log.model.js @@ -0,0 +1,7 @@ +const { model } = require('mongoose'); +const schema = require('./log.schema.js'); + +const Model = model('Log', schema); + + +module.exports = Model; diff --git a/src/services/events/log.schema.js b/src/services/events/log.schema.js new file mode 100644 index 0000000..bb8d4a5 --- /dev/null +++ b/src/services/events/log.schema.js @@ -0,0 +1,7 @@ +const { Schema, Types } = require('mongoose'); + +module.exports = new Schema({ + eventId: Types.ObjectId, + message: String +}, { timestamps: true }); + |