@bgord/bun 0.29.4 → 0.29.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/graceful-shutdown.service.d.ts +2 -5
- package/dist/graceful-shutdown.service.d.ts.map +1 -1
- package/dist/graceful-shutdown.service.js +18 -21
- package/dist/graceful-shutdown.service.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/src/graceful-shutdown.service.ts +20 -24
package/package.json
CHANGED
|
@@ -4,21 +4,16 @@ import { formatError } from "./logger-format-error.service";
|
|
|
4
4
|
|
|
5
5
|
type ServerType = ReturnType<typeof Bun.serve>;
|
|
6
6
|
|
|
7
|
-
type GracefulShutdownOptions = { exitFn?: (code: number) => never };
|
|
8
|
-
|
|
9
7
|
export class GracefulShutdown {
|
|
10
8
|
private readonly base = { operation: "shutdown", component: "infra" } as const;
|
|
11
|
-
private readonly exitFn: (code: number) => never;
|
|
12
9
|
private isShuttingDown = false;
|
|
13
10
|
|
|
14
11
|
constructor(
|
|
15
12
|
private readonly logger: LoggerPort,
|
|
16
|
-
|
|
17
|
-
) {
|
|
18
|
-
this.exitFn = options.exitFn ?? ((code: number) => process.exit(code));
|
|
19
|
-
}
|
|
13
|
+
private readonly exitFn: (code: number) => never = ((code: number) => process.exit(code)) as never,
|
|
14
|
+
) {}
|
|
20
15
|
|
|
21
|
-
private
|
|
16
|
+
private shutdown(server: ServerType, cleanup: () => any, exitCode: number) {
|
|
22
17
|
if (this.isShuttingDown) return;
|
|
23
18
|
this.isShuttingDown = true;
|
|
24
19
|
|
|
@@ -28,35 +23,36 @@ export class GracefulShutdown {
|
|
|
28
23
|
this.logger.error({ message: "Server stop failed", error: formatError(error), ...this.base });
|
|
29
24
|
}
|
|
30
25
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
this.logger.info({ message: "HTTP server closed", ...this.base })
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
26
|
+
Promise.resolve()
|
|
27
|
+
.then(() => cleanup())
|
|
28
|
+
.then(() => this.logger.info({ message: "HTTP server closed", ...this.base }))
|
|
29
|
+
.catch((error) =>
|
|
30
|
+
this.logger.error({ message: "Cleanup hook failed", error: formatError(error), ...this.base }),
|
|
31
|
+
)
|
|
32
|
+
.finally(() => {
|
|
33
|
+
this.exitFn(exitCode);
|
|
34
|
+
});
|
|
39
35
|
}
|
|
40
36
|
|
|
41
37
|
applyTo(server: ServerType, cleanup: () => any = tools.noop) {
|
|
42
|
-
process.once("SIGTERM",
|
|
38
|
+
process.once("SIGTERM", () => {
|
|
43
39
|
this.logger.info({ message: "SIGTERM received", ...this.base });
|
|
44
|
-
|
|
40
|
+
this.shutdown(server, cleanup, 0);
|
|
45
41
|
});
|
|
46
42
|
|
|
47
|
-
process.once("SIGINT",
|
|
43
|
+
process.once("SIGINT", () => {
|
|
48
44
|
this.logger.info({ message: "SIGINT received", ...this.base });
|
|
49
|
-
|
|
45
|
+
this.shutdown(server, cleanup, 0);
|
|
50
46
|
});
|
|
51
47
|
|
|
52
|
-
process.once("unhandledRejection",
|
|
48
|
+
process.once("unhandledRejection", (reason) => {
|
|
53
49
|
this.logger.error({ message: "UnhandledRejection received", error: formatError(reason), ...this.base });
|
|
54
|
-
|
|
50
|
+
this.shutdown(server, cleanup, 1);
|
|
55
51
|
});
|
|
56
52
|
|
|
57
|
-
process.once("uncaughtException",
|
|
53
|
+
process.once("uncaughtException", (error) => {
|
|
58
54
|
this.logger.error({ message: "UncaughtException received", error: formatError(error), ...this.base });
|
|
59
|
-
|
|
55
|
+
this.shutdown(server, cleanup, 1);
|
|
60
56
|
});
|
|
61
57
|
}
|
|
62
58
|
}
|