Skip to content

Commit

Permalink
Add a SIGINT handler for proper server shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
mikiher committed Dec 25, 2023
1 parent 14f42e1 commit 209847d
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions server/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
Expand Down Expand Up @@ -383,6 +396,7 @@ class Server {
}

async stop() {
Logger.info('=== Stopping Server ===')
await this.watcher.close()
Logger.info('Watcher Closed')

Expand Down

0 comments on commit 209847d

Please sign in to comment.