From 209847d98ad67118e567c767d4cd2b07c3d4427e Mon Sep 17 00:00:00 2001 From: mikiher Date: Mon, 25 Dec 2023 09:25:04 +0200 Subject: [PATCH] Add a SIGINT handler for proper server shutdown --- server/Server.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/server/Server.js b/server/Server.js index 5e8cab76..3de8ff7f 100644 --- a/server/Server.js +++ b/server/Server.js @@ -276,6 +276,19 @@ class Server { }) app.get('/healthcheck', (req, res) => res.sendStatus(200)) + let sigintAlreadyReceived = false + process.on('SIGINT', async () => { + if (!sigintAlreadyReceived) { + sigintAlreadyReceived = true + Logger.info('SIGINT (Ctrl+C) received. Shutting down...') + await this.stop() + Logger.info('Server stopped. Exiting.') + } else { + Logger.info('SIGINT (Ctrl+C) received again. Exiting immediately.') + } + process.exit(0) + }) + this.server.listen(this.Port, this.Host, () => { if (this.Host) Logger.info(`Listening on http://${this.Host}:${this.Port}`) else Logger.info(`Listening on port :${this.Port}`) @@ -383,6 +396,7 @@ class Server { } async stop() { + Logger.info('=== Stopping Server ===') await this.watcher.close() Logger.info('Watcher Closed')