Skip to content

Commit

Permalink
feat: gracefully and force shutdown (#3880)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait authored Sep 24, 2021
1 parent 4bf8951 commit db24b16
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
24 changes: 22 additions & 2 deletions lib/Server.js
Original file line number Diff line number Diff line change
Expand Up @@ -1114,11 +1114,31 @@ class Server {
if (this.options.setupExitSignals) {
const signals = ["SIGINT", "SIGTERM"];

let needForceShutdown = false;

const exitProcess = () => {
// eslint-disable-next-line no-process-exit
process.exit();
};

signals.forEach((signal) => {
process.on(signal, () => {
if (needForceShutdown) {
exitProcess();
}

this.logger.info(
"Gracefully shutting down. To force exit, press ^C again. Please wait..."
);

needForceShutdown = true;

this.stopCallback(() => {
// eslint-disable-next-line no-process-exit
process.exit();
if (typeof this.compiler.close === "function") {
this.compiler.close(exitProcess);
} else {
exitProcess();
}
});
});
});
Expand Down
9 changes: 9 additions & 0 deletions test/server/setupExitSignals-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ describe("setupExitSignals option", () => {
let exitSpy;
let stopCallbackSpy;
let stdinResumeSpy;
let closeCallbackSpy;

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

Expand All @@ -36,6 +37,10 @@ describe("setupExitSignals option", () => {
.spyOn(process.stdin, "resume")
.mockImplementation(() => {});
stopCallbackSpy = jest.spyOn(server, "stopCallback");

if (server.compiler.close) {
closeCallbackSpy = jest.spyOn(server.compiler, "close");
}
});

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

if (server.compiler.close) {
expect(closeCallbackSpy.mock.calls.length).toEqual(1);
}

clearInterval(interval);

resolve();
Expand Down

0 comments on commit db24b16

Please sign in to comment.