Skip to content

Commit 3170f23

Browse files
committed
refactor: improve gracefulShutdown dx
1 parent 0973a59 commit 3170f23

File tree

1 file changed

+8
-20
lines changed

1 file changed

+8
-20
lines changed

src/_plugins.ts

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,38 +37,26 @@ export const gracefulShutdownPlugin: ServerPlugin = (server) => {
3737
const shutdown = async () => {
3838
if (isShuttingDown) return;
3939
isShuttingDown = true;
40-
console.log(
41-
c.gray(
42-
`\nShutting down server... (timeout in ${gracefulShutdown}+${forceShutdown}s)`,
43-
),
44-
);
40+
const w = process.stderr.write.bind(process.stderr);
41+
w(c.gray(`\nShutting down server in ${gracefulShutdown}s...`));
4542
let timeout: any;
4643
await Promise.race([
4744
// Graceful shutdown
4845
server.close().finally(() => {
4946
clearTimeout(timeout);
50-
console.log(c.green("Server closed all connections."));
47+
w(c.gray(" Server closed.\n"));
5148
}),
5249
new Promise<void>((resolve) => {
5350
timeout = setTimeout(() => {
5451
// Graceful shutdown timeout
55-
console.warn(
56-
c.yellow(
57-
`Forcing closing connections to exit... (timeout in ${forceShutdown}s)`,
58-
),
59-
);
52+
w(c.gray(`\nForce closing connections in ${forceShutdown}s...`));
6053
timeout = setTimeout(() => {
6154
// Force shutdown timeout
62-
console.error(
63-
c.red("Could not close connections in time, force exiting."),
64-
);
55+
w(c.red("\nCould not close connections in time, force exiting."));
6556
resolve();
66-
}, 1000);
67-
return server.close(true).finally(() => {
68-
clearTimeout(timeout);
69-
resolve();
70-
});
71-
}, 1000);
57+
}, forceShutdown * 1000);
58+
return server.close(true);
59+
}, gracefulShutdown * 1000);
7260
}),
7361
]);
7462
globalThis.process.exit(0);

0 commit comments

Comments
 (0)