Screenshotter error handling

I realized if a page throws an error, then the browser never closes, causing a memory leak.
This commit is contained in:
Lamp 2018-08-25 13:06:26 -07:00
parent c67adda7b2
commit a37fccaeba
No known key found for this signature in database
GPG Key ID: 0F1F8704BEDE369E
1 changed files with 35 additions and 22 deletions

View File

@ -1,26 +1,39 @@
global.screenshotter = { global.screenshotter = {
capture: async function () { capture: async function () {
console.log('Starting screen captures'); console.log('Starting screen captures');
var puppeteer = require('puppeteer'); try {
var browser = await puppeteer.launch({args:['--no-sandbox']}); var puppeteer = require('puppeteer');
var page = await browser.newPage(); var browser = await puppeteer.launch({ args: ['--no-sandbox'] });
await page.setViewport({width: 1440, height: 900}); var page = await browser.newPage();
await page.goto('http://www.multiplayerpiano.com/lobby'); await page.setViewport({ width: 1440, height: 900 });
await page.evaluate(function(){ document.getElementById('modal').click() }); try {
await new Promise(resolve => setTimeout(resolve, 5000)); await page.goto('http://www.multiplayerpiano.com/lobby');
var screenshot = await page.screenshot({type: 'png'}); await page.evaluate(function () { document.getElementById('modal').click() });
var filename = `Screenshot of www.multiplayerpiano.com/lobby @ ${new Date().toISOString()}.png`; await new Promise(resolve => setTimeout(resolve, 5000));
var attachment = new Discord.MessageAttachment(screenshot, filename); let screenshot = await page.screenshot({ type: 'png' });
await dClient.channels.get(config.channels.mpp_screenshot).send(attachment); let filename = `Screenshot of www.multiplayerpiano.com/lobby @ ${new Date().toISOString()}.png`;
await page.goto('http://ourworldofpixels.com'); let attachment = new Discord.MessageAttachment(screenshot, filename);
await page.evaluate(function(){OWOP.camera.zoom = 1;}); await dClient.channels.get(config.channels.mpp_screenshot).send(attachment);
await new Promise(resolve => setTimeout(resolve, 5000)); } catch (error) {
var screenshot = await page.screenshot({type: 'png'}); await dClient.channels.get(config.channels.mpp_screenshot).send(`:warning: ${error.stack}`);
var filename = `Screenshot of ourworldofpixels.com/main @ ${new Date().toISOString()}.png`; }
var attachment = new Discord.MessageAttachment(screenshot, filename); try {
await dClient.channels.get(config.channels.owop_screenshot).send(attachment); await page.goto('http://ourworldofpixels.com');
await browser.close(); await page.evaluate(function () { OWOP.camera.zoom = 1; });
console.log('Finished screen captures'); await new Promise(resolve => setTimeout(resolve, 5000));
let screenshot = await page.screenshot({ type: 'png' });
let filename = `Screenshot of ourworldofpixels.com/main @ ${new Date().toISOString()}.png`;
let attachment = new Discord.MessageAttachment(screenshot, filename);
await dClient.channels.get(config.channels.owop_screenshot).send(attachment);
} catch (error) {
await dClient.channels.get(config.channels.owop_screenshot).send(attachment);
}
} catch(error) {
console.error(`Error occured with screen capture:\n${error.stack}`)
} finally {
await browser.close();
console.log('Finished screen captures');
}
}, },
interval: setInterval(()=>{screenshotter.capture();}, 1000*60*60) interval: setInterval(() => { screenshotter.capture(); }, 1000 * 60 * 60)
}; };