-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathcluster-service.js
92 lines (77 loc) · 2.54 KB
/
cluster-service.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
var cluster = require("cluster");
var colors = require("colors");
if (!('cservice' in global)) {
global.cservice = { locals: require("./lib/defaults") };
}
module.exports = exports;
exports.debug = require("./lib/util").debug;
exports.log = require("./lib/util").log;
exports.error = require("./lib/util").error;
exports.results = require("./lib/util").results;
exports.processSafeSend = require("./lib/util").processSafeSend;
exports.msgBus = require("./lib/message-bus");
exports.workerReady = require("./lib/worker-ready");
Object.defineProperty(exports, "workers", {
get: require("./lib/workers").get
});
Object.defineProperty(exports, "isMaster", {
get: function() {
return cluster.isMaster;
}
});
Object.defineProperty(exports, "isWorker", {
get: function() {
return cluster.isWorker;
}
});
Object.defineProperty(exports, "options", {
get: function() {
return global.cservice.locals.options;
}
});
Object.defineProperty(exports, "locals", {
get: function() {
return global.cservice.locals;
}
});
if (cluster.isMaster === true) {
exports.control = require("./lib/control").addControls;
exports.stop = require("./lib/stop");
exports.newWorker = require("./lib/new-worker");
exports.on = require("./lib/commands").on;
exports.registerCommands = require("./lib/commands").register;
} else {
exports.on = function() { };
exports.registerCommands = function() { };
}
exports.trigger = require("./lib/trigger");
exports.start = require("./lib/start");
exports.netServers = require("./lib/net-servers");
exports.netStats = require("./lib/net-stats");
exports.proxy = require('./lib/proxy');
if (
cluster.isWorker === true
&& typeof (cluster.worker.module) === "undefined"
){
// intermediate state to prevent 2nd call while async in progress
cluster.worker.module = {};
cluster.worker.env = process.env;
var workers = require("./lib/workers");
workers.demote();
// load the worker if not already loaded
// async, in case worker loads cluster-service, we need to return before
// it's avail
setImmediate(function() {
cluster.worker.module = require(process.env.worker);
if (global.cservice.locals.workerReady === undefined
&& process.env.ready.toString() === "false") {
// if workerReady not invoked explicitly, we'll track it automatically
exports.workerReady(false);
exports.netServers.waitForReady(function() {
exports.workerReady(); // NOW we're ready
});
}
});
// start worker monitor to establish two-way relationship with master
workers.monitor();
}