Skip to content

Commit db24b16

Browse files
feat: gracefully and force shutdown (#3880)
1 parent 4bf8951 commit db24b16

File tree

2 files changed

+31
-2
lines changed

2 files changed

+31
-2
lines changed

lib/Server.js

+22-2
Original file line numberDiff line numberDiff line change
@@ -1114,11 +1114,31 @@ class Server {
11141114
if (this.options.setupExitSignals) {
11151115
const signals = ["SIGINT", "SIGTERM"];
11161116

1117+
let needForceShutdown = false;
1118+
1119+
const exitProcess = () => {
1120+
// eslint-disable-next-line no-process-exit
1121+
process.exit();
1122+
};
1123+
11171124
signals.forEach((signal) => {
11181125
process.on(signal, () => {
1126+
if (needForceShutdown) {
1127+
exitProcess();
1128+
}
1129+
1130+
this.logger.info(
1131+
"Gracefully shutting down. To force exit, press ^C again. Please wait..."
1132+
);
1133+
1134+
needForceShutdown = true;
1135+
11191136
this.stopCallback(() => {
1120-
// eslint-disable-next-line no-process-exit
1121-
process.exit();
1137+
if (typeof this.compiler.close === "function") {
1138+
this.compiler.close(exitProcess);
1139+
} else {
1140+
exitProcess();
1141+
}
11221142
});
11231143
});
11241144
});

test/server/setupExitSignals-option.test.js

+9
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ describe("setupExitSignals option", () => {
1111
let exitSpy;
1212
let stopCallbackSpy;
1313
let stdinResumeSpy;
14+
let closeCallbackSpy;
1415

1516
const signals = ["SIGINT", "SIGTERM"];
1617

@@ -36,6 +37,10 @@ describe("setupExitSignals option", () => {
3637
.spyOn(process.stdin, "resume")
3738
.mockImplementation(() => {});
3839
stopCallbackSpy = jest.spyOn(server, "stopCallback");
40+
41+
if (server.compiler.close) {
42+
closeCallbackSpy = jest.spyOn(server.compiler, "close");
43+
}
3944
});
4045

4146
afterEach(async () => {
@@ -57,6 +62,10 @@ describe("setupExitSignals option", () => {
5762
if (doExit) {
5863
expect(stopCallbackSpy.mock.calls.length).toEqual(1);
5964

65+
if (server.compiler.close) {
66+
expect(closeCallbackSpy.mock.calls.length).toEqual(1);
67+
}
68+
6069
clearInterval(interval);
6170

6271
resolve();

0 commit comments

Comments
 (0)