summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreug-vs <eug-vs@keemail.me>2020-11-14 21:27:59 +0300
committereug-vs <eug-vs@keemail.me>2020-11-14 21:35:22 +0300
commit25633772f607a61cbb4a18f34c523d007dbc2c6b (patch)
treebb50ff4394778beff87e4e6d6ade03f455e8483a
parent61b16a068ac2bb4d8576d9a8aeb0f3f572587eed (diff)
downloadbsu-fantom-25633772f607a61cbb4a18f34c523d007dbc2c6b.tar.gz
feat: use multiple contexts of one browser
-rw-r--r--src/handlers/index.js26
-rw-r--r--src/handlers/launchUserSession.js32
-rw-r--r--src/services/events/event.service.js2
3 files changed, 33 insertions, 27 deletions
diff --git a/src/handlers/index.js b/src/handlers/index.js
index 196c3db..7101250 100644
--- a/src/handlers/index.js
+++ b/src/handlers/index.js
@@ -1,28 +1,30 @@
const { Types } = require('mongoose');
+const puppeteer = require('puppeteer');
const Bluebird = require('bluebird');
const UserModel = require('../services/users/user.model.js');
const launchUserSession = require('./launchUserSession.js');
const attendConference = require('./attendConference.js');
const { clickElementBySelector } = require('./utils.js');
-const { EDUFPMI_URL, NODE_ENV, HEADLESS } = process.env;
+const { EDUFPMI_URL, NODE_ENV, HEADFUL } = process.env;
// Always run headless in production, but allow configuring for development
-const headless = NODE_ENV === 'production' || HEADLESS;
+const headless = NODE_ENV === 'production' || !HEADFUL;
-const handleJobAsUser = async (job, user) => {
+const handleJobAsUser = async (job, browser, user) => {
console.log(`Running job as ${user.username}`);
+ const browserContext = await browser.createIncognitoBrowserContext();
+
const { conferenceId } = job.attrs.data;
const conferenceUrl = `${EDUFPMI_URL}/mod/bigbluebuttonbn/view.php?id=${conferenceId}`;
- const browser = await launchUserSession(user, headless);
- const page = await browser.newPage();
+ const page = await launchUserSession(user, browserContext);
await page.goto(conferenceUrl);
// Launch the conference in a new tab
- const conferencePagePromise = new Promise(resolve => browser.on(
+ const conferencePagePromise = new Promise(resolve => browserContext.on(
'targetcreated',
target => resolve(target.page())
));
@@ -31,7 +33,7 @@ const handleJobAsUser = async (job, user) => {
await attendConference(conferencePage, () => console.log(`Joined the conference at ${conferenceUrl}`));
- await browser.close();
+ await browserContext.close();
};
const handleJob = async job => {
@@ -45,7 +47,15 @@ const handleJob = async job => {
console.log('Participants: ', participants.map(participant => participant.username));
- return Bluebird.map(participants, participant => handleJobAsUser(job, participant));
+ const browser = await puppeteer.launch({ headless, args: ['--no-sandbox'] });
+
+ try {
+ await Bluebird.map(participants, participant => handleJobAsUser(job, browser, participant));
+ } catch (err) {
+ console.error(err);
+ } finally {
+ await browser.close();
+ }
};
diff --git a/src/handlers/launchUserSession.js b/src/handlers/launchUserSession.js
index 44f2193..5e490e0 100644
--- a/src/handlers/launchUserSession.js
+++ b/src/handlers/launchUserSession.js
@@ -1,27 +1,23 @@
-const puppeteer = require('puppeteer');
const { clickElementBySelector } = require('./utils.js');
const { EDUFPMI_URL } = process.env;
-const launchUserSession = (user, headless = true) => puppeteer.launch({
- headless,
- args: ['--no-sandbox']
-}).then(async browser => {
- const { username, password } = user;
- const page = await browser.newPage();
+const launchUserSession = async (user, 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}`);
+ // 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;
- });
+ return page;
+};
module.exports = launchUserSession;
diff --git a/src/services/events/event.service.js b/src/services/events/event.service.js
index 267016f..112990a 100644
--- a/src/services/events/event.service.js
+++ b/src/services/events/event.service.js
@@ -22,7 +22,7 @@ class Events {
// Logs
this.agenda.on('start', job => console.log(`Starting ${job.attrs.name} job`));
this.agenda.on('complete', job => console.log(`Job ${job.attrs.name} finished`));
- this.agenda.on('fail', (job, err) => console.log(`Job failed with the error ${err}`));
+ this.agenda.on('fail', (err, job) => console.log(`Job failed with the error ${err}`));
return this.agenda.start();
};