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

Add the ability to have a sync shutdown #28

Merged
merged 2 commits into from
Jun 24, 2024
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
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,8 @@ where `State` is an enum that contains, `STARTING`, `READY`, `SHUTTING_DOWN` and
All of the below options are optional.

| Name | Type | Default | Description |
| ----------------- | :------------------------: | :-----: | ---------------------------------------------------------------: |
|-------------------|:--------------------------:|:-------:|-----------------------------------------------------------------:|
| syncClose | boolean | false | Run the closePromises in a series. |
| closePromises | (() => Promise<unknown>)[] | [] | The functions to run when the API is stopping |
| timeout | number | 1000 | The time in milliseconds to wait before shutting down the server |
| healthCheck | boolean | true | Enable/Disable the default endpoints (liveness and readiness) |
Expand Down
1 change: 1 addition & 0 deletions src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { IGracefulServerOptions } from "#interface/gracefulServerOptions";
import type { IOptions } from "#interface/options";

const options: IOptions = {
syncClose: false,
closePromises: [],
timeout: 1000,
healthCheck: true,
Expand Down
12 changes: 9 additions & 3 deletions src/core/improvedServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const improvedServer = <TServer extends Server>(server: TServer, serverStatus: I

stopping = true;

const { timeout, closePromises } = config;
const { timeout, closePromises, syncClose } = config;

let error: Error | undefined;
if (args.body && args.body.message) {
Expand All @@ -87,14 +87,20 @@ const improvedServer = <TServer extends Server>(server: TServer, serverStatus: I

await sleep(timeout);

await Promise.all(closePromises.map((closePromise) => closePromise()));
if (syncClose) {
for (const closePromise of closePromises) {
await closePromise();
}
} else {
await Promise.allSettled(closePromises.map((closePromise) => closePromise()));
}

server.removeAllListeners("request");
server.on("request", (_: http.IncomingMessage, res: http.ServerResponse) => {
if (!res.headersSent) res.setHeader("connection", "close");
});

await Promise.all([socketsPool.closeAll(), secureSocketsPool.closeAll()]);
await Promise.allSettled([socketsPool.closeAll(), secureSocketsPool.closeAll()]);

await new Promise((resolve, reject) => {
server.close((err) => {
Expand Down
1 change: 1 addition & 0 deletions src/interface/gracefulServerOptions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export type IGracefulServerOptions = {
syncClose?: boolean;
closePromises?: (() => Promise<unknown>)[];
timeout?: number;
healthCheck?: boolean;
Expand Down
1 change: 1 addition & 0 deletions src/interface/options.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export type IOptions = {
syncClose: boolean;
closePromises: (() => Promise<unknown>)[];
timeout: number;
healthCheck: boolean;
Expand Down
26 changes: 0 additions & 26 deletions src/util/shutdown.ts

This file was deleted.