Skip to content

Commit

Permalink
Merge pull request #516 from bcgov/fix-puppeteer-defunct-chrome-subpr…
Browse files Browse the repository at this point in the history
…ocesses

Fix puppeteer defunct chrome subprocesses
  • Loading branch information
trev-dev authored Jul 25, 2024
2 parents d5ad854 + b6cf5f5 commit 4c09730
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
6 changes: 4 additions & 2 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ RUN npm ci
COPY . /opt/app-root/src

RUN apk upgrade --no-cache --available \
&& apk add --no-cache chromium-swiftshader nss freetype harfbuzz ca-certificates ttf-freefont ghostscript
&& apk add --no-cache chromium-swiftshader nss freetype \
harfbuzz ca-certificates ttf-freefont tini ghostscript

ENV CHROME_BIN=/usr/bin/chromium-browser \
CHROME_PATH=/usr/lib/chromium/ \
Expand All @@ -25,4 +26,5 @@ RUN mkdir -p /tmp/npm \
&& chmod -R 777 /tmp/npm

EXPOSE 443 8080
CMD ["npm", "start"]
ENTRYPOINT ["/sbin/tini", "--"]
CMD ["node", "--max-old-space-size=450", "./src/server.js"]
12 changes: 9 additions & 3 deletions backend/src/components/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -450,12 +450,18 @@ async function printPdf(req, numOfRetries = 0) {
]
}); //to debug locally add {headless: false, devtools: true} in options <-make sure they are boolean and not string

const browserProcess = browser.process();
browserProcess
.on('exit', code => log.info(`printPdf :: browser process exited, status: ${code}`));
browserProcess
.on('close', code => log.info(`printPdf :: browser process closed, status: ${code}`));

try {
log.info('printPdf :: starting new page');
const page = await browser.newPage();

page.setDefaultTimeout(300000); //set navigation timeouts to 5 mins. So large organizations waiting to load do not throw error.
await page.setRequestInterception(true);
await page.setDefaultTimeout(300000); //set navigation timeouts to 5 mins. So large organizations waiting to load do not throw error.

page.on('request', (request) => {
const headers = request.headers();
Expand All @@ -471,7 +477,6 @@ async function printPdf(req, numOfRetries = 0) {
log.info('printPdf :: pdf buffer created starting compression');
const compressedPdfBuffer = await compress(pdfBuffer, {gsModulePath: process.env.GHOSTSCRIPT_PATH}); //this is set in dockerfile to fix ghostscript error on deploy
log.info('printPdf :: compression completed for applicationId', req.params.applicationId);
await browser.close();

let payload;
//if the body contains an application ID, the summary dec is for PCF. Else, it should be a change request.
Expand Down Expand Up @@ -502,7 +507,6 @@ async function printPdf(req, numOfRetries = 0) {
return payload;
} catch (e) {
log.error(e);
await browser.close();

if (numOfRetries >= 3) {
log.info('printPdf :: maximum number of retries reached');
Expand All @@ -514,6 +518,8 @@ async function printPdf(req, numOfRetries = 0) {
log.info(`printPdf :: retry count ${retryCount}`);
await printPdf(req, retryCount);
}
} finally {
await browser.close();
}
}

Expand Down

0 comments on commit 4c09730

Please sign in to comment.