-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
51 lines (44 loc) · 1.16 KB
/
main.js
File metadata and controls
51 lines (44 loc) · 1.16 KB
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
var _cluster = require('cluster');
var _os = require('os');
var _ = require('underscore');
var main = './operator.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
});
for (var i = 0; i < cpuCount; 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 + 5000 < 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();
}
});
};