Skip to content

Instantly share code, notes, and snippets.

@Acconut
Created March 9, 2016 19:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Acconut/6531444131ce90b4b12e to your computer and use it in GitHub Desktop.
Save Acconut/6531444131ce90b4b12e to your computer and use it in GitHub Desktop.
var cluster = require("cluster");
var http = require("http");
if(cluster.isMaster) {
cluster.fork();
testConnection();
setInterval(testConnection, 1000);
} else {
var server = http.createServer(function(req, res) {
res.writeHeader(202);
res.end();
setTimeout(function() {
server.listen(7070);
}, 2000);
server.close();
});
server.listen(7070);
}
function testConnection() {
http.get("http://localhost:7070/", function(res) {
printStatus(res.statusCode == 202);
}).on("error", function() {
printStatus(false);
});
}
function printStatus(success) {
var worker = cluster.workers[1];
if(success) {
process.stdout.write(".");
} else {
process.stdout.write("!");
}
if(!worker.isConnected() || worker.isDead()) {
console.error("Worker dead or disconnected");
process.exit(1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment