forked from tcecspectator/mytcecgui
-
Notifications
You must be signed in to change notification settings - Fork 17
/
cluster.js
63 lines (53 loc) · 1.53 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/*
globals
require, setInterval
*/
'use strict';
let {DefaultInt, LS} = require('./js/common.js'),
cluster = require('cluster'),
os = require('os'),
argv = require('yargs').argv;
let // bonus = DefaultInt(argv.bonus, 0),
numServer = 1,
portnum = DefaultInt(argv.port, 8000);
if (argv.port != undefined)
argv.port = portnum;
function eachWorker(callback) {
for (let id in cluster.workers)
callback(cluster.workers[id]);
}
if (cluster.isMaster) {
let clientCount = 0,
count = 0,
cpus = os.cpus().length;
for (let i = 0; i < numServer; i ++) {
LS(`Forking for ${cpus} CPUs`);
count = 0;
let worker = cluster.fork();
worker.on('message', function(msg) {
if (typeof msg.users != 'undefined') {
LS(`CLUSTER: count=${count} : msg.users=${parseInt(msg.users)} : clientCount=${clientCount}`);
count = parseInt(count) + parseInt(msg.users);
clientCount ++;
}
});
}
let updateWorkers = () => {
eachWorker(worker => {
if (clientCount == numServer)
worker.send({'count':count});
});
count = 0;
clientCount = 0;
};
updateWorkers();
setInterval(updateWorkers, 10000);
cluster.on('exit', (worker, code) => {
if (code !== 0 && !worker.exitedAfterDisconnect) {
LS(`Worker ${worker.id} crashed. Starting a new worker...`);
cluster.fork();
}
});
}
else
require('./server-new.js');