generated from bcgov/vue-scaffold
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #543 from bcgov/ccof-3694-puppeteer-debug
Allow simultaneous PDFs to be generated at once
- Loading branch information
Showing
7 changed files
with
146 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
const puppeteer = require('puppeteer'); | ||
const log = require('../components/logger'); | ||
|
||
/** @typedef {import('puppeteer').Browser} Browser - The puppeteer browser instance */ | ||
|
||
/** @type {Browser|null} */ | ||
let browser = null; | ||
|
||
/** | ||
* Boostrap a browser instance and return a fresh browser context. | ||
* | ||
* @returns {Promise<import('puppeteer').BrowserContext|null>} The browser instance | ||
*/ | ||
async function getBrowserContext() { | ||
try { | ||
if (browser instanceof puppeteer.Browser && browser.process() !== null) { | ||
return browser; | ||
} | ||
// To debug locally add {headless: false, devtools: true} in options | ||
// make sure they are boolean and not string | ||
log.info('Puppeteer :: getBrowserContext launching new browser process'); | ||
browser = await puppeteer.launch({ | ||
headless: true, // setting this to 'new' will crash on openshift | ||
devtools: false, | ||
dumpio: true, | ||
args: [ | ||
'--no-sandbox', | ||
'--disable-software-rasterizer', | ||
'--disable-dev-shm-usage', | ||
'--disable-gpu', | ||
], | ||
userDataDir: process.env.CHROME_DATA_DIR, | ||
}); | ||
|
||
const browserProcess = browser.process(); | ||
browserProcess | ||
.on('exit', code => log.info(`Puppeteer :: browser process exited, status: ${code}`)); | ||
browserProcess | ||
.on('close', code => log.info(`Puppeteer :: browser process closed, status: ${code}`)); | ||
|
||
return browser.createBrowserContext(); | ||
} catch (e) { | ||
log.error('Puppeteer :: Browser process could not be retrieved', e); | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* Gracefully close the browser and all of its pages/contexts. | ||
*/ | ||
async function closeBrowser() { | ||
if (browser.pages().length === 0) { | ||
await browser.close(); | ||
} else { | ||
log.warn('Puppeteer :: closeBrowser was called with pages open'); | ||
} | ||
} | ||
|
||
module.exports = { | ||
getBrowserContext, | ||
closeBrowser | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters