From a4898bc197ec51d0c82cd011b0cd2680973fc2c6 Mon Sep 17 00:00:00 2001 From: rickkwa Date: Sun, 21 May 2023 02:25:27 -0400 Subject: [PATCH] Add SIGTERM handler --- src/backend/server.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/backend/server.ts b/src/backend/server.ts index 078b4b6b..93597a18 100644 --- a/src/backend/server.ts +++ b/src/backend/server.ts @@ -131,6 +131,15 @@ export class Server { this.server.listen(Config.Server.port, Config.Server.host); this.server.on('error', this.onError); this.server.on('listening', this.onListening); + this.server.on('close', this.onClose); + + // Sigterm handler + process.on('SIGTERM', () => { + Logger.info(LOG_TAG, 'SIGTERM signal received'); + this.server.close(() => { + process.exit(0); + }); + }); this.onStarted.trigger(); } @@ -170,6 +179,13 @@ export class Server { typeof addr === 'string' ? 'pipe ' + addr : 'port ' + addr.port; Logger.info(LOG_TAG, 'Listening on ' + bind); }; + + /** + * Event listener for HTTP server "close" event. + */ + private onClose = () => { + Logger.info(LOG_TAG, 'Closed http server'); + }; }