-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
138 lines (125 loc) · 3.33 KB
/
index.js
File metadata and controls
138 lines (125 loc) · 3.33 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
var cluster = require('cluster')
, util = require('util')
, events = require('events')
;
function ClusterAppError(message) {
this.message = message;
Error.captureStackTrace(this, ClusterAppError);
}
util.inherits(ClusterAppError, Error);
function args(a) {
return [].slice.call(a, 0);
}
function App(options) {
var self = this
, workers = options && options.workers || require('os').cpus().length
, timeout = options && options.timeout || 2000
, respawn = options && options.restart || false
, evlog = options && options.evlog || false
, started = false
, inited = false
;
function setEvlog(value) {
evlog = value;
return self;
}
function init(w) {
if (!w) throw new ClusterAppError('Empty worker value');
if (inited)
throw new ClusterAppError('Already initialized');
try {
require.resolve(w)
} catch (e) {
throw new ClusterAppError('Wrong worker')
}
inited = true;
cluster.setupMaster({
exec: w,
silent: false
});
return this;
}
function start() {
if (!inited) throw new ClusterAppError('Not initialized');
if (started) throw new ClusterAppError('Already started');
[
'fork'
, 'listening'
, 'online'
, 'disconnect'
, 'exit'
].forEach(function (event) {
cluster.on(event, function (worker) {
if (evlog)
util.log('Worker ' + worker.uniqueID + ': ' + event);
self.emit.apply(self, [event].concat(args(arguments)));
})
});
//timeout implementation
var timeouts = {};
self.on('fork',function (worker) {
timeouts[worker.uniqueID] = setTimeout(function () {
self.emit('timeout', worker, timeout)
}, timeout);
}).on('listening',function (worker) {
clearTimeout(timeouts[worker.uniqueID]);
}).on('exit', function (worker) {
// console.log('self.on(exit)', worker.uniqueID, 'exited')
clearTimeout(timeouts[worker.uniqueID]);
if (respawn) cluster.fork();//on exit restart
});
for (var i = 0; i < workers; i++) {
(function (w) {
w.on('exit',function () {
if (!Object.keys(cluster.workers).length)
self.emit('noworkers')
}).on('message', function (msg) {
switch (msg.action) {
case 'stop':
self.stop();
break;
case 'restart':
self.restart();
break;
case 'log':
self.emit('log', msg.msg);
break;
}
})
})(cluster.fork())
}
self.emit('start');
started = true;
return self
}
function stop() {
if (!started) throw new ClusterAppError('Not started');
var t = respawn;
respawn = false;
for (var id in cluster.workers) {
if (cluster.workers.hasOwnProperty(id)) {
cluster.workers[id].process.kill();
}
}
self.on('noworkers', function () {
respawn = t;
});
started = false;
self.emit('stop');
return self
}
function restart() {
self.on('stop',function () {
start()
}).stop();
return self
}
this.init = init;
this.start = start;
this.stop = stop;
this.restart = restart;
this.setEvlog = setEvlog
}
util.inherits(App, events.EventEmitter);
App.ClusterAppError = ClusterAppError;
module.exports = App;