-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.coffee
454 lines (396 loc) · 10.4 KB
/
index.coffee
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
'use strict'
###
*
* Stereo multi-node helper
* Copyright(c) 2011 Vladimir Dronnikov <dronnikov@gmail.com>
* MIT Licensed
*
###
#
# thanks 'LearnBoost/cluster'
# takes chunks in buffer. when the buffer contains valid JSON literal
# reset the buffer and emit 'message' event passing parsed JSON as parameter
#
# usage: stream.on('data', framing.bind(stream, id))
#
#
# TODO: JSON is way slow. msgpack?
#
buf = {}
framing = (chunk) ->
id = @pid
buf[id] = '' unless buf[id]
for c, i in chunk
if '\n' is c
#process.log '---'
#process.log 'FRAME', buf[id]
#process.log '---'
obj = JSON.parse buf[id]
buf[id] = ''
@emit 'message', obj
else
buf[id] += c
return
frame = (obj) ->
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 = (server, options = {}) ->
net = require 'net'
fs = require 'fs'
# options
options.port ?= 3000
options.host ?= '0.0.0.0'
nworkers = options.workers or require('os').cpus().length
options.ipc ?= '.ipc'
####################################################################
#
# worker branch
#
####################################################################
if process.env._NODE_WORKER_FOR_
#
# define logger
#
process.log = (args...) ->
args[0] = "#{Date.now()} WORKER #{process.pid}: " + args[0]
console.error.apply console, args
#
# setup HTTP(S) server
# N.B. request handler to be attached elsewhere
#
unless server
if options.ssl
credentials =
key: fs.readFileSync options.ssl.key, 'utf8'
cert: fs.readFileSync options.ssl.cert, 'utf8'
#ca: options.ssl.caCerts.map (fname) -> fs.readFileSync fname, 'utf8'
server = require('https').createServer credentials
else
server = require('http').createServer()
#
# setup signals
#
#
# graceful shutdown, if timeout specified
#
if options.workerShutdownTimeout
process.on 'SIGQUIT', () ->
@log 'shutting down...'
if server.connections
# stop accepting
server.watcher.stop()
# check pending connections
setInterval (-> server.connections or process.exit 0), 2000
# timeout
setTimeout (-> process.exit 0), options.workerShutdownTimeout
else
@exit 0
#
# exit
#
process.on 'exit', () ->
@log 'shutdown'
#
# uncaught exceptions cause worker shutdown
#
process.on 'uncaughtException', (err) ->
@log "EXCEPTION: #{err.stack or err.message or err}"
@exit 1
#
# establish communication with master
#
comm = net.createConnection options.ipc
#
# connected to master -> setup the stream
#
comm.on 'connect', () ->
comm.setEncoding 'utf8'
#process.publish 'connect'
#
# wait for complete JSON message to come, parse it and emit process' 'message' event
#
comm.on 'data', framing.bind process
#
# relay received messages to the process 'message' handler
#
#comm.on 'message', (message) -> process.emit 'message', message
#
# master socket descriptor has arrived
#
comm.once 'fd', (fd) ->
# listen to the master socket
server.listenFD fd
# register the worker
process.publish 'register'
#
# master has gone -> exit
#
comm.once 'end', () ->
process.exit()
#
# define message publisher
#
process.publish = (channel, message) ->
data =
from: @pid
channel: channel
data: message
comm.write frame data
#
# keep-alive?
#
#setInterval (() -> process.publish 'bcast', foo: 'bar'), 10000
#
# return server for further tuning
#
return server
####################################################################
#
# master branch
#
####################################################################
else
#
# define logger
#
process.log = (args...) ->
args[0] = "MASTER: " + args[0]
console.error.apply console, args
#
# bind master socket
#
netBinding = process.binding 'net'
socket = netBinding.socket 'tcp' + (if netBinding.isIP(options.host) is 6 then 6 else 4)
netBinding.bind socket, options.port, options.host
netBinding.listen socket, options.connections or 1024
#
# drop privileges
#
if process.getuid() is 0
process.setuid options.uid if options.uid
process.setgid options.gid if options.gid
#
# chdir
#
process.chdir options.pwd if options.pwd
#
# setup IPC
#
workers = {} # array of workers
args = options.args or process.argv # allow to override workers arguments
# copy environment
env = {}
env[k] = v for own k, v of process.env
env[k] = v for own k, v of options.env or {}
spawnWorker = () ->
env._NODE_WORKER_FOR_ = process.pid
worker = require('child_process').spawn args[0], args.slice(1),
#cwd: undefined
env: env
customFds: [0, process.stdout, process.stderr]
#setsid: false
#
# define broadcast message publisher
#
process.publish = (channel, message) ->
data = frame
from: null # master
channel: channel
data: message
worker.write data for pid, worker of workers
return
#
# create IPC server
#
ipc = net.createServer (stream) ->
#
# setup the stream
#
stream.setEncoding 'utf8'
#
# worker has born -> pass it configuration and the master socket to listen to
#
stream.write '{"foo": "bar"}\n', 'utf8', socket
#
# wait for complete JSON object to come, parse it and emit process' 'message' event
#
stream.on 'data', framing.bind process
#
# worker has gone
#
stream.on 'end', () ->
# unregister gone worker
for pid, worker of workers
if worker is stream
delete workers[pid]
# start new worker
spawnWorker() if nworkers > Object.keys(workers).length
return
#
# message from a worker
#
process.on 'message', (data) ->
# register new worker
if data.channel is 'bcast'
data = frame data
worker.write data for pid, worker of workers
else if data.channel is 'register'
workers[data.from] = stream
process.log "WORKER #{data.from} started and listening to *:#{options.port}"
return
#
# start IPC server
#
ipc.listen options.ipc, () ->
# spawn initial workers
spawnWorker() for id in [0...nworkers]
return
#
# handle signals
#
['SIGINT','SIGTERM','SIGKILL','SIGUSR2','SIGHUP','SIGQUIT','exit'].forEach (signal) ->
process.on signal, () ->
@log "signalled #{signal}"
# relay signal to all workers
for pid, worker of workers
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'
# SIGHUP just restarts workers, SIGQUIT gracefully restarts workers
process.exit() unless signal in ['exit', 'SIGHUP', 'SIGQUIT']
#
# REPL
#
# options.repl: true -- REPL on stdin
# options.repl: <number> -- REPL on localhost:<number>
# options.repl: <string> -- REPL on UNIX socket <string>
#
if options.repl
#
# define REPL handler and context
#
REPL = (stream) ->
repl = require('repl').start 'node>', stream
# expose master control interface
repl.context[k] = v for k, v of {
shutdown: () ->
nworkers = 0
process.kill process.pid, 'SIGQUIT'
process.exit 0
stop: () ->
process.exit 0
respawn: () ->
process.kill process.pid, 'SIGQUIT'
restart: () ->
process.kill process.pid, 'SIGHUP'
spawn: (n) ->
# add workers
if n > 0
while n-- > 0
# N.B. don't start all workers at once
setTimeout () ->
spawnWorker()
# adjust max workers count
++nworkers
, n * 1000
# remove workers
else if n < 0
# adjust max workers count
nworkers = Math.max(0, nworkers + n)
# shutdown all workers, spawn at most nworkers
process.kill process.pid, 'SIGQUIT'
return
mem: () ->
console.log process.memoryUsage()
status: () ->
console.log "TOTAL #{Object.keys(workers).length} worker(s)\n"
for pid, worker of workers
# thanks 'LearnBoost/cluster'
try
process.kill pid, 0
status = 'alive'
catch err
if ESRCH is err.errno
status = 'dead'
else
throw err
console.log "STATUS for #{pid} is #{status}"
return
}
return
#
# start REPL
#
if options.repl is 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 is '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."
#
# setup watchdog, to reload modified source files
# thanks spark2
#
# TODO: elaborate on inhibit restarting if restarting in progress
#
if options.watch
watch = options.watch.join(' ')
#cmd = "find #{watch} -name '*.js' -o -name '*.coffee'"
cmd = "find #{watch}"
require('child_process').exec cmd, (err, out) ->
restarting = false
files = out.trim().split '\n'
#process.log err, "WATCH?: #{files}"
files.forEach (file) ->
process.log "WATCH: #{file}"
fs.watchFile file, {interval: options.watchInterval or 500}, (curr, prev) ->
return if restarting
if curr.mtime > prev.mtime
process.log "#{file} has changed, respawning"
restarting = true
process.kill process.pid, 'SIGQUIT'
restarting = false
#
# uncaught exceptions cause workers respawn
#
process.on 'uncaughtException', (err) ->
process.log "EXCEPTION: #{err.stack or err.message}"
process.kill process.pid, 'SIGHUP'
#
# return undefined for master
#
return