Name: js-handler/node_modules/daemon/examples/cluster.js
| 1: | var cluster = require('cluster'); |
| 2: | var numCPUs = require('os').cpus().length; |
| 3: | |
| 4: | if (cluster.isMaster) { |
| 5: | // Fork workers. |
| 6: | for (var i = 0; i < numCPUs; ++i) { |
| 7: | cluster.fork(); |
| 8: | } |
| 9: | |
| 10: | cluster.on('exit', function(worker, code, signal) { |
| 11: | console.log('worker ' + worker.process.pid + ' died'); |
| 12: | cluster.fork(); |
| 13: | }); |
| 14: | |
| 15: | // daemonize after setting up cluster |
| 16: | return require('../')(); |
| 17: | } |
| 18: | |
| 19: | var http = require('http'); |
| 20: | http.createServer(function(req, res) { |
| 21: | res.writeHead(200); |
| 22: | res.end('process: ' + process.pid); |
| 23: | |
| 24: | // just a demo to cycle workers |
| 25: | // DO NOT DO THIS IN PRODUCTION |
| 26: | process.exit(); |
| 27: | }).listen(8000); |
