-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcluster_server.js
More file actions
29 lines (25 loc) · 849 Bytes
/
Copy pathcluster_server.js
File metadata and controls
29 lines (25 loc) · 849 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
var cluster = require('cluster');
var numWorkers = require('os').cpus().length;
if (cluster.isMaster) {
console.log("starting "+numWorkers + " clusters");
for (var i = 0; i < numWorkers; i++) {
cluster.fork();
}
cluster.on('online',function (worker) {
console.log("worker "+ worker.process.pid+ ' is working');
});
cluster.on('exit',function(worker,code,signal){
console.log('worker '+ worker.process.pid + ' stopped with code '+ code + ' and signal '+signal );
console.log('worker is starting');
cluster.fork();
});
}
else {
var express = require('express')();
express.all('/*',function(req , res){
res.end('process ' + process.pid + ' says hello ');
});
var server = express.listen(3000 , function () {
console.log('Process ' + process.pid + ' is listening to all incoming requests');
});
}