Created
September 21, 2012 06:41
-
-
Save turtlesoupy/3760055 to your computer and use it in GitHub Desktop.
Minimal example of a gracefully restarting node.js process
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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