/
index.js
360 lines (347 loc) · 10.5 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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
'use strict';
/*
*
* Stereo multi-node helper
* Copyright(c) 2011 Vladimir Dronnikov <dronnikov@gmail.com>
* MIT Licensed
*
*/var buf, frame, framing;
var __slice = Array.prototype.slice, __hasProp = Object.prototype.hasOwnProperty;
buf = {};
framing = function(chunk) {
var c, i, id, obj, _len;
id = this.pid;
if (!buf[id]) {
buf[id] = '';
}
for (i = 0, _len = chunk.length; i < _len; i++) {
c = chunk[i];
if ('\n' === c) {
obj = JSON.parse(buf[id]);
buf[id] = '';
this.emit('message', obj);
} else {
buf[id] += c;
}
}
};
frame = function(obj) {
return JSON.stringify(obj) + '\n';
};
/*
node cluster factory, takes options:
options.host - host to bind server to = '0.0.0.0'
options.port - port to bind server to = 80
options.connections - listener capacity = 1024
worker process options
options.uid
options.gid
options.pwd
options.args
options.env
workers configuration
options.workers - number of workers to start = # of CPU cores
options.workerShutdownTimeout
options.ipc - UNIX domain socket path for IPC = '.ipc'
files modification watchdog
options.watch - array of paths to watch for = undefined
options.watchInterval - interval to watch for changes, ms = 500
REPL
options.repl - start REPL
true -- REPL on stdin
<number> -- REPL on localhost:<number>
<string> -- REPL on UNIX socket <string>
HTTPS credentials paths
options.ssl.key
options.ssl.cert
options.ssl.caCerts
*/
module.exports = function(server, options) {
var REPL, args, cmd, comm, credentials, env, fs, ipc, k, net, netBinding, nworkers, socket, spawnWorker, v, watch, workers, _ref, _ref2, _ref3, _ref4, _ref5;
if (options == null) {
options = {};
}
net = require('net');
fs = require('fs');
(_ref = options.port) != null ? _ref : options.port = 3000;
(_ref2 = options.host) != null ? _ref2 : options.host = '0.0.0.0';
nworkers = options.workers || require('os').cpus().length;
(_ref3 = options.ipc) != null ? _ref3 : options.ipc = '.ipc';
if (process.env._NODE_WORKER_FOR_) {
process.log = function() {
var args;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
args[0] = ("" + (Date.now()) + " WORKER " + process.pid + ": ") + args[0];
return console.error.apply(console, args);
};
if (!server) {
if (options.ssl) {
credentials = {
key: fs.readFileSync(options.ssl.key, 'utf8'),
cert: fs.readFileSync(options.ssl.cert, 'utf8')
};
server = require('https').createServer(credentials);
} else {
server = require('http').createServer();
}
}
if (options.workerShutdownTimeout) {
process.on('SIGQUIT', function() {
this.log('shutting down...');
if (server.connections) {
server.watcher.stop();
setInterval((function() {
return server.connections || process.exit(0);
}), 2000);
return setTimeout((function() {
return process.exit(0);
}), options.workerShutdownTimeout);
} else {
return this.exit(0);
}
});
}
process.on('exit', function() {
return this.log('shutdown');
});
process.on('uncaughtException', function(err) {
this.log("EXCEPTION: " + (err.stack || err.message || err));
return this.exit(1);
});
comm = net.createConnection(options.ipc);
comm.on('connect', function() {
return comm.setEncoding('utf8');
});
comm.on('data', framing.bind(process));
comm.once('fd', function(fd) {
server.listenFD(fd);
return process.publish('register');
});
comm.once('end', function() {
return process.exit();
});
process.publish = function(channel, message) {
var data;
data = {
from: this.pid,
channel: channel,
data: message
};
return comm.write(frame(data));
};
return server;
} else {
process.log = function() {
var args;
args = 1 <= arguments.length ? __slice.call(arguments, 0) : [];
args[0] = "MASTER: " + args[0];
return console.error.apply(console, args);
};
netBinding = process.binding('net');
socket = netBinding.socket('tcp' + (netBinding.isIP(options.host) === 6 ? 6 : 4));
netBinding.bind(socket, options.port, options.host);
netBinding.listen(socket, options.connections || 1024);
if (process.getuid() === 0) {
if (options.uid) {
process.setuid(options.uid);
}
if (options.gid) {
process.setgid(options.gid);
}
}
if (options.pwd) {
process.chdir(options.pwd);
}
workers = {};
args = options.args || process.argv;
env = {};
_ref4 = process.env;
for (k in _ref4) {
if (!__hasProp.call(_ref4, k)) continue;
v = _ref4[k];
env[k] = v;
}
_ref5 = options.env || {};
for (k in _ref5) {
if (!__hasProp.call(_ref5, k)) continue;
v = _ref5[k];
env[k] = v;
}
spawnWorker = function() {
var worker;
env._NODE_WORKER_FOR_ = process.pid;
return worker = require('child_process').spawn(args[0], args.slice(1), {
env: env,
customFds: [0, process.stdout, process.stderr]
});
};
process.publish = function(channel, message) {
var data, pid, worker;
data = frame({
from: null,
channel: channel,
data: message
});
for (pid in workers) {
worker = workers[pid];
worker.write(data);
}
};
ipc = net.createServer(function(stream) {
stream.setEncoding('utf8');
stream.write('{"foo": "bar"}\n', 'utf8', socket);
stream.on('data', framing.bind(process));
stream.on('end', function() {
var pid, worker;
for (pid in workers) {
worker = workers[pid];
if (worker === stream) {
delete workers[pid];
}
}
if (nworkers > Object.keys(workers).length) {
spawnWorker();
}
});
return process.on('message', function(data) {
var pid, worker;
if (data.channel === 'bcast') {
data = frame(data);
for (pid in workers) {
worker = workers[pid];
worker.write(data);
}
} else if (data.channel === 'register') {
workers[data.from] = stream;
process.log("WORKER " + data.from + " started and listening to *:" + options.port);
}
});
});
ipc.listen(options.ipc, function() {
var id;
for (id = 0; (0 <= nworkers ? id < nworkers : id > nworkers); (0 <= nworkers ? id += 1 : id -= 1)) {
spawnWorker();
}
});
['SIGINT', 'SIGTERM', 'SIGKILL', 'SIGUSR2', 'SIGHUP', 'SIGQUIT', 'exit'].forEach(function(signal) {
return process.on(signal, function() {
var pid, worker;
this.log("signalled " + signal);
for (pid in workers) {
worker = workers[pid];
try {
process.log("sending " + signal + " to WORKER " + pid);
process.kill(pid, signal);
} catch (err) {
process.log("sending EMERGENCY exit to WORKER " + pid);
worker.emit('exit');
}
}
if (signal !== 'exit' && signal !== 'SIGHUP' && signal !== 'SIGQUIT') {
return process.exit();
}
});
});
if (options.repl) {
REPL = function(stream) {
var k, repl, v, _ref;
repl = require('repl').start('node>', stream);
_ref = {
shutdown: function() {
nworkers = 0;
process.kill(process.pid, 'SIGQUIT');
return process.exit(0);
},
stop: function() {
return process.exit(0);
},
respawn: function() {
return process.kill(process.pid, 'SIGQUIT');
},
restart: function() {
return process.kill(process.pid, 'SIGHUP');
},
spawn: function(n) {
if (n > 0) {
while (n-- > 0) {
setTimeout(function() {
spawnWorker();
return ++nworkers;
}, n * 1000);
}
} else if (n < 0) {
nworkers = Math.max(0, nworkers + n);
process.kill(process.pid, 'SIGQUIT');
}
},
mem: function() {
return console.log(process.memoryUsage());
},
status: function() {
var pid, status, worker;
console.log("TOTAL " + (Object.keys(workers).length) + " worker(s)\n");
for (pid in workers) {
worker = workers[pid];
try {
process.kill(pid, 0);
status = 'alive';
} catch (err) {
if (ESRCH === err.errno) {
status = 'dead';
} else {
throw err;
}
}
console.log("STATUS for " + pid + " is " + status);
}
}
};
for (k in _ref) {
v = _ref[k];
repl.context[k] = v;
}
};
if (options.repl === true) {
process.stdin.on('close', process.exit);
REPL();
process.log("REPL running in the console. Use CTRL+C to stop.");
} else {
net.createServer(REPL).listen(options.repl);
if (typeof options.repl === 'number') {
process.log("REPL running on 127.0.0.1:" + options.repl + ". Use CTRL+C to stop.");
} else {
process.log("REPL running on " + options.repl + ". Use CTRL+C to stop.");
}
}
}
if (options.watch) {
watch = options.watch.join(' ');
cmd = "find " + watch;
require('child_process').exec(cmd, function(err, out) {
var files, restarting;
restarting = false;
files = out.trim().split('\n');
return files.forEach(function(file) {
process.log("WATCH: " + file);
return fs.watchFile(file, {
interval: options.watchInterval || 500
}, function(curr, prev) {
if (restarting) {
return;
}
if (curr.mtime > prev.mtime) {
process.log("" + file + " has changed, respawning");
restarting = true;
process.kill(process.pid, 'SIGQUIT');
return restarting = false;
}
});
});
});
}
process.on('uncaughtException', function(err) {
process.log("EXCEPTION: " + (err.stack || err.message));
return process.kill(process.pid, 'SIGHUP');
});
}
};