Created
December 22, 2013 16:34
-
-
Save parshap/8085068 to your computer and use it in GitHub Desktop.
Node.js zero-downtime reload with cluster
This file contains 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
var cluster = require("cluster"); | |
function master() { | |
var worker = cluster.fork(); | |
process.on("SIGUSR2", function() { | |
var newWorker = cluster.fork(); | |
newWorker.on("listening", function() { | |
worker.disconnect(); | |
worker = newWorker; | |
}); | |
}); | |
} | |
function worker() { | |
require("http").createServer(function(req, res) { | |
res.end("Hello"); | |
}).listen(); | |
} | |
if (cluster.isMaster) { | |
master(); | |
} | |
else { | |
worker(); | |
} |
Care to elaborate? Some potential issues I've thought of:
- May need application-level logic to gracefully shutdown (e.g., end long-lasting requests or client connections)
- Can't zero-downtime update the master
- This example code doesn't handle concurrent SIGUSR2 signals :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this might be an over simplification of the problem. but if it suites your use case, use it :)