Skip to content

fix(widget): add docker container healthcheck for widget web #7953

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

Draft
wants to merge 7 commits into
base: next
Choose a base branch
from
1 change: 1 addition & 0 deletions apps/web/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ USER 1000
WORKDIR /app

COPY --chown=1000:1000 --from=builder /usr/src/app/apps/web/env.sh /app/env.sh
COPY --chown=1000:1000 --from=builder /usr/src/app/apps/web/healthchecks/web-healthcheck.mjs /app/healthchecks/web-healthcheck.mjs

COPY --chown=1000:1000 --from=builder /usr/src/app/apps/web/build /app/build
COPY --chown=1000:1000 --from=builder /usr/src/app/apps/web/.env.sample /app/.env.sample
Expand Down
45 changes: 45 additions & 0 deletions apps/web/healthchecks/web-healthcheck.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* WARN:
* Ensure we always return exit code 0 or exit code 1 as these are the defaults docker healthcheck can currently handle
*/

/**
* NOTE: This should be the port used inside the container.
* Pass in the port to the script via argv or use default
*/
const port = process.argv[2] ?? 4200;

// Assemble Webserver Url
const webServerUrl = `http://127.0.0.1:${port}`;

/**
* Healthcheck for the `web` docker container
*
* Runs a fetch request and evaluats the response code
*/
async function webHealthcheck() {
try {
const response = await fetch(webServerUrl);

if (response.status === 200) {
const successMessage = `Healthcheck passed for 'web' container at ${webServerUrl}`;

process.stdout.write(successMessage);

process.exit(0);
}

process.stderr.write(`Unexpected response status: ${response.status}`);

process.exit(1);
} catch (error) {
const errorMessage = `Healthcheck failed for 'web' container at ${webServerUrl}`;

process.stdout.write(errorMessage + error);

process.exit(1);
}
}

// Run Healthcheck
webHealthcheck();
28 changes: 27 additions & 1 deletion docker/community/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ services:
options:
max-size: '50m'
max-file: '5'
healthcheck:
test: ['CMD', 'redis-cli', 'ping']
interval: 30s
timeout: 5s
retries: 3
start_period: 10s

mongodb:
image: mongo:8.0.3
Expand All @@ -28,6 +34,16 @@ services:
- mongodb:/data/db
ports:
- 27017:27017
healthcheck:
test:
[
'CMD-SHELL',
"echo 'db.runCommand({ ping: 1 }).ok' | mongosh mongodb://${MONGO_INITDB_ROOT_USERNAME}:${MONGO_INITDB_ROOT_PASSWORD}@127.0.0.1:27017/admin --quiet",
]
interval: 30s
timeout: 10s
retries: 5
start_period: 60s

api:
image: 'ghcr.io/novuhq/novu/api:2.1.1'
Expand Down Expand Up @@ -158,7 +174,17 @@ services:
REACT_APP_WS_URL: ${REACT_APP_WS_URL}
ports:
- 4200:4200
command: ['/bin/sh', '-c', 'pnpm run envsetup:docker && pnpm run start:static:build']
command:
[
'/bin/sh',
'-c',
'pnpm run envsetup:docker && pnpm run start:static:build',
]
healthcheck:
test: [CMD-SHELL, 'node ./healthchecks/web-healthcheck.mjs']
interval: 5s
timeout: 5s
retries: 3

volumes:
mongodb: ~
Loading