summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreug-vs <eug-vs@keemail.me>2020-11-14 17:21:21 +0300
committereug-vs <eug-vs@keemail.me>2020-11-14 17:21:21 +0300
commitae94313667b3abe257c0767439c6d3986ba0fde6 (patch)
tree212e5082cda719c1d26625b251d60452aea275ab
parent4f0561acef93970f69b0ddda1cea132532355e7d (diff)
downloadbsu-fantom-ae94313667b3abe257c0767439c6d3986ba0fde6.tar.gz
feat: launch session on schedule for many users
-rw-r--r--src/handlers/index.js80
-rw-r--r--src/handlers/launchUserSession.js25
-rw-r--r--src/services/events/event.service.js14
3 files changed, 58 insertions, 61 deletions
diff --git a/src/handlers/index.js b/src/handlers/index.js
index c50ef23..188547c 100644
--- a/src/handlers/index.js
+++ b/src/handlers/index.js
@@ -1,53 +1,29 @@
-const puppeteer = require('puppeteer');
-const attendConference = require('./attendConference.js');
-const { clickElementBySelector } = require('./utils.js');
-
-const {
- EDUFPMI_URL,
- EDUFPMI_USERNAME,
- EDUFPMI_PASSWORD,
- COURSE_URL
-} = process.env;
-
-puppeteer.launch({ headless: true })
- .then(async browser => {
- const page = await browser.newPage();
-
- // Login
- await page.goto(EDUFPMI_URL, { waitUntil: 'domcontentloaded' });
- await page.type('input#username', EDUFPMI_USERNAME);
- await page.type('input#password', EDUFPMI_PASSWORD);
- await Promise.all([
- clickElementBySelector(page, 'button#loginbtn'),
- page.waitForNavigation()
- ]);
- console.log(`Logged in as ${EDUFPMI_USERNAME}`);
-
- // Find the course
- await page.goto(COURSE_URL);
- await Promise.all([
- clickElementBySelector(page, 'li.bigbluebuttonbn a'),
- page.waitForNavigation() // Wait until the next page loads
- ]);
-
- // Launch the conference in a new tab
- const conferencePagePromise = new Promise(resolve => browser.on(
- 'targetcreated',
- target => resolve(target.page())
- ));
- await clickElementBySelector(page, 'input#join_button_input');
- const conferencePage = await conferencePagePromise;
-
- // Prepare onJoin callback
- const fillAttendance = async () => {
- await page.goBack();
- await clickElementBySelector(page, 'li.attendance a');
- // TODO: actually fill the attendance
- console.log('Attendance filled');
- };
-
- await attendConference(conferencePage, fillAttendance);
- await browser.close();
- })
- .catch(e => console.log(e));
+const { Types } = require('mongoose');
+const Bluebird = require('bluebird');
+const UserModel = require('../services/users/user.model.js');
+const launchUserSession = require('./launchUserSession.js');
+
+
+const handleJobAsUser = async (job, user) => {
+ console.log(`Running job as ${user.username}`);
+ return launchUserSession(user);
+};
+
+const handleJob = async job => {
+ console.log('Running attend class job');
+ const { data } = job.attrs;
+
+ const participants = await UserModel.find({
+ _id: {
+ $in: data.participantIds.map(Types.ObjectId)
+ }
+ });
+
+ console.log({ participants })
+
+ return Bluebird.map(participants, participant => handleJobAsUser(job, participant));
+};
+
+
+module.exports = handleJob;
diff --git a/src/handlers/launchUserSession.js b/src/handlers/launchUserSession.js
new file mode 100644
index 0000000..58dde62
--- /dev/null
+++ b/src/handlers/launchUserSession.js
@@ -0,0 +1,25 @@
+const puppeteer = require('puppeteer');
+const { clickElementBySelector } = require('./utils.js');
+
+const { EDUFPMI_URL } = process.env;
+
+const launchUserSession = (user, headless = true) => puppeteer.launch({ headless })
+ .then(async browser => {
+ const { username, password } = user;
+ const page = await browser.newPage();
+
+ // Login
+ await page.goto(EDUFPMI_URL, { waitUntil: 'domcontentloaded' });
+ await page.type('input#username', username);
+ await page.type('input#password', password);
+ await Promise.all([
+ clickElementBySelector(page, 'button#loginbtn'),
+ page.waitForNavigation()
+ ]);
+ console.log(`Logged in as ${username}`);
+
+ return browser;
+ });
+
+module.exports = launchUserSession;
+
diff --git a/src/services/events/event.service.js b/src/services/events/event.service.js
index 5c5bcff..accd45d 100644
--- a/src/services/events/event.service.js
+++ b/src/services/events/event.service.js
@@ -1,11 +1,13 @@
const Agenda = require('agenda');
const { getConnection } = require('../../connectDb.js');
+const handleAttendClassJob = require('../../handlers');
class Events {
setup(app) {
this.collectionName = 'events';
+ // Reuse mongoose connection
const connection = getConnection();
this.agenda = new Agenda();
this.agenda.mongo(
@@ -13,20 +15,14 @@ class Events {
this.collectionName
);
- this.defineJobs();
+ // Define jobs
+ this.agenda.define('attend class', handleAttendClassJob);
return this.agenda.start();
};
- defineJobs() {
- this.agenda.define('attend class', async job => {
- console.log('Running attend class job with attrs:');
- console.log(job.attrs);
- });
- };
-
async create(data, params) {
- return this.agenda.schedule(data.date, 'attend class', { data });
+ return this.agenda.schedule(data.date, 'attend class', data);
};
}