From 9a8f7b9ac5373f3c53d116d6cd6371e3a0eaff43 Mon Sep 17 00:00:00 2001 From: James Magahern Date: Sat, 15 Feb 2025 16:45:59 -0800 Subject: [PATCH] backend: graceful shutdown in prod --- src/server.ts | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/src/server.ts b/src/server.ts index 2a1e563..b1aed09 100644 --- a/src/server.ts +++ b/src/server.ts @@ -106,6 +106,27 @@ app.get("*", (req, res) => { res.sendFile("dist/frontend/index.html", { root: "." }); }); -app.listen(3000, () => { - console.log("Server is running on port 3000"); -}); \ No newline at end of file +const port = process.env.PORT || 3000; +const server = app.listen(port, () => { + console.log(`Server is running on port ${port}`); +}); + +// Add graceful shutdown handling +const shutdown = async () => { + console.log('Received shutdown signal. Closing server...'); + + server.close(() => { + console.log('Server closed'); + process.exit(0); + }); + + // Force termination after some timeout (10sec) + setTimeout(() => { + console.log('Forcing server shutdown'); + process.exit(1); + }, 10000); +}; + +// Handle various shutdown signals +process.on('SIGTERM', shutdown); +process.on('SIGINT', shutdown); \ No newline at end of file