Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Minimal example of a gracefully restarting node.js process
express = require 'express'
gracefullyExiting = false
app = express.createServer()
app.use (req, res, next) ->
return next() unless gracefullyExiting
res.setHeader "Connection", "close"
res.send 502, "Server is in the process of restarting."
realApp = app.listen 61337
process.on 'SIGTERM', ->
gracefullyExiting = true
console.log "Received kill signal (SIGTERM), shutting down"
setTimeout( ->
console.error "Could not close connections in time, forcefully shutting down"
process.exit(1)
, 30*1000)
realApp.close ->
console.info "Closed out remaining connections."
process.exit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment