summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreug-vs <eug-vs@keemail.me>2020-12-04 00:54:07 +0300
committereug-vs <eug-vs@keemail.me>2020-12-04 00:54:07 +0300
commita79e87296271a2891594a1ab11d746fdd875c928 (patch)
tree83ce7d7400abe4bea0813c2f8762f801e5128b5b
parent778f614a8eb753529c02d71e49486846391f0a3f (diff)
downloadbsu-fantom-a79e87296271a2891594a1ab11d746fdd875c928.tar.gz
refactor: migrate to mongo-cronjob
-rw-r--r--index.js2
-rw-r--r--package-lock.json71
-rw-r--r--package.json1
-rw-r--r--src/connectDb.js6
-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.js70
-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.js16
-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
12 files changed, 99 insertions, 189 deletions
diff --git a/index.js b/index.js
index f0778fd..84ccb67 100644
--- a/index.js
+++ b/index.js
@@ -1,7 +1,7 @@
require('dotenv').config();
const app = require('./src/app.js');
-const { connectDb } = require('./src/connectDb.js');
+const connectDb = require('./src/connectDb.js');
const PORT = process.env.PORT || 3030;
diff --git a/package-lock.json b/package-lock.json
index 71eb5c5..046186f 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -124,6 +124,14 @@
"@types/node": "*"
}
},
+ "@types/bson": {
+ "version": "4.0.3",
+ "resolved": "https://registry.npmjs.org/@types/bson/-/bson-4.0.3.tgz",
+ "integrity": "sha512-mVRvYnTOZJz3ccpxhr3wgxVmSeiYinW+zlzQz3SXWaJmD1DuL05Jeq7nKw3SnbKmbleW5qrLG5vdyWe/A9sXhw==",
+ "requires": {
+ "@types/node": "*"
+ }
+ },
"@types/connect": {
"version": "3.4.33",
"resolved": "https://registry.npmjs.org/@types/connect/-/connect-3.4.33.tgz",
@@ -174,6 +182,15 @@
"resolved": "https://registry.npmjs.org/@types/mime/-/mime-2.0.3.tgz",
"integrity": "sha512-Jus9s4CDbqwocc5pOAnh8ShfrnMcPHuJYzVcSUU7lrh8Ni5HuIqX3oilL86p3dlTrk0LzHRCgA/GQ7uNCw6l2Q=="
},
+ "@types/mongodb": {
+ "version": "3.5.34",
+ "resolved": "https://registry.npmjs.org/@types/mongodb/-/mongodb-3.5.34.tgz",
+ "integrity": "sha512-73iy3+MiH+wxSM+hVA5jcW9ZTUaor2WKvM7hW+htOSgVb7E6/JBHOWaxj7rL1/vaxEBziKRr/VPecy3YAKqLuQ==",
+ "requires": {
+ "@types/bson": "*",
+ "@types/node": "*"
+ }
+ },
"@types/node": {
"version": "14.14.7",
"resolved": "https://registry.npmjs.org/@types/node/-/node-14.14.7.tgz",
@@ -1038,6 +1055,60 @@
"moment": ">= 2.9.0"
}
},
+ "mongo-cronjob": {
+ "version": "0.1.1",
+ "resolved": "https://registry.npmjs.org/mongo-cronjob/-/mongo-cronjob-0.1.1.tgz",
+ "integrity": "sha512-fZe2kH53x6TAemoBVKOg7Ez96WtiB22l3bdi/sU02Dxtoom6cIbsH58RS/QVZ013LJR5IOYLm7QYXzyky5wJwA==",
+ "requires": {
+ "bluebird": "^3.7.2",
+ "cron": "^1.8.2",
+ "mongoose": "^5.11.1"
+ },
+ "dependencies": {
+ "mongoose": {
+ "version": "5.11.1",
+ "resolved": "https://registry.npmjs.org/mongoose/-/mongoose-5.11.1.tgz",
+ "integrity": "sha512-lP1lfYVNJzoFoR/gS3T+pZtvQiKKFAmUEhzpwjzDBn7ugPyAcYjOnBLHQMSt/MeQEcWqeoTE6hdSabiINjCxNw==",
+ "requires": {
+ "@types/mongodb": "^3.5.27",
+ "bson": "^1.1.4",
+ "kareem": "2.3.1",
+ "mongodb": "3.6.3",
+ "mongoose-legacy-pluralize": "1.0.2",
+ "mpath": "0.8.0",
+ "mquery": "3.2.2",
+ "ms": "2.1.2",
+ "regexp-clone": "1.0.0",
+ "safe-buffer": "5.2.1",
+ "sift": "7.0.1",
+ "sliced": "1.0.1"
+ }
+ },
+ "mpath": {
+ "version": "0.8.0",
+ "resolved": "https://registry.npmjs.org/mpath/-/mpath-0.8.0.tgz",
+ "integrity": "sha512-slIifXzF6pBxKpPV47ScgqWfGgkpwZNy55fY/umDrgmAxjWMz/WYzYsd8cThU49kw0rLyPWTZaWrOlazaeW57Q=="
+ },
+ "safe-buffer": {
+ "version": "5.2.1",
+ "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz",
+ "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ=="
+ }
+ }
+ },
+ "mongodb": {
+ "version": "3.6.3",
+ "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-3.6.3.tgz",
+ "integrity": "sha512-rOZuR0QkodZiM+UbQE5kDsJykBqWi0CL4Ec2i1nrGrUI3KO11r6Fbxskqmq3JK2NH7aW4dcccBuUujAP0ERl5w==",
+ "requires": {
+ "bl": "^2.2.1",
+ "bson": "^1.1.4",
+ "denque": "^1.4.1",
+ "require_optional": "^1.0.1",
+ "safe-buffer": "^5.1.2",
+ "saslprep": "^1.0.0"
+ }
+ },
"mongoose": {
"version": "5.10.14",
"resolved": "https://registry.npmjs.org/mongoose/-/mongoose-5.10.14.tgz",
diff --git a/package.json b/package.json
index cbd2602..8d9e300 100644
--- a/package.json
+++ b/package.json
@@ -18,6 +18,7 @@
"dotenv": "^8.2.0",
"feathers-mongoose": "^8.3.1",
"lodash": "^4.17.20",
+ "mongo-cronjob": "^0.1.1",
"mongoose": "^5.10.14",
"puppeteer": "^5.4.1"
}
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..2a15c96 100644
--- a/src/services/events/event.service.js
+++ b/src/services/events/event.service.js
@@ -1,71 +1,15 @@
-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);
};
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..99dada1
--- /dev/null
+++ b/src/services/events/models.js
@@ -0,0 +1,16 @@
+const mongoose = require('mongoose');
+const { Client } = require('mongo-cronjob');
+const { getConnection } = require('../../connectDb.js');
+
+const contextSchema = new mongoose.Schema({
+ participants: [String],
+ attendanceId: String,
+ conferenceId: String
+});
+
+const connection = mongoose.connection;
+const client = new Client(connection, contextSchema);
+
+
+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 });
-