summaryrefslogtreecommitdiff
path: root/src/services/events/event.service.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/services/events/event.service.js')
-rw-r--r--src/services/events/event.service.js74
1 files changed, 46 insertions, 28 deletions
diff --git a/src/services/events/event.service.js b/src/services/events/event.service.js
index 6c0454e..4d87c6c 100644
--- a/src/services/events/event.service.js
+++ b/src/services/events/event.service.js
@@ -2,28 +2,53 @@ 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 handleAttendClassJob = require('../../handlers');
const CronJob = cron.CronJob;
+const handleTestJob = () => new Promise(res => setTimeout(res, 10000));
+
+
class Events extends Service {
async setup(app, path) {
- await this.Model.rescheduleOldEvents();
- const fetchNextEvent = () => this.fetchNextEvent();
- this.fetchEventJob = new CronJob('*/10 * * * * *', fetchNextEvent);
- this.fetchEventJob.start()
+ 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 fetchNextEvent() {
- const event = await this.Model.findNextEvent();
+ 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();
- if (event) {
- this.log(`Found an upcoming event: ${event.name}`);
- if (this.nextJob) this.nextJob.stop();
- this.nextJob = new CronJob(event.schedule, () => this.process(event._id))
- this.nextJob.start()
- } else this.log('No upcoming events.');
+ this.stopAllJobs();
+
+ const events = await this.Model.findNextEvents();
+ if (!events.length) this.log('No upcoming events.');
+ this.jobs = events.map(event => new CronJob(
+ event.schedule,
+ () => this.run(event._id)
+ ));
+
+ this.startAllJobs();
}
log(message) {
@@ -32,27 +57,20 @@ class Events extends Service {
return console.log(`[${timestamp}] ${message}`);
}
- async process(id) {
+ async run(id) {
const event = await this.Model.findById(id);
- this.log(`Running event ${event.name}`);
- event.status = 'running';
- await event.save();
- await this.fetchNextEvent();
+ event.lastRunAt = new Date();
+ event.setStatus('running')
+
try {
- // await handleAttendClassJob(event);
- await new Promise(res => {
- console.log('Job started')
- setTimeout(() => {
- console.log('Job ended')
- res();
- }, 10000)
- });
+ this.log(`Event ${event.name} started`)
+ await handleTestJob(event);
+ this.log(`Event ${event.name} ended`)
} catch (error) {
- event.status = 'failed';
+ event.setStatus('failed')
}
if (event.status === 'running') {
- event.status = 'complete';
- event.save();
+ event.setStatus('complete')
}
}
}