Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: gracefully and force shutdown #3880

Merged
merged 4 commits into from
Sep 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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