@bgord/bun 0.29.3 → 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 +3 -1
- package/dist/graceful-shutdown.service.d.ts.map +1 -1
- package/dist/graceful-shutdown.service.js +27 -20
- package/dist/graceful-shutdown.service.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +3 -3
- package/src/graceful-shutdown.service.ts +30 -21
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@bgord/bun",
|
|
3
|
-
"version": "0.29.
|
|
3
|
+
"version": "0.29.5",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"author": "Bartosz Gordon",
|
|
@@ -29,7 +29,7 @@
|
|
|
29
29
|
"@types/nodemailer": "7.0.1",
|
|
30
30
|
"@types/yazl": "3.3.0",
|
|
31
31
|
"cspell": "9.2.1",
|
|
32
|
-
"knip": "5.64.
|
|
32
|
+
"knip": "5.64.1",
|
|
33
33
|
"lefthook": "1.13.4",
|
|
34
34
|
"only-allow": "1.2.1",
|
|
35
35
|
"shellcheck": "4.1.0",
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
"croner": "9.1.0",
|
|
46
46
|
"csv": "6.4.1",
|
|
47
47
|
"hcaptcha": "0.2.0",
|
|
48
|
-
"hono": "4.9.
|
|
48
|
+
"hono": "4.9.9",
|
|
49
49
|
"lodash": "4.17.21",
|
|
50
50
|
"mime-types": "3.0.1",
|
|
51
51
|
"node-cache": "5.1.2",
|
|
@@ -5,45 +5,54 @@ import { formatError } from "./logger-format-error.service";
|
|
|
5
5
|
type ServerType = ReturnType<typeof Bun.serve>;
|
|
6
6
|
|
|
7
7
|
export class GracefulShutdown {
|
|
8
|
-
constructor(private readonly logger: LoggerPort) {}
|
|
9
|
-
|
|
10
8
|
private readonly base = { operation: "shutdown", component: "infra" } as const;
|
|
9
|
+
private isShuttingDown = false;
|
|
11
10
|
|
|
12
|
-
|
|
13
|
-
|
|
11
|
+
constructor(
|
|
12
|
+
private readonly logger: LoggerPort,
|
|
13
|
+
private readonly exitFn: (code: number) => never = ((code: number) => process.exit(code)) as never,
|
|
14
|
+
) {}
|
|
14
15
|
|
|
15
|
-
|
|
16
|
-
|
|
16
|
+
private shutdown(server: ServerType, cleanup: () => any, exitCode: number) {
|
|
17
|
+
if (this.isShuttingDown) return;
|
|
18
|
+
this.isShuttingDown = true;
|
|
17
19
|
|
|
18
|
-
|
|
20
|
+
try {
|
|
21
|
+
server.stop();
|
|
19
22
|
} catch (error) {
|
|
20
|
-
this.logger.error({ message: "
|
|
23
|
+
this.logger.error({ message: "Server stop failed", error: formatError(error), ...this.base });
|
|
21
24
|
}
|
|
25
|
+
|
|
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
|
+
});
|
|
22
35
|
}
|
|
23
36
|
|
|
24
37
|
applyTo(server: ServerType, cleanup: () => any = tools.noop) {
|
|
25
|
-
process.once("SIGTERM",
|
|
38
|
+
process.once("SIGTERM", () => {
|
|
26
39
|
this.logger.info({ message: "SIGTERM received", ...this.base });
|
|
27
|
-
|
|
28
|
-
process.exitCode = 0;
|
|
40
|
+
this.shutdown(server, cleanup, 0);
|
|
29
41
|
});
|
|
30
42
|
|
|
31
|
-
process.once("SIGINT",
|
|
43
|
+
process.once("SIGINT", () => {
|
|
32
44
|
this.logger.info({ message: "SIGINT received", ...this.base });
|
|
33
|
-
|
|
34
|
-
process.exitCode = 0;
|
|
45
|
+
this.shutdown(server, cleanup, 0);
|
|
35
46
|
});
|
|
36
47
|
|
|
37
|
-
process.once("unhandledRejection",
|
|
38
|
-
this.logger.error({ message: "UnhandledRejection received", error: formatError(
|
|
39
|
-
|
|
40
|
-
process.exitCode = 1;
|
|
48
|
+
process.once("unhandledRejection", (reason) => {
|
|
49
|
+
this.logger.error({ message: "UnhandledRejection received", error: formatError(reason), ...this.base });
|
|
50
|
+
this.shutdown(server, cleanup, 1);
|
|
41
51
|
});
|
|
42
52
|
|
|
43
|
-
process.once("uncaughtException",
|
|
53
|
+
process.once("uncaughtException", (error) => {
|
|
44
54
|
this.logger.error({ message: "UncaughtException received", error: formatError(error), ...this.base });
|
|
45
|
-
|
|
46
|
-
process.exitCode = 1;
|
|
55
|
+
this.shutdown(server, cleanup, 1);
|
|
47
56
|
});
|
|
48
57
|
}
|
|
49
58
|
}
|