diff --git a/server.js b/server.js index b870808..96ac013 100644 --- a/server.js +++ b/server.js @@ -51,6 +51,23 @@ app.get('/health/:service', async (req, res) => { app.use((_, response) => response.sendStatus(404)) -app.listen(PORT, () => { - console.log(`Health Check Proxy listening on port ${PORT}`) -}) \ No newline at end of file +const server = app.listen(PORT, () => { + console.log(`@syncraft/healthcheck listening on port ${PORT}`) +}) + +const shutdown_handler = (signal) => { + console.log(`${signal} received. Initiating graceful shutdown.`) + + server.close((error) => { + if (error) { + console.error('Error during HTTP server close:', error) + process.exit(1) + } + + console.log('HTTP server closed. Exiting process.') + process.exit(0) + }); +} + +process.on('SIGTERM', () => shutdown_handler('SIGTERM')) +process.on('SIGINT', () => shutdown_handler('SIGINT')) \ No newline at end of file