-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
186 lines (156 loc) · 4.24 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
var cluster = require('cluster')
var http = require('http')
var numCPUs = require('os').cpus().length
var net = require('net')
var EventEmitter = require('events').EventEmitter
var split = require('split')
var parse = require('through-parse')
var through = require('through')
var fs = require('fs')
var path = require('path')
var headpath = path.join(__dirname + '/HEAD')
var ee = new EventEmitter
function readHEAD() {
return fs.readFileSync(headpath).toString()
}
function writeHEAD(value) {
return fs.writeFileSync(headpath, value)
}
function reply(cmd, value) {
ee.emit('reply', { cmd: cmd, value: value || 'OK' })
}
module.exports = function(opts) {
if (typeof opts == 'string') {
opts = { target: opts }
}
if (typeof opts['zero-downtime'] === 'undefined') {
opts['zero-downtime'] = true
}
if (opts.head) {
headpath = opts.head
}
if (!cluster.isMaster) {
try {
require(readHEAD())
}
catch(ex) {
console.error(ex)
}
return
}
writeHEAD(opts.target)
cluster.on('exit', function(worker, code, signal) {
if (worker.suicide === true) return
console.log('Worker process crashed! (%d/%d/%s)',
worker.process.pid, code, signal)
if (opts['zero-downtime']) {
var fork = cluster.fork()
console.log('Restarting fork. New pid:', fork.process.pid)
}
})
function sig(cmd, value) {
if (cmd !== 'upgrade') return broadcast()
var mpath = path.resolve(value)
fs.stat(mpath, function(err, stat) {
if (err) return reply(cmd, err)
writeHEAD(mpath)
broadcast()
})
function broadcast() {
var keys = Object.keys(cluster.workers)
keys.forEach(function(id, index) {
var worker = cluster.workers[id]
if (cmd === 'message') {
worker.send(value)
}
else if (cmd === 'upgrade' && opts['zero-downtime']) {
if (index % 2 === 0 && !isLast(index)) {
cluster.fork()
setImmediate(function() {
disconnect(worker)
})
}
else {
disconnect(worker, function() {
cluster.fork()
})
}
}
else if (cmd === 'upgrade' || cmd === 'kill') {
worker.kill()
cluster.fork()
}
else if (cmd === 'die') {
worker.kill()
if (isLast(index)) {
reply(cmd)
process.kill()
}
}
})
reply(cmd)
function disconnect(worker, cb) {
worker.disconnect()
var timeout = setTimeout(function() {
worker.kill()
cb && cb()
}, 60000)
worker.on('disconnect', function() {
clearTimeout(timeout)
worker.kill()
cb && cb()
})
}
function isLast(i) { return i === keys.length - 1 }
}
}
//
// create a server to listen for out of process
// instructions to kill, disconnect or message workers.
//
var server = net.createServer(function(conn) {
var cmd
function onReply(d) {
conn.write(JSON.stringify(d) + '\n')
}
ee.on('reply', onReply)
conn.on('close', function() { ee.removeListener('reply', onReply) })
conn.on('error', function(err) { reply(cmd, err) })
conn
.pipe(split())
.pipe(parse())
.pipe(through(function(data) {
if (!data || !data.cmd) return
cmd = data.cmd
switch(cmd) {
case 'upgrade':
if (opts['pre-upgrade']) {
return require(opts['pre-upgrade'])(data, function(err, value) {
if (err) return reply(cmd, err)
sig('upgrade', value || data.value || opts.target)
})
}
sig('upgrade', data.value || opts.target)
break
case 'die':
sig('die')
break
case 'kill':
sig('kill')
break
case 'message':
if (data.value) {
sig('message', data.value)
}
break
}
}))
})
server.listen(opts.port || 3000, opts.address, function() {
console.log('Starting on port %d', opts.port)
})
var forks = opts.forks || numCPUs
for (var i = 0; i < forks; i++) {
cluster.fork()
}
}