-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathcluster_process.js
325 lines (279 loc) · 9.26 KB
/
cluster_process.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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
const cluster = require('cluster'),
path = require('path'),
RPC = require('./rpc'),
RPCCallback = require('./rpc-callback'),
Configuration = require('./configuration'),
EventEmitterEx = require('./event_emitter_ex'),
LusterClusterProcessError = require('./errors').LusterClusterProcessError,
LusterConfigurationError = require('./errors').LusterConfigurationError;
/**
* @param {Object} context
* @param {String} propName
* @returns {Boolean}
*/
function has(context, propName) {
return Object.prototype.hasOwnProperty.call(context, propName);
}
/**
* Add `basedir`, `node_modules` contained in the `basedir` and its ancestors to `module.paths`
* @param {String} basedir
*/
function extendResolvePath(basedir) {
// using module internals isn't good, but restarting with corrected NODE_PATH looks more ugly, IMO
module.paths.push(basedir);
const _basedir = basedir.split('/'),
size = basedir.length;
let i = 0;
while (size > i++) {
const modulesPath = _basedir.slice(0, i).join('/') + '/node_modules';
if (module.paths.indexOf(modulesPath) === -1) {
module.paths.push(modulesPath);
}
}
}
/**
* @typedef Extension
* @property {Function} [configure] (Object config, ClusterProcess proc)
*/
/**
* @constructor
* @class ClusterProcess
* @augments EventEmitterEx
*/
class ClusterProcess extends EventEmitterEx {
constructor() {
super();
/** @private */
this._remoteCommands = {};
/** @private */
this.extensions = {};
/**
* @type Promise<void>
* @private
* */
this._initPromise = new Promise(resolve => {
this.once('initialized', resolve);
});
/**
* @type {Configuration}
* @public
*/
this.config = null;
this.once('configured', this._onConfigured.bind(this));
this._setupIPCMessagesHandler();
this.registerRemoteCommand(RPC.fns.callback, RPCCallback.processCallback.bind(RPCCallback));
}
/**
* @memberOf ClusterProcess
* @property {Boolean} isMaster
* @readonly
* @public
*/
get isMaster() {
return cluster.isMaster;
}
/**
* @memberOf ClusterProcess
* @property {Boolean} isWorker
* @readonly
* @public
*/
get isWorker() {
return cluster.isWorker;
}
/**
* @event ClusterProcess#configured
*/
/**
* @fires ClusterProcess#configured
* @param {Object} config
* @param {Boolean} [applyEnv=true]
* @param {String} [basedir=process.cwd()] for Configuration#resolve relative paths
* @returns {ClusterProcess} this
* @throws {LusterConfigurationError} if configuration check failed (check errors will be logged to STDERR)
* @public
*/
configure(config, applyEnv, basedir) {
if (typeof applyEnv === 'undefined' || applyEnv) {
Configuration.applyEnvironment(config);
}
if (typeof(basedir) === 'undefined') {
basedir = process.cwd();
}
if (Configuration.check(config) > 0) {
this.emit('error',
LusterConfigurationError.createError(
LusterConfigurationError.CODES.CONFIGURATION_CHECK_FAILED));
} else {
this.config = Configuration.extend(config, basedir);
// hack to tweak underlying EventEmitter max listeners
// if your luster-based app extensively use luster events
this.setMaxListeners(this.config.get('maxEventListeners', 100));
this.emit('configured');
}
return this;
}
/**
* @param {String} name
* @param {Function} callback function(error)
*/
loadExtension(name, callback) {
const /** @type Extension */
extension = require(name);
let config = this.config.get('extensions.' + name);
this.extensions[name] = extension;
// if `config` was an Object then it became instance of Configuration
// else returns original value
config = Configuration.extend(config, this.config.getBaseDir());
if (extension.configure.length > 2) {
setImmediate(() => extension.configure(config, this, callback));
} else {
setImmediate(() => {
extension.configure(config, this);
callback();
});
}
}
/**
* @event ClusterProcess#initialized
*/
/**
* @fires ClusterProcess#initialized
* @private
*/
_onConfigured() {
cluster.setMaxListeners(this.getMaxListeners());
// try to use `extensionsPath` option to resolve extensions' modules
// use worker file directory as fallback
extendResolvePath(path.resolve(
this.config.resolve('extensionsPath', path.dirname(this.config.resolve('app')))
));
const extensions = this.config.getKeys('extensions'),
wait = extensions.length,
loadedExtensions = new Set(),
loadTimeout = this.config.get('extensionsLoadTimeout', 10000);
let loadTimer;
if (wait === 0) {
this.emit('initialized');
return;
}
extensions.forEach(name => {
this.loadExtension(name, error => {
if (error) {
return this.emit('error', error);
}
loadedExtensions.add(name);
this.emit('extension loaded', name);
if (loadedExtensions.size === wait) {
clearTimeout(loadTimer);
this.emit('initialized');
}
});
});
loadTimer = setTimeout(() => {
const timeouted = extensions.filter(name => !loadedExtensions.has(name)),
error = LusterClusterProcessError.createError(
LusterClusterProcessError.CODES.EXTENSIONS_LOAD_TIMEOUT,
{timeouted, timeout: loadTimeout});
this.emit('error', error);
}, loadTimeout);
}
/**
* Resolves when ClusterProcess done initialization.
* @this {ClusterProcess}
* @returns {Promise<void>}
*/
whenInitialized() {
return this._initPromise;
}
/**
* Register `fn` as allowed for remote call via IPC.
* @param {String} name
* @param {Function} fn
* @throws LusterClusterProcessError if remote procedure with `name` already registered.
* @public
*/
registerRemoteCommand(name, fn) {
if (has(this._remoteCommands, name)) {
throw LusterClusterProcessError.createError(
LusterClusterProcessError.CODES.REMOTE_COMMAND_ALREADY_REGISTERED,
{name});
}
this._remoteCommands[name] = fn;
}
/**
* Remove previously registered remote command
* @param {String} name
* @public
*/
unregisterRemoteCommand(name) {
delete this._remoteCommands[name];
}
/**
* Checks is remote command registered.
* @param {String} name
* @returns {Boolean}
*/
hasRegisteredRemoteCommand(name) {
return has(this._remoteCommands, name);
}
/**
* @abstract
* @throws LusterClusterProcessError if method is not overriden in the inheritor of ClusterProcess
* @private
*/
_setupIPCMessagesHandler() {
throw LusterClusterProcessError.createError(
LusterClusterProcessError.CODES.ABSTRACT_METHOD_IS_NOT_IMPLEMENTED,
{
method: 'ClusterProcess#_setupIPCMessagesHandler',
klass: this.constructor.name
});
}
/**
* Call function registered as remote command if `rawMessage` is valid luster IPC message
* @param {WorkerWrapper|Worker} target object with `remoteCall` method which can be used to respond to message
* @param {*} rawMessage
* @see RPC
* @private
*/
_onMessage(target, rawMessage) {
const message = RPC.parseMessage(rawMessage);
if (message === null) {
return;
}
if (!has(this._remoteCommands, message.cmd)) {
throw LusterClusterProcessError.createError(
LusterClusterProcessError.CODES.REMOTE_COMMAND_IS_NOT_REGISTERED,
{
name: message.cmd,
klass: this.constructor.name
});
} else if (typeof message.args === 'undefined') {
this._remoteCommands[message.cmd](target);
} else {
this._remoteCommands[message.cmd](target, ...message.args);
}
}
/**
* Register remote command with respect to the presence of callback
* @param {String} command
* @param {Function} handler
*/
registerRemoteCommandWithCallback(command, handler) {
/**
* @param {ClusterProcess} proc
* @param {*} [data]
* @param {String} callbackId
*/
this.registerRemoteCommand(command, (proc, data, callbackId) => {
/**
* @param {*} [callbackData]
*/
return handler(callbackData => {
proc.remoteCall(RPC.fns.callback, callbackId, callbackData);
}, data);
});
}
}
module.exports = ClusterProcess;