summaryrefslogtreecommitdiff
path: root/src/services/events/event.model.js
diff options
context:
space:
mode:
authorEugene Sokolov <eug-vs@keemail.me>2020-12-04 01:33:37 +0300
committerGitHub <noreply@github.com>2020-12-04 01:33:37 +0300
commitfac35a9a0ad1628cf31380bf29c251f6c51f13f1 (patch)
tree0aa142dce4a5d8dc4e90b090007cfbba3b4fc0fb /src/services/events/event.model.js
parent778f614a8eb753529c02d71e49486846391f0a3f (diff)
parent8ef929e503b00d93c5fee4ab996aeaa9abf283d3 (diff)
downloadbsu-fantom-fac35a9a0ad1628cf31380bf29c251f6c51f13f1.tar.gz
Merge pull request #1 from eug-vs/refactor/mongo-cronjobHEADmaster
Refactor/mongo cronjob
Diffstat (limited to 'src/services/events/event.model.js')
-rw-r--r--src/services/events/event.model.js82
1 files changed, 0 insertions, 82 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;
-
-
-