-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
261 lines (196 loc) · 8.36 KB
/
index.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
//cluster-emitter is a different EventEmitter which allows all messages send between master & slaves, with an additional parameters as 'target'
//the rest of the api should be the same as EventEmitter, so 'on', 'once', 'removeListener', 'removeAllListeners', 'emit'
'use strict';
var cluster = require('cluster'),
_ = require('underscore');
var masterEmitter = process.masterEmitter = process.masterEmitter || {//masterEmitter is the emitter variation in master process
get logger(){
if(!_.isFunction(process.getLogger)){
return {
'info': _.bind(console.log, console),
'debug': _.bind(console.log, console)
};
}
return process.getLogger(__filename);
},
get workers(){//return active workers
return cluster.workers;
},
'handlers': {
//a map of event handlers
},
'on': function(event, handler, duplicateAllowed){
var _this = this;
_this.handlers[event] = _this.handlers[event] || [];
if(!_.contains(_this.handlers, handler) || duplicateAllowed){//avoid duplicates
_this.handlers[event].push(handler);
}
_.each(_this.workers, function(worker){
if(!worker.clusterEventHandler){
//this is the 1st time a handler is registered, must register 'message' handler on all workers
worker.clusterEventHandler = function(message){
_.invoke(_this.handlers[message.type] || [], 'apply', null, message.params);
};
worker.on('message', worker.clusterEventHandler);
}
});
if(_this.handlers[event].length === 1){
//this is the 1st time a handler of this event is registered in master process
process.on(event, function(){
_.invoke(_this.handlers[event] || [], 'apply', null, arguments);
});
}
},
'once': function(event, handler){
var _this = this,
handleOnce = function handleOnce(){
//wrap handler, and remove itself immediately after
handler.apply(null, arguments);
_this.removeListener(event, handleOnce);
};
_this.on(event, handleOnce);
},
'emit': function(){
var _this = this,
pids = _.map(_this.workers, function(worker){//map all workers' pids
return worker.process.pid;
});
return _this.emitTo(pids.concat(['self'])/*plus 'self'*/, arguments);
},
'to': function(targets){
var _this = this;
return {
'emit': function(){
//set audience to @param targets
return _this.emitTo(targets, arguments);
}
};
},
'emitTo': function(targets, args){
args = _.toArray(args || []);
var _this = this,
event = args.shift(),
audience = _.filter(_this.workers, function(worker){
//filter workers, include only those in the @param targets
return _.contains(targets, worker.process.pid);
});
_.each(audience, function(worker){
try{
worker.send({
'type': event,
'params': args
});
}
catch(error){
_this.logger.warn('[cluster2] master sending to worker failed:%j', error);
}
});
if(_.contains(targets, 'master') || _.contains(targets, 'self') || _.contains(targets, process.pid)){
//audience including 'master' itself
args.unshift(event);
process.emit.apply(process, args);
}
},
'removeListener': function(event, handler){
var _this = this;
_this.handlers[event] = _.without(_this.handlers[event] || [], handler);
if(_this.handlers[event].length === 0){
process.removeAllListeners(event);
}
},
'removeAllListeners': function(event){
this.handlers[event] = [];
process.removeAllListeners(event);
}
},
//slaveEmitter is the emitter variation in all worker processes
slaveEmitter = process.slaveEmitter = process.slaveEmitter || {
'handlers': {},
'on': function(event, handler){
var _this = this;
_this.handlers[event] = _this.handlers[event] || [];
_this.handlers[event].push(handler);
if(_this.handlers[event].length === 1){
//1st time this event handler is registered in worker process
process.on(event, function(){
_.invoke(_this.handlers[event] || [], 'apply', null, arguments);
});
}
},
'once': function(event, handler){
var _this = this,
handleOnce = function handleOnce(){
handler.apply(null, arguments);
_this.removeListener(event, handleOnce);
};
_this.on(event, handleOnce);
},
'emit': function(){
//default audience being both 'master' & 'worker' itself
return this.emitTo(['master', 'self'], arguments);
},
'to': function(targets){
var _this = this;
return {
'emit': function(){
//set audience to @param targets
return _this.emitTo(targets, arguments);
}
};
},
'emitTo': function(targets, args){
args = _.toArray(args || []);
var event = args.shift();
if(_.contains(targets, 'master')){
//audience includes 'master'
process.send({
'type': event,
'params': args
});
}
if(_.contains(targets, 'self') || _.contains(targets, process.pid)){
//audience includes 'worker' itself
args.unshift(event);
process.emit.apply(process, args);
}
},
'removeListener': function(event, handler){
var _this = this;
_this.handlers[event] = _.without(_this.handlers[event] || [], handler);
if(_this.handlers[event].length === 0){
process.removeAllListeners(event);
}
},
'removeAllListeners': function(event){
this.handlers[event] = [];
process.removeAllListeners(event);
}
};
//export the proper emitter based on whose runtime this is, master vs. worker.
if(cluster.isMaster){
var emitter = module.exports = masterEmitter;
//any newly forked worker must get a replication of the known event handlers
cluster.on('fork', function(worker){
if(!worker.clusterEventHandler){
worker.clusterEventHandler = function(message){
_.invoke(emitter.handlers[message.type] || [], 'apply', null, message.params);
};
worker.on('message', worker.clusterEventHandler);
}
});
emitter.on('ask-parent-pid', function(childPid){
//just to tell the child the parent's pid, sends null if it's the parent itself asking
if(childPid){
emitter.to([childPid]).emit('reply-parent-pid', process.pid === childPid ? null : process.pid);
}
});
}
else{
var emitter = module.exports = slaveEmitter;
//enable listening to master's messages
process.on('message', function(message){
_.each(emitter.handlers[message.type] || [], function(h){
h.apply(null, message.params);
});
});
}