From 740a429790c25e1f67e4b959d558f823a1877e10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerg=C5=91=20M=C3=B3ricz?= Date: Tue, 12 Nov 2024 18:10:24 +0100 Subject: [PATCH] feat(api): graceful shutdown for less 502 errors --- apps/api/src/index.ts | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/apps/api/src/index.ts b/apps/api/src/index.ts index 7f7ec036..049b37a9 100644 --- a/apps/api/src/index.ts +++ b/apps/api/src/index.ts @@ -80,6 +80,17 @@ function startServer(port = DEFAULT_PORT) { `For the Queue UI, open: http://${HOST}:${port}/admin/${process.env.BULL_AUTH_KEY}/queues` ); }); + + const exitHandler = () => { + logger.info('SIGTERM signal received: closing HTTP server') + server.close(() => { + logger.info("Server closed."); + process.exit(0); + }); + }; + + process.on('SIGTERM', exitHandler); + process.on('SIGINT', exitHandler); return server; }