Skip to content

Commit cd084eb

Browse files
authored
fix: ensure graceful shutdown and container stop when stopApp is called (hoppscotch#5494)
* fix: graceful shutdown handling for backend app * chore: add startup delay to health check script
1 parent 6064186 commit cd084eb

File tree

4 files changed

+17
-5
lines changed

4 files changed

+17
-5
lines changed

healthcheck.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,14 @@ curlCheck() {
88
fi
99
}
1010

11+
# Wait for initial startup period to avoid unnecessary error logs
12+
# Check if the container has been running for at least 15 seconds
13+
UPTIME=$(awk '{print int($1)}' /proc/uptime)
14+
if [ "$UPTIME" -lt 15 ]; then
15+
echo "Container still starting up (uptime: ${UPTIME}s), skipping health check..."
16+
exit 0
17+
fi
18+
1119
if [ "$ENABLE_SUBPATH_BASED_ACCESS" = "true" ]; then
1220
curlCheck "http://localhost:${HOPP_AIO_ALTERNATE_PORT:-80}/backend/ping" || exit 1
1321
else

packages/hoppscotch-backend/src/infra-config/helper.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -370,14 +370,16 @@ export async function isInfraConfigTablePopulated(): Promise<boolean> {
370370
}
371371

372372
/**
373-
* Stop the app after 5 seconds
374-
* (Docker will re-start the app)
373+
* Stop the app after 5 seconds with graceful shutdown
374+
* (Sends SIGTERM to trigger NestJS graceful shutdown, then Docker container stops)
375375
*/
376376
export function stopApp() {
377377
console.log('Stopping app in 5 seconds...');
378378

379379
setTimeout(() => {
380-
console.log('Stopping app now...');
380+
console.log('Stopping app now with graceful shutdown...');
381+
// Send SIGTERM to the current process to trigger graceful shutdown
382+
// This will call app.close() which triggers onModuleDestroy lifecycle hooks
381383
process.kill(process.pid, 'SIGTERM');
382384
}, 5000);
383385
}

packages/hoppscotch-backend/src/main.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,10 @@ async function bootstrap() {
9999

100100
// Graceful shutdown
101101
process.on('SIGTERM', async () => {
102-
console.info('SIGTERM signal received');
102+
console.info('SIGTERM signal received, initiating graceful shutdown...');
103103
await app.close();
104+
console.info('Application closed successfully');
105+
process.exit(0);
104106
});
105107
}
106108

prod.Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ COPY aio-subpath-access.Caddyfile /etc/caddy/aio-subpath-access.Caddyfile
183183

184184
ENTRYPOINT [ "tini", "--" ]
185185
COPY --chmod=755 healthcheck.sh /
186-
HEALTHCHECK --interval=2s CMD /bin/sh /healthcheck.sh
186+
HEALTHCHECK --interval=2s --start-period=15s CMD /bin/sh /healthcheck.sh
187187

188188
WORKDIR /dist/backend
189189
CMD ["node", "/usr/src/app/aio_run.mjs"]

0 commit comments

Comments
 (0)