Skip to content

Commit

Permalink
feat(docker): Avoid use of process.exit
Browse files Browse the repository at this point in the history
  • Loading branch information
Joxit committed Jun 8, 2019
1 parent 8606a12 commit 15fc619
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions server/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,26 @@ if (cpus > 1) {
// handle SIGTERM (required for fast docker restarts)
process.on('SIGTERM', () => {
console.error('[master] closing app')
process.exit(0)
Object.keys(cluster.workers)
.map(id => cluster.workers[id])
.forEach(worker => worker.send('gracefull-shutdown'))
})

// fork workers
for (var c = 0; c < cpus; c++) {
cluster.fork()
}
} else {
app.listen(PORT, HOST, () => {
const server = app.listen(PORT, HOST, () => {
console.error('[worker %d] listening on %s:%s', process.pid, HOST || '0.0.0.0', PORT)
})
process.on('message', (msg) => {
// handle SIGTERM (required for fast docker restarts)
if (msg === 'gracefull-shutdown') {
console.error('[worker %d] closing server', process.pid)
server.close(() => cluster.worker.disconnect())
}
})
}

// start single-threaded server
Expand Down

0 comments on commit 15fc619

Please sign in to comment.