Created
August 24, 2012 18:56
-
-
Save wishfoundry/3454315 to your computer and use it in GitHub Desktop.
Node unix socket worker process
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
if (cluster.isMaster) { | |
var cpus = os.cpus().length; | |
var worker; | |
/* start up the HTTP servers */ | |
process.env.HTTP_WORKER = "1"; | |
for (var i = 0; i < cpus; i++) { | |
worker = cluster.fork(); | |
worker.isHTTP = true; | |
} | |
delete process.env.HTTP_WORKER; | |
process.env.CPU_WORKER = "1"; | |
/* start up the workers */ | |
for (var i = 0; i < cpus; i++) { | |
cluster.fork(); | |
} | |
cluster.on("death", function (worker) { | |
console.log("worker " + worker.pid + " died, respawning"); | |
if (worker.isHTTP) { | |
process.env.HTTP_WORKER = "1"; | |
delete process.env.CPU_WORKER; | |
} else { | |
process.env.CPU_WORKER = "1"; | |
delete process.env.HTTP_WORKER; | |
} | |
var newguy = cluster.fork(); | |
if (worker.isHTTP) { | |
newguy.isHTTP = true; | |
} | |
}); | |
} else if (process.env.HTTP_WORKER) { | |
everyauth.helpExpress(app); | |
app.listen(1337); // listens on http://127.0.0.1:1337 | |
} else if (process.env.CPU_WORKER) { | |
/* code omitted because I haven't written it */ | |
var srv = net.createServer(...); | |
srv.listen("/tmp/worker"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment