-
Notifications
You must be signed in to change notification settings - Fork 568
/
base.ts
354 lines (313 loc) 路 9.74 KB
/
base.ts
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
import * as os from 'os';
import * as util from 'util';
import { logDate, sleep } from '../util';
import { EventEmitter, once } from 'events';
import { ForkOptions } from '../interface';
import type { IEventBus, EventBusOptions } from '@midwayjs/event-bus';
import { debuglog } from 'util';
const debug = debuglog('midway:bootstrap');
export abstract class AbstractForkManager<
Worker,
ClusterOptions extends ForkOptions
> {
private reforks = [];
private disconnectCount = 0;
private unexpectedCount = 0;
private disconnects = {};
private hub = new EventEmitter();
protected workers: Map<string, Worker> = new Map();
protected eventBus: IEventBus<Worker>;
private isClosing = false;
private exitListener: () => void;
protected constructor(readonly options: ClusterOptions) {
options.count = options.count || os.cpus().length - 1;
options.refork = options.refork !== false;
options.limit = options.limit || 60;
options.duration = options.duration || 60000; // 1 min
options.logger = options.logger || console;
options.workerInitTimeout = options.workerInitTimeout || 30000;
this.eventBus = this.createEventBus({
initTimeout: options.workerInitTimeout,
});
}
public async start() {
debug('Start manager with options: %j', this.options);
this.bindWorkerDisconnect(worker => {
debug(
' - worker(%s): trigger event = disconnect',
this.getWorkerId(worker)
);
const log =
this.options.logger[worker['disableRefork'] ? 'info' : 'error'];
this.disconnectCount++;
const isDead = this.isWorkerDead(worker);
if (isDead) {
debug(' - worker(%s): worker is dead', this.getWorkerId(worker));
// worker has terminated before disconnect
this.options.logger.info(
"[%s] [bootstrap:master:%s] don't fork, because worker:%s exit event emit before disconnect",
logDate(),
process.pid,
this.getWorkerId(worker)
);
return;
}
if (worker['disableRefork']) {
debug(
' - worker(%s): worker is disableRefork(maybe terminated by master)',
this.getWorkerId(worker)
);
// worker has terminated by master
log(
"[%s] [bootstrap:master:%s] don't fork, because worker:%s will be kill soon",
logDate(),
process.pid,
this.getWorkerId(worker)
);
return;
}
this.disconnects[this.getWorkerId(worker)] = logDate();
this.tryToRefork(worker);
});
this.bindWorkerExit((worker, code, signal) => {
debug(' - worker(%s): trigger event = exit', this.getWorkerId(worker));
// remove worker
this.workers.delete(this.getWorkerId(worker));
if (worker['disableRefork']) {
return;
}
const isExpected = !!this.disconnects[this.getWorkerId(worker)];
debug(
' - worker(%s): isExpected=%s',
this.getWorkerId(worker),
isExpected
);
if (isExpected) {
delete this.disconnects[this.getWorkerId(worker)];
// worker disconnect first, exit expected
return;
}
debug(
' - worker(%s): isWorkerDead=%s',
this.getWorkerId(worker),
this.isWorkerDead(worker)
);
if (this.isWorkerDead(worker)) {
return;
}
debug(' - worker(%s): unexpectedCount will add');
this.unexpectedCount++;
this.tryToRefork(worker);
this.onUnexpected(worker, code, signal);
});
this.bindClose();
this.hub.on('reachReforkLimit', this.onReachReforkLimit.bind(this));
// defer to set the listeners
// so you can listen this by your own
setImmediate(() => {
if (process.listeners('uncaughtException').length === 0) {
process.on('uncaughtException', this.onerror.bind(this));
}
});
for (let i = 0; i < this.options.count; i++) {
const w = this.createWorker();
debug(' - worker(%s) created', this.getWorkerId(w));
this.eventBus.addWorker(w);
this.workers.set(this.getWorkerId(w), w);
}
await this.eventBus.start();
}
protected tryToRefork(oldWorker: Worker) {
if (this.allowRefork()) {
debug(
' - worker(%s): allow refork and will fork new',
this.getWorkerId(oldWorker)
);
const newWorker = this.createWorker(oldWorker);
this.workers.set(this.getWorkerId(newWorker), newWorker);
this.options.logger.info(
'[%s] [bootstrap:master:%s] new worker:%s fork (state: %s)',
logDate(),
process.pid,
this.getWorkerId(newWorker),
newWorker['state']
);
this.eventBus.addWorker(newWorker);
} else {
debug(
' - worker(%s): forbidden refork and will stop',
this.getWorkerId(oldWorker)
);
this.options.logger.info(
"[%s] [bootstrap:master:%s] don't fork new work (refork: %s)",
logDate(),
process.pid,
this.options.refork
);
}
}
/**
* allow refork
*/
protected allowRefork() {
if (!this.options.refork || this.isClosing) {
return false;
}
const times = this.reforks.push(Date.now());
if (times > this.options.limit) {
this.reforks.shift();
}
const span = this.reforks[this.reforks.length - 1] - this.reforks[0];
const canFork =
this.reforks.length < this.options.limit || span > this.options.duration;
if (!canFork) {
this.hub.emit('reachReforkLimit');
}
return canFork;
}
/**
* uncaughtException default handler
*/
protected onerror(err) {
if (!err) {
return;
}
this.options.logger.error(
'[%s] [bootstrap:master:%s] master uncaughtException: %s',
logDate(),
process.pid,
err.stack
);
this.options.logger.error(err);
this.options.logger.error(
'(total %d disconnect, %d unexpected exit)',
this.disconnectCount,
this.unexpectedCount
);
}
/**
* unexpectedExit default handler
*/
protected onUnexpected(worker: Worker, code, signal) {
// eslint-disable-next-line no-prototype-builtins
const propertyName = worker.hasOwnProperty('exitedAfterDisconnect')
? 'exitedAfterDisconnect'
: 'suicide';
const err = new Error(
util.format(
'worker:%s died unexpected (code: %s, signal: %s, %s: %s, state: %s)',
this.getWorkerId(worker),
code,
signal,
propertyName,
worker[propertyName],
worker['state']
)
);
err.name = 'WorkerDiedUnexpectedError';
this.options.logger.error(
'[%s] [bootstrap:master:%s] (total %d disconnect, %d unexpected exit) %s',
logDate(),
process.pid,
this.disconnectCount,
this.unexpectedCount,
err.stack
);
}
/**
* reachReforkLimit default handler
*/
protected onReachReforkLimit() {
this.options.logger.error(
'[%s] [bootstrap:master:%s] worker died too fast (total %d disconnect, %d unexpected exit)',
logDate(),
process.pid,
this.disconnectCount,
this.unexpectedCount
);
}
protected async killWorker(worker, timeout) {
// kill process, if SIGTERM not work, try SIGKILL
await this.closeWorker(worker);
await Promise.race([once(worker, 'exit'), sleep(timeout)]);
if (worker.killed) return;
// SIGKILL: http://man7.org/linux/man-pages/man7/signal.7.html
// worker: https://github.com/nodejs/node/blob/master/lib/internal/cluster/worker.js#L22
// subProcess.kill is wrapped to subProcess.destroy, it will wait to disconnected.
(worker.process || worker).kill('SIGKILL');
}
public async stop(timeout = 2000) {
debug('run close');
this.isClosing = true;
await this.eventBus.stop();
for (const worker of this.workers.values()) {
worker['disableRefork'] = true;
await this.killWorker(worker, timeout);
}
if (this.exitListener) {
await this.exitListener();
}
}
public hasWorker(workerId: string): boolean {
return this.workers.has(workerId);
}
public getWorker(workerId: string): Worker {
return this.workers.get(workerId);
}
public getWorkerIds(): string[] {
return Array.from(this.workers.keys());
}
public onStop(exitListener) {
this.exitListener = exitListener;
}
protected bindClose() {
// kill(2) Ctrl-C
process.once('SIGINT', this.onSignal.bind(this, 'SIGINT'));
// kill(3) Ctrl-\
process.once('SIGQUIT', this.onSignal.bind(this, 'SIGQUIT'));
// kill(15) default
process.once('SIGTERM', this.onSignal.bind(this, 'SIGTERM'));
process.once('exit', this.onMasterExit.bind(this));
}
/**
* on bootstrap receive a exit signal
* @param signal
*/
private async onSignal(signal) {
if (!this.isClosing) {
this.options.logger.info(
'[bootstrap:master] receive signal %s, closing',
signal
);
try {
await this.stop();
this.options.logger.info(
'[bootstrap:master] close done, exiting with code:0'
);
process.exit(0);
} catch (err) {
this.options.logger.error('[midway:master] close with error: ', err);
process.exit(1);
}
}
}
/**
* on bootstrap process exit
* @param code
*/
private onMasterExit(code) {
this.options.logger.info('[bootstrap:master] exit with code:%s', code);
}
abstract createWorker(oldWorker?: Worker): Worker;
abstract bindWorkerDisconnect(listener: (worker: Worker) => void): void;
abstract bindWorkerExit(
listener: (worker: Worker, code, signal) => void
): void;
abstract getWorkerId(worker: Worker): string;
abstract isWorkerDead(worker: Worker): boolean;
abstract closeWorker(worker: Worker);
abstract createEventBus(
eventBusOptions: EventBusOptions<Worker>
): IEventBus<Worker>;
abstract isPrimary(): boolean;
}