Skip to content

Instantly share code, notes, and snippets.

@turtlesoupy
Created September 21, 2012 06:41
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save turtlesoupy/3760055 to your computer and use it in GitHub Desktop.
Save turtlesoupy/3760055 to your computer and use it in GitHub Desktop.
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