From 16aead862e90a0ceecdc79ed55b95c0bf0ad66b3 Mon Sep 17 00:00:00 2001 From: EarthlingDavey <15802017+EarthlingDavey@users.noreply.github.com> Date: Thu, 5 Dec 2024 12:07:08 +0000 Subject: [PATCH] Install pm2 for better handling of uncaught runtime errors (#24) --- Dockerfile | 5 ++++- conf/entrypoint/start-node-prod.sh | 4 ++-- conf/node/constants.js | 1 + conf/node/controllers/main.js | 9 +++++++-- 4 files changed, 14 insertions(+), 5 deletions(-) diff --git a/Dockerfile b/Dockerfile index 942a45a..7212c50 100644 --- a/Dockerfile +++ b/Dockerfile @@ -96,8 +96,11 @@ RUN chmod -R +x /docker-entrypoint.d/start-node-prod.sh # Set the environment to production for install & runtime. ENV NODE_ENV=production +# Install pm2 for better handling of uncaught runtime errors. +RUN npm install pm2 -g + # Install the node modules. -RUN npm ci --only=prod +RUN npm ci --only=prod # Remove the npm package manager. RUN apt remove -y curl npm unzip diff --git a/conf/entrypoint/start-node-prod.sh b/conf/entrypoint/start-node-prod.sh index 6d7f3ea..2a7351c 100644 --- a/conf/entrypoint/start-node-prod.sh +++ b/conf/entrypoint/start-node-prod.sh @@ -1,4 +1,4 @@ #!/bin/bash -nohup node /usr/local/bin/node/server.js & - +# pm2 will restart the node process on unhandled errors, `--no-daemon` will attach to application log +pm2 start /usr/local/bin/node/server.js --no-daemon & diff --git a/conf/node/constants.js b/conf/node/constants.js index 5936230..be86487 100644 --- a/conf/node/constants.js +++ b/conf/node/constants.js @@ -53,6 +53,7 @@ export const allowedTargetAgencies = export const sensitiveFiles = [ "cookies.txt", // Contains the JWT and CloudFront cookies. + "hts-in_progress.lock", // Contains the JWT. "hts-log.txt", // Has the httrack command line arguments - this includes the JWT. `hts-cache/doit.log`, // Has the httrack command line arguments - this includes the JWT. `hts-cache/new.zip`, diff --git a/conf/node/controllers/main.js b/conf/node/controllers/main.js index 8c03a6b..bbe1f3d 100644 --- a/conf/node/controllers/main.js +++ b/conf/node/controllers/main.js @@ -30,11 +30,16 @@ export const main = async ({ url, agency, depth }) => { runHttrack(httrackArgs); - await waitForHttrackComplete(paths.fs); + const { timedOut } = await waitForHttrackComplete(paths.fs); + + if (timedOut) { + console.error("Httrack timed out", { url: url.href, agency, depth }); + return; + } // Remove sensitive files - before syncing to S3 await Promise.all( - sensitiveFiles.map(file => fs.rm(`${paths.fs}/${file}`, { force: true })) + sensitiveFiles.map((file) => fs.rm(`${paths.fs}/${file}`, { force: true })), ); // Sync the snapshot to S3