diff --git a/server/src/index.ts b/server/src/index.ts index 6a18126..1de0e9b 100644 --- a/server/src/index.ts +++ b/server/src/index.ts @@ -42,6 +42,25 @@ const stopPoller = startStatusPoller( app.addHook('onClose', async () => stopPoller()); +// As container PID 1, an unhandled SIGTERM may be ignored. Explicitly close +// HTTP, the status poller, relay pool and SQLite before normal termination. +let closing = false; +const shutdown = () => { + if (closing) return; + closing = true; + const deadline = setTimeout(() => process.exit(1), 10_000); + deadline.unref(); + void app.close().then(() => { + clearTimeout(deadline); + process.exit(0); + }, (error) => { + app.log.error(error); + process.exit(1); + }); +}; +process.on('SIGTERM', shutdown); +process.on('SIGINT', shutdown); + try { await app.listen({ port: config.PORT, host: config.HOST }); } catch (err) {