fix: add shutdown timeout and improve health endpoint

- Add 8s shutdown timeout to prevent indefinite hang when app.close()
  stalls. Stays under Docker's default 10s stop_grace_period.
- Health endpoint now checks database connectivity, returns 503 when
  DB is unreachable so Docker marks container unhealthy.
- Removed variant field from health response (single image now).
This commit is contained in:
Siddharth Kumar Sah
2026-04-10 00:22:57 +08:00
parent 84f7057a49
commit 986ad37bb5
+25 -6
View File
@@ -108,12 +108,23 @@ await teamsRoutes(app);
// API docs (Scalar)
await docsRoutes(app);
// Public health check (minimal - no internal details)
app.get("/api/v1/health", async () => ({
status: "healthy",
version: APP_VERSION,
variant: process.env.STIRLING_VARIANT === "lite" ? "lite" : "full",
}));
// Public health check (checks core dependencies)
app.get("/api/v1/health", async (_request, reply) => {
let dbOk = false;
try {
db.select().from(schema.settings).limit(1).all();
dbOk = true;
} catch {
/* db unreachable */
}
const status = dbOk ? "healthy" : "unhealthy";
const code = dbOk ? 200 : 503;
return reply.code(code).send({
status,
version: APP_VERSION,
});
});
// Admin health check (full diagnostics)
app.get("/api/v1/admin/health", async (request, reply) => {
@@ -161,12 +172,19 @@ try {
}
// Graceful shutdown
const SHUTDOWN_TIMEOUT_MS = 8000;
let shuttingDown = false;
async function shutdown(signal: string) {
if (shuttingDown) return;
shuttingDown = true;
console.log(`\n${signal} received, shutting down gracefully...`);
const forceExit = setTimeout(() => {
console.error("Shutdown timed out, forcing exit");
process.exit(1);
}, SHUTDOWN_TIMEOUT_MS);
forceExit.unref();
cleanupCron.stop();
try {
@@ -199,6 +217,7 @@ async function shutdown(signal: string) {
console.error("Error closing database:", err);
}
clearTimeout(forceExit);
process.exit(0);
}