Skip to content

Instantly share code, notes, and snippets.

@parshap
Created December 22, 2013 16:34
Show Gist options
  • Save parshap/8085068 to your computer and use it in GitHub Desktop.
Save parshap/8085068 to your computer and use it in GitHub Desktop.
Node.js zero-downtime reload with cluster
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();
}
@heapwolf
Copy link

this might be an over simplification of the problem. but if it suites your use case, use it :)

@parshap
Copy link
Author

parshap commented Dec 22, 2013

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