summaryrefslogtreecommitdiff
path: root/src/services
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
parent778f614a8eb753529c02d71e49486846391f0a3f (diff)
parent8ef929e503b00d93c5fee4ab996aeaa9abf283d3 (diff)
downloadbsu-fantom-master.tar.gz
Merge pull request #1 from eug-vs/refactor/mongo-cronjobHEADmaster
Refactor/mongo cronjob
Diffstat (limited to 'src/services')
-rw-r--r--src/services/events/event.model.js82
-rw-r--r--src/services/events/event.schema.js22
-rw-r--r--src/services/events/event.service.js71
-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.js7
-rw-r--r--src/services/index.js2
-rw-r--r--src/services/logs/log.model.js7
-rw-r--r--src/services/logs/log.schema.js7
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 });
-