-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
173 lines (139 loc) · 4.79 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
var _ = require('lodash');
var util = require('util');
var cluster = require('cluster');
var mixdownMaster = require('./lib/master.js');
var mixdownWorker = require('./lib/worker.js');
var path = require('path');
var packageJSON = require(path.join(process.cwd(), '/package.json'));
// Export the factory
module.exports.create = function(mixdown, options) {
var main = new Main(mixdown, options);
return main;
};
var Main = function(mixdown, options) {
// instance attrs
this.server = null;
this.workers = {}; // if this is a master, then we'll load this with child processes.
this.socket = null;
this.master = null; // if this is a master, then we'll set this delegate.
this.worker = null; // if this is a worker, then we'll set this delegate.
// passed configs.
this.mixdown = mixdown;
this.options = _.defaults(options || {}, {
cluster: {
on: false
}
});
};
var logServerInfo = function(server,message) {
var hmap = _.map(server.mixdown.apps, function(app){
return _.pick(app, 'vhosts', 'id');
});
var address = server.server && server.server.address();
logger.info(message || 'Server Information. ', address|| ' ', hmap);
};
Main.prototype.createMaster = function(callback) {
var self = this;
// start server. Sets up server, port, and starts the app.
self.master = new mixdownMaster(self.workers, self.options, self.mixdown);
self.master.start(function(err, data) {
if (err) {
logger.error("Could not start server. Stopping process.", err);
process.exit();
}
else {
self.socket = data.socket;
self.server = data.server;
logServerInfo(self, 'Server started successfully.');
typeof(callback) === 'function' ? callback(err, self) : null;
}
});
};
Main.prototype.stop = function(callback) {
throw new Error('stop() not implemented on server. TODO.');
};
Main.prototype.start = function(callback) {
var self = this;
var mixdown = this.mixdown;
// this reload listener just logs the reload info.
mixdown.on('reload', function() {
logServerInfo(self, 'Mixdown reloaded. ');
});
// Start cluster.
var clusterConfig = mixdown.main.options.cluster || {};
if(clusterConfig.on){
var numChidrenToSpawn = clusterConfig.workers || require('os').cpus().length;
if(cluster.isMaster){
logger.info("Using cluster");
//cluser is on, and this is the master!
logger.info("Starting master with " + numChidrenToSpawn + " workers");
// spawn n workers
for (var i = 0; i < numChidrenToSpawn; i++) {
(function(){
var child = cluster.fork();
child.once('message',function(message){
if(message == 'ready'){
self.workers[child.process.pid] = child;
logger.debug('initial child ready');
}
});
})();
}
// Add application kill signals.
var signals = ['SIGINT', 'SIGTERM', 'SIGQUIT'];
_.each(signals, function(sig) {
process.on(sig, function() {
_.each(cluster.workers, function(child) {
child.destroy(); // send suicide signal
});
// create function to check self all workers are dead.
var checkExit = function() {
if (_.keys(cluster.workers).length == 0) {
process.exit();
}
else {
setImmediate(checkExit); // keep polling for safe shutdown.
}
};
// poll the master and exit when children are all gone.
setImmediate(checkExit);
});
});
cluster.on('disconnect',function(worker) {
delete self.workers[worker.process.pid];
logger.info('worker '+worker.process.pid+' disconnected');
});
cluster.on('exit', function(worker) {
// remove the child from the tracked running list..
delete self.workers[worker.process.pid];
// if it purposely destroyed itself, then do no re-spawn.
if(!worker.suicide){
logger.error('Worker exited unexpectedly. Spawning new worker');
// spawn new child
var child = cluster.fork();
child.on('message',function(message){
if(message == 'ready'){
logger.debug('respawned child ready id: ' + child.process.pid);
self.workers[child.process.pid] = child;
}
});
}
});
self.createMaster(callback);
}
else {
//cluser is on, and this is a worker!
logger.info("new worker Worker id: "+process.pid);
try {
self.worker = new mixdownWorker(mixdown);
}
catch(e) {
typeof(callback) === 'function' ? callback(e, self) : null;
}
}
}
else {
//cluster isn't running so create a master server.
self.createMaster(callback);
}
};