Skip to content

Instantly share code, notes, and snippets.

@Acconut
Created March 9, 2016 20:04
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/ccbf22ea97e1f0f783bb to your computer and use it in GitHub Desktop.
Save Acconut/ccbf22ea97e1f0f783bb to your computer and use it in GitHub Desktop.
var cluster = require("cluster");
var http = require("http");
if(cluster.isMaster) {
cluster.fork();
cluster.fork();
testConnection();
setInterval(testConnection, 1000);
} else {
var server = http.createServer(function(req, res) {
process.stdout.write(cluster.worker.id.toString());
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) {
if(success) {
process.stdout.write(".");
} else {
process.stdout.write("!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment