-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
cluster.js
49 lines (41 loc) · 1.33 KB
/
cluster.js
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const cluster = require('cluster');
const http = require('http');
const numCPUs = require('os').cpus().length;
let numReqs = 0;
if (cluster.isMaster) {
// Master process id
console.log(`Master ${process.pid} is running`);
// Fork workers.
console.log("Number of CPUs : " + numCPUs);
for (let i = 0; i < numCPUs; i++) {
let worker = cluster.fork();
worker.on('message', msg => {
if (msg.cmd && msg.cmd == 'notifyRequest') {
numReqs++;
console.log("numReqs =", numReqs);
}
});
worker.on('listening', address => {
console.log(address);
});
console.log("Worker PID : ", worker.pid === undefined ? worker.process.pid : worker.pid);
}
cluster.on('exit', (worker, code, signal) => {
console.log(`worker ${worker.process.pid} died with code ${code} and signal ${signal}`);
console.log(`worker restart ..`);
const newWorker = cluster.fork();
// Note the process IDs
const newPID = newWorker.process.pid;
const oldPID = worker.process.pid;
// Log the event
console.log('worker '+ oldPID +' died.');
console.log('worker '+ newPID +' born.');
});
} else {
// Worker processes have a http server.
http.Server( (req, res) => {
res.writeHead(200);
res.end("hello world\n");
process.send({ cmd: 'notifyRequest' });
}).listen(8000);
}