forked from corruptmem/nodejsmodules
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcluster.coffee
More file actions
34 lines (27 loc) · 845 Bytes
/
cluster.coffee
File metadata and controls
34 lines (27 loc) · 845 Bytes
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
cluster = require('cluster')
children = 4
exiting = false
if cluster.isMaster
recycle = () =>
for workerId of cluster.workers
worker = cluster.workers[workerId]
worker.send('shutdown')
term = () =>
console.log("master got SIGTERM - terminating workers then exiting.")
exiting = true
recycle()
process.on 'SIGTERM', term
process.on 'SIGINT', term
process.on 'SIGHUP', =>
console.log("master got SIGHUP - recycling workers")
recycle()
console.log("master #{process.pid} is online")
for s in [1..children]
cluster.fork()
cluster.on 'exit', (worker, code, signal) =>
if not exiting
console.error("worker #{worker.process.pid} died: #{code}, #{signal}")
setTimeout (=> cluster.fork()), 2000
else
console.log("worker #{process.pid} is online")
require('./server')