diff --git a/.gitignore b/.gitignore index 4c633a5..41c866e 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ config.json .DS_Store package-lock.json static/exports -.env \ No newline at end of file +.env +report.* \ No newline at end of file diff --git a/handlers/generate.js b/handlers/generate.js index 72968ef..2939f9b 100644 --- a/handlers/generate.js +++ b/handlers/generate.js @@ -47,9 +47,9 @@ module.exports = async function(req, res) { } if (!global.browser) { - const browser = await puppeteer.launch({ args: ['--no-sandbox'] }) + await reGenerateBrowser(); } - const page = await global.browser.newPage(); + const page = await getNewPageFromBrowser(); page.setDefaultNavigationTimeout(0); if (req.body.url) { await page.goto(req.body.url); @@ -72,6 +72,26 @@ module.exports = async function(req, res) { }); } +async function getNewPageFromBrowser() { + const newPage = await global.browser.newPage().catch( (e) => { + console.log("Browser instance seems to be dead.") + }) + + if (newPage) { + return newPage; + } else { + await reGenerateBrowser(); + console.log("Created a new page instance."); + return await global.browser.newPage() + } + +} + +async function reGenerateBrowser() { + global.browser = await puppeteer.launch({ args: ['--disable-gpu', '--no-sandbox', '--disable-dev-shm-usage'] }); + console.log('Browser instance regenerated.') +} + function revisedRandId() { return Math.random().toString(36).replace(/[^a-z]+/g, '').substr(2, 10); } diff --git a/puppeteer.js b/puppeteer.js index dd369bc..861ba41 100644 --- a/puppeteer.js +++ b/puppeteer.js @@ -2,7 +2,7 @@ const puppeteer = require('puppeteer'); const config = require('./config'); const headlessState = true; -puppeteer.launch({ headless: headlessState, args: ['--no-sandbox', '--disable-dev-shm-usage'] }).then(function(browser) { +puppeteer.launch({ headless: headlessState, args: ['--disable-gpu', '--no-sandbox', '--disable-dev-shm-usage'] }).then(function(browser) { global.browser = browser; console.log('browser ready'); }); diff --git a/purge_expired.js b/purge_expired.js index c9704d8..9c4a3a5 100644 --- a/purge_expired.js +++ b/purge_expired.js @@ -28,4 +28,4 @@ function purgeExpired() { } } }); -} \ No newline at end of file +}