Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix puppeteer defunct chrome subprocesses #515

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading