-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
74 lines (66 loc) · 1.63 KB
/
main.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
64
65
66
67
68
69
70
71
72
73
var _cluster = require('cluster');
var _os = require('os');
var _ = require('underscore');
var _datastore = require('./lib.datastore').datastore;
var main = './cylon.js';
var options = {
timeout: 10000 // if the process doesn't respond after this time, it is killed
}
var i;
var workers = {};
var cpuCount = _os.cpus().length;
_cluster.setupMaster({
exec: main
});
function processArgs() {
var i;
var args = process.argv.slice(2);
var output = {};
for (i=0;i<args.length;i++) {
var l1 = args[i].substr(0,1);
if (l1 == "-") {
output[args[i].substr(1)] = args[i+1];
i++;
}
}
return output;
};
var args = processArgs();
for (i in args) {
console.log("-> ",i,": ",args[i]);
}
if (!args.thread) {
args.thread = 64;
}
args.thread = Math.min(args.thread*1, cpuCount);
for (var i = 0; i < args.thread; i++) {
createWorker();
}
if (_cluster.isMaster) {
if (options.timeout > 0) {
setInterval(function() {
var time = new Date().getTime();
for (pid in workers) {
if (workers[pid] && workers[pid].lastCheck + 10000 < time) {
console.log("TIMEOUT: ", pid);
workers[pid].worker.process.kill();
delete workers[pid];
createWorker();
}
}
}, options.timeout);
}
}
function createWorker() {
var worker = _cluster.fork();
console.log("New worker: ",worker.process.pid);
workers[worker.process.pid] = {
worker: worker,
lastCheck: new Date().getTime()-1000 // allow boot time
};
worker.on('message', function(data) {
if (workers[worker.process.pid] && workers[worker.process.pid].lastCheck) {
workers[worker.process.pid].lastCheck = new Date().getTime();
}
});
};