forked from azat-co/practicalnode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcluster.js
More file actions
24 lines (23 loc) · 818 Bytes
/
Copy pathcluster.js
File metadata and controls
24 lines (23 loc) · 818 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
var cluster = require('cluster');
var numCPUs = require('os').cpus().length;
var express = require('express');
if (cluster.isMaster) {
console.log (' Fork %s worker(s) from master', numCPUs);
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}
cluster.on('online', function(worker) {
console.log ('worker is running on %s pid', worker.process.pid);
});
cluster.on('exit', function(worker, code, signal) {
console.log('worker with %s is closed', worker.process.pid );
});
} else if (cluster.isWorker) {
var port = 3000;
console.log('worker (%s) is now listening to http://localhost:%s', cluster.worker.process.pid, port);
var app = express();
app.get('*', function(req, res) {
res.send(200, 'cluser ' + cluster.worker.process.pid + ' responded \n');
});
app.listen(port);
}