Skip to content

Commit

Permalink
Increase connect timeout, exit if browser failed to launch (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxsnq authored Mar 21, 2023
1 parent 92d90d8 commit 65fde94
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
23 changes: 15 additions & 8 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,25 @@ const closeContextRouter = require('./routes/close_context');
const app = express();

const HEADLESS = (process.env.HEADLESS || "true").toLowerCase() === "true";
const CONNECT_TIMEOUT = parseInt(process.env.CONNECT_TIMEOUT) || 180000;
const VIEWPORT_WIDTH = parseInt(process.env.VIEWPORT_WIDTH) || 1280;
const VIEWPORT_HEIGHT = parseInt(process.env.VIEWPORT_HEIGHT) || 720;

async function setupBrowser() {
//TODO add params for puppeteer launch
const browser = await puppeteer.launch(
{
headless: HEADLESS,
defaultViewport: { width: VIEWPORT_WIDTH, height: VIEWPORT_HEIGHT }
});
browser.on('disconnected', setupBrowser);
app.set('browser', browser);
try {
//TODO add more params for puppeteer launch
const browser = await puppeteer.launch(
{
headless: HEADLESS,
defaultViewport: {width: VIEWPORT_WIDTH, height: VIEWPORT_HEIGHT},
timeout: CONNECT_TIMEOUT
});
browser.on('disconnected', setupBrowser);
app.set('browser', browser);
} catch (error) {
console.error('Failed to start browser:', error);
process.exit(1);
}
}

(async () => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "scrapy-puppeteer-service",
"version": "0.0.7",
"version": "0.0.8",
"private": true,
"scripts": {
"start": "node ./bin/www"
Expand Down

0 comments on commit 65fde94

Please sign in to comment.