-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathnode-graceful.ts
167 lines (136 loc) · 5.75 KB
/
node-graceful.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
// +----------------------------------------------------------------------+
// | node-graceful v3 (https://github.com/mrbar42/node-graceful) |
// | Graceful process exit manager. |
// |----------------------------------------------------------------------|
export type GracefulListener = (
signal: string,
details?: object,
) => void | any | Promise<any> | Promise<void> | Promise<Error>;
export type GracefulSubscription = () => void;
export class Graceful {
private static DEADLY_SIGNALS = ['SIGTERM', 'SIGINT', 'SIGBREAK', 'SIGHUP'];
public static exitOnDouble = true;
public static timeout = 30000;
private static _captureExceptions = false;
private static _captureRejections = false;
private static listeners: GracefulListener[] = [];
private static isRegistered = false;
private static isExiting = false;
private static exceptionListener = (event: any) => {
process.exitCode = 1;
Graceful.onDeadlyEvent('uncaughtException', event);
};
private static rejectionListener = (event: any) => {
process.exitCode = 1;
Graceful.onDeadlyEvent('unhandledRejection', event);
};
private static signalsListeners: {[signal: string]: (event: any) => void} = {};
public static get captureExceptions() {
return Graceful._captureExceptions;
}
public static set captureExceptions(newValue: boolean) {
if (Graceful._captureExceptions === newValue) return;
Graceful._captureExceptions = newValue;
if (Graceful._captureExceptions) {
process.on('uncaughtException' as any, Graceful.exceptionListener);
} else {
process.removeListener('uncaughtException', Graceful.exceptionListener);
}
}
public static get captureRejections() {
return Graceful._captureRejections;
}
public static set captureRejections(newValue: boolean) {
if (Graceful._captureRejections === newValue) return;
Graceful._captureRejections = newValue;
if (Graceful._captureRejections) {
process.on('unhandledRejection' as any, Graceful.rejectionListener);
} else {
process.removeListener('unhandledRejection', Graceful.rejectionListener);
}
}
public static on(signal: 'exit', listener: GracefulListener): GracefulSubscription {
if (signal !== 'exit') throw new Error("Only supports 'exit' signal");
Graceful.listeners.push(listener);
Graceful.updateRegistration();
return () => Graceful.off('exit', listener);
}
public static off(signal: 'exit', listener: GracefulListener) {
if (signal !== 'exit') throw new Error("Only supports 'exit' signal");
const index = Graceful.listeners.indexOf(listener);
if (index !== -1) Graceful.listeners.splice(index, 1);
Graceful.updateRegistration();
}
public static clear() {
Graceful.listeners.splice(0, Number.POSITIVE_INFINITY);
Graceful.updateRegistration();
}
public static exit(code?: number | string, signal = 'SIGTERM') {
const exitSignal = typeof code === 'string' ? code : signal;
if (typeof code === 'number') {
process.exitCode = code;
}
Graceful.onDeadlyEvent(exitSignal, {reason: 'Manual call to Graceful.exit()'});
}
private static onDeadlyEvent(signal: string, details?: object) {
// console.log(signal, details);
if (Graceful.isExiting) {
if (Graceful.exitOnDouble) Graceful.killProcess(true);
return;
}
const listeners = Graceful.listeners.slice(0);
Graceful.isExiting = true;
let completedListeners = 0;
const done = () => {
completedListeners++;
if (completedListeners === listeners.length) {
Graceful.killProcess(false);
}
};
if (Number(Graceful.timeout)) {
const timeoutRef = setTimeout(() => Graceful.killProcess(true), Graceful.timeout);
if (timeoutRef?.unref) timeoutRef.unref();
}
for (const listener of listeners) {
Graceful.invokeListener(listener, done, signal, details);
}
}
private static invokeListener(listener: GracefulListener, done: () => void, signal: string, details?: object) {
let invoked = false;
const listenerDone = () => {
if (!invoked) {
invoked = true;
done();
}
};
const retVal: any = listener(signal, details);
// allow returning a promise
if (retVal && typeof retVal.then === 'function') {
retVal.then(listenerDone, listenerDone);
} else {
listenerDone();
}
}
private static updateRegistration() {
if (Graceful.listeners.length > 0 && !Graceful.isRegistered) {
for (const deadlySignal of Graceful.DEADLY_SIGNALS) {
const listener = () => Graceful.onDeadlyEvent(deadlySignal);
Graceful.signalsListeners[deadlySignal] = listener;
process.on(deadlySignal as any, listener);
}
Graceful.isRegistered = true;
} else if (Graceful.listeners.length === 0 && Graceful.isRegistered) {
for (const deadlySignal of Graceful.DEADLY_SIGNALS) {
const listener = Graceful.signalsListeners[deadlySignal];
if (listener) {
process.removeListener(deadlySignal, listener);
delete Graceful.signalsListeners[deadlySignal];
}
}
Graceful.isRegistered = false;
}
}
private static killProcess(force: boolean) {
process.exit(process.exitCode || (force ? 1 : 0));
}
}