summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreug-vs <eug-vs@keemail.me>2020-11-28 01:21:41 +0300
committereug-vs <eug-vs@keemail.me>2020-11-28 01:31:23 +0300
commitc3b76d1b5d0f85deb9a52dbe516c665ab4de8c07 (patch)
treecfb7d6fae788ca16260199f6d52d40e45920f94b
parent2a1b70edbdef4ab934d0091a92c14327dde8a256 (diff)
downloadbsu-fantom-c3b76d1b5d0f85deb9a52dbe516c665ab4de8c07.tar.gz
fix: re-fetch event before running
-rw-r--r--src/handlers/index.js6
-rw-r--r--src/services/events/event.model.js10
-rw-r--r--src/services/events/event.service.js14
3 files changed, 16 insertions, 14 deletions
diff --git a/src/handlers/index.js b/src/handlers/index.js
index 2234b05..4e7ee83 100644
--- a/src/handlers/index.js
+++ b/src/handlers/index.js
@@ -13,7 +13,7 @@ const headless = NODE_ENV === 'production' || !HEADFUL;
const handleEventAsUser = async (event, browser, user) => {
- console.log(`Running event as ${user.username}`);
+ event.log(`Running event as ${user.username}`);
const browserContext = await browser.createIncognitoBrowserContext();
@@ -32,7 +32,7 @@ const handleEventAsUser = async (event, browser, user) => {
const conferencePage = await conferencePagePromise;
await page.close();
- await attendConference(conferencePage, () => console.log(`Joined the conference at ${conferenceUrl}`));
+ await attendConference(conferencePage, () => event.log(`Joined the conference at ${conferenceUrl}`));
await browserContext.close();
};
@@ -44,7 +44,7 @@ const handleEvent = async event => {
}
});
- console.log('Participants: ', participants.map(participant => participant.username));
+ event.log('Participants: ', participants.map(participant => participant.username));
const browser = await puppeteer.launch({ headless, args: ['--no-sandbox', '--incognito'] });
diff --git a/src/services/events/event.model.js b/src/services/events/event.model.js
index 9ecacb7..56ba3c9 100644
--- a/src/services/events/event.model.js
+++ b/src/services/events/event.model.js
@@ -36,16 +36,6 @@ schema.methods.fail = function(error) {
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();
diff --git a/src/services/events/event.service.js b/src/services/events/event.service.js
index 227480d..f2185a6 100644
--- a/src/services/events/event.service.js
+++ b/src/services/events/event.service.js
@@ -45,11 +45,23 @@ class Events extends Service {
if (!events.length) console.log('WARNING: no upcoming events');
this.jobs = events.map(event => new CronJob(
event.schedule,
- () => event.run(handleTestJob)
+ () => 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);
+ }
+ }
}