Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,43 @@ export async function bootstrap(): Promise<{ server: Server }> {
void maturityWorker.stop();
});

let shuttingDown = false;
const shutdown = async (signal: NodeJS.Signals): Promise<void> => {
if (shuttingDown) return;
shuttingDown = true;
logger.info("Received shutdown signal; draining HTTP server.", { signal });

const forceShutdown = setTimeout(() => {
logger.error("HTTP server drain timed out; forcing shutdown.", {
timeout_ms: config.http.shutdownTimeoutMs,
});
server.closeAllConnections();
}, config.http.shutdownTimeoutMs);
forceShutdown.unref();

server.close(async (error) => {
clearTimeout(forceShutdown);
if (error) {
logger.error("Failed to close HTTP server cleanly.", { error });
process.exitCode = 1;
}

try {
await maturityWorker.stop();
if (dataSource.isInitialized) await dataSource.destroy();
logger.info("Application shutdown complete.");
} catch (shutdownError) {
logger.error("Failed to close application resources.", {
error: shutdownError instanceof Error ? shutdownError.message : String(shutdownError),
});
process.exitCode = 1;
}
});
};

process.on("SIGTERM", () => void shutdown("SIGTERM"));
process.on("SIGINT", () => void shutdown("SIGINT"));

return { server };
}

Expand Down
Loading