Skip to content

Commit

Permalink
Add retry logic when launching browser in NPM driver
Browse files Browse the repository at this point in the history
  • Loading branch information
AliasIO committed Aug 6, 2023
1 parent d16838e commit 9dfa108
Showing 1 changed file with 38 additions and 31 deletions.
69 changes: 38 additions & 31 deletions src/drivers/npm/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -369,40 +369,47 @@ class Driver {
}

async init() {
this.log('Launching browser...')
for (let attempt = 1; attempt <= 3; attempt++) {
this.log(`Launching browser (attempt ${attempt})...`)

try {
if (CHROMIUM_WEBSOCKET) {
this.browser = await puppeteer.connect({
ignoreHTTPSErrors: true,
acceptInsecureCerts: true,
browserWSEndpoint: CHROMIUM_WEBSOCKET,
})
} else {
this.browser = await puppeteer.launch({
ignoreHTTPSErrors: true,
acceptInsecureCerts: true,
args: chromiumArgs,
executablePath: CHROMIUM_BIN,
})
}
try {
if (CHROMIUM_WEBSOCKET) {
this.browser = await puppeteer.connect({
ignoreHTTPSErrors: true,
acceptInsecureCerts: true,
browserWSEndpoint: CHROMIUM_WEBSOCKET,
})
} else {
this.browser = await puppeteer.launch({
ignoreHTTPSErrors: true,
acceptInsecureCerts: true,
args: chromiumArgs,
executablePath: CHROMIUM_BIN,
timeout: 5000,
})
}

this.browser.on('disconnected', async () => {
this.log('Browser disconnected')
break
} catch (error) {
this.log(error)

if (!this.destroyed) {
try {
await this.init()
} catch (error) {
this.log(error)
}
if (attempt >= 3) {
throw new Error(error.message || error.toString())
}
})
} catch (error) {
this.log(error)

throw new Error(error.message || error.toString())
}
}

this.browser.on('disconnected', async () => {
this.log('Browser disconnected')

if (!this.destroyed) {
try {
await this.init()
} catch (error) {
this.log(error)
}
}
})
}

async destroy() {
Expand Down Expand Up @@ -537,7 +544,7 @@ class Site {
promise,
fallback,
errorMessage = 'Operation took too long to complete',
maxWait = Math.min(this.options.maxWait, 1000)
maxWait = Math.min(this.options.maxWait, 3000)
) {
let timeout = null

Expand Down Expand Up @@ -1260,7 +1267,7 @@ class Site {

const body = await get(new URL(path, url.href), {
userAgent: this.options.userAgent,
timeout: Math.min(this.options.maxWait, 1000),
timeout: Math.min(this.options.maxWait, 3000),
})

this.log(`Probe ok (${path})`)
Expand Down

0 comments on commit 9dfa108

Please sign in to comment.