From 53d896bfad660aca9ceffba58de5dfd9bff2e745 Mon Sep 17 00:00:00 2001 From: Russell Dempsey <1173416+SgtPooki@users.noreply.github.com> Date: Sun, 22 Oct 2023 22:04:25 -0700 Subject: [PATCH] feat: add health-check --- Dockerfile | 3 +++ package.json | 3 ++- src/healthcheck.ts | 9 +++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 src/healthcheck.ts diff --git a/Dockerfile b/Dockerfile index 9be1437..747a3cd 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,4 +8,7 @@ WORKDIR /app COPY . . RUN npm ci --quiet RUN npm run build + +HEALTHCHECK --interval=12s --timeout=12s --start-period=10s CMD npm run healthcheck + CMD [ "npm", "start" ] diff --git a/package.json b/package.json index b392e63..067db68 100644 --- a/package.json +++ b/package.json @@ -7,7 +7,8 @@ "build": "aegir build --bundle false", "start": "node dist/src/index.js", "start:dev": "npm run build && node dist/src/index.js", - "test": "echo \"Error: no test specified\" && exit 1" + "test": "echo \"Error: no test specified\" && exit 1", + "healthcheck": "node dist/src/healthcheck.js" }, "type": "module", "repository": { diff --git a/src/healthcheck.ts b/src/healthcheck.ts new file mode 100644 index 0000000..a928043 --- /dev/null +++ b/src/healthcheck.ts @@ -0,0 +1,9 @@ +#!/usr/bin/env node +/** + * This healthcheck script is used to check if the server is running and healthy. + */ +const rootReq = await fetch(`http://localhost:${process.env.PORT ?? 8080}`, { method: 'GET' }) +const status = rootReq.status +// eslint-disable-next-line no-console +console.log(`Healthcheck status: ${status}`) +process.exit(status === 200 ? 0 : 1)