This repository has been archived by the owner on Feb 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
213 lines (164 loc) · 5.28 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
var cluster = require('cluster')
, _ = require('lodash')
, async = require('async')
module.exports = function (options) {
var logger = options.logger
, workers = []
, restarting = false
, restartSignal = options.restartSignal || 'SIGUSR2'
var numWorkers = _.result(options, 'numWorkers', 2);
cluster.setupMaster({
exec: options.worker
})
logger.info({numWorkers: numWorkers}, 'Starting cluster')
async.times(numWorkers, function (n, next) {
newWorker(next)
}, function (err, workers) {
logger.info({workers: workers.length}, 'Cluster ready')
})
function restartAllWorkers(triggeringEvent) {
logger.info(triggeringEvent, 'Restart requested')
if (restarting) {
logger.warn('Restart already in process, ignoring this request')
return
}
restarting = true
async.each(_.filter(workers, {state: 'running'}), restartWorker, function (err) {
logger.info('Restart complete')
restarting = false
})
}
function shutdown(triggeringEvent) {
logger.info(triggeringEvent, 'Shutdown requested')
restarting = true
_.each(workers, function (worker) {
worker.state = 'stopping'
stopWorker(worker)
})
var timeout = setTimeout(function () {
logger.warn('Cluster did not shutdown cleanly within timeout, exiting')
process.exit(1)
}, 10000)
var cleanExit = function () {
if (_.every(workers, {state: 'exited'})) {
restarting = false
clearTimeout(timeout)
logger.info('Cluster shutdown cleanly')
process.exit(0)
return
}
logger.info('workers not yet exited, sleeping 250ms')
setTimeout(cleanExit, 250)
}
cluster.disconnect(cleanExit)
}
process.on(restartSignal, function () {
restartAllWorkers({signal: restartSignal})
})
process.on('SIGTERM', function () {
shutdown({signal: 'SIGTERM'})
})
// Fork a new worker and add it to the cluster
// invokes callback once the worker is safely online
function newWorker(callback) {
callback = callback || _.noop
logger.info('Creating new worker')
options.beforeFork && options.beforeFork()
var clusterWorker = cluster.fork(options.workerEnvironment)
var metaWorker = {
id: clusterWorker.id
, clusterWorker: clusterWorker
, logger: logger.child({
workerId: clusterWorker.id
, workerPid: clusterWorker.process.pid
})
, state: 'starting'
}
workers.push(metaWorker)
clusterWorker.once('listening', function () {
metaWorker.logger.info('Worker listening')
metaWorker.state = 'running'
callback(null)
})
clusterWorker.on('message', function (msg) {
if (msg.event === 'request-restart') {
if (metaWorker.state !== 'running') {
return
}
metaWorker.logger.info('Worker dying')
restartWorker(metaWorker)
}
})
clusterWorker.on('disconnect', function () {
metaWorker.logger.info('Worker disconnected')
if (metaWorker.state === 'starting') {
metaWorker.logger.warn('Worker disconnected unexpectedly while starting')
metaWorker.state = 'disconnected'
newWorker(callback)
}
if (metaWorker.state === 'running') {
metaWorker.logger.warn('Worker disconnection was unexpected')
metaWorker.state = 'disconnected'
newWorker()
}
metaWorker.state = 'disconnected'
stopWorker(metaWorker)
})
clusterWorker.on('exit', function (code, signal) {
metaWorker.logger.info({
code: code
, signal: signal
}, 'Worker exited')
if (metaWorker.killTimer) {
clearTimeout(metaWorker.killTimer)
metaWorker.killTimer = null
}
metaWorker.state = 'exited'
})
}
// Bring up a new worker, then gracefully close down the supplied worker
// then invoke the callback
function restartWorker(metaWorker, callback) {
metaWorker.logger.info('Restarting worker')
metaWorker.state = 'restarting'
newWorker(function () {
metaWorker.logger.info('Replacement worker available - disconnecting')
stopWorker(metaWorker)
callback && callback(null)
})
}
// Ensure a worker is dead or dying
function stopWorker(metaWorker) {
metaWorker.logger.debug({state: metaWorker.state}, 'Stopping worker')
function startKillTimer() {
return setTimeout(function () {
if (metaWorker.state === 'exited') return
metaWorker.logger.debug('Hard-killing worker as it did not die gracefully')
try {
metaWorker.clusterWorker.kill()
} catch (err) {
metaWorker.logger.debug(err, 'Could not kill with worker.kill()')
}
try {
metaWorker.clusterWorker.process.kill()
} catch (err) {
metaWorker.logger.debug(err, 'Could not kill with worker.process.kill()')
}
}, 5000)
}
switch (metaWorker.state) {
case 'disconnected':
metaWorker.logger.debug('Killing worker')
metaWorker.clusterWorker.kill()
break
case 'exited':
break
default:
metaWorker.logger.debug('Disconnecting worker')
metaWorker.clusterWorker.disconnect()
}
if (metaWorker.state !== 'exited') {
metaWorker.killTimer = startKillTimer()
}
}
}