@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
|
@@ -2,8 +2,10 @@ import type { LoggerPort } from "./logger.port";
|
|
|
2
2
|
type ServerType = ReturnType<typeof Bun.serve>;
|
|
3
3
|
export declare class GracefulShutdown {
|
|
4
4
|
private readonly logger;
|
|
5
|
-
|
|
5
|
+
private readonly exitFn;
|
|
6
6
|
private readonly base;
|
|
7
|
+
private isShuttingDown;
|
|
8
|
+
constructor(logger: LoggerPort, exitFn?: (code: number) => never);
|
|
7
9
|
private shutdown;
|
|
8
10
|
applyTo(server: ServerType, cleanup?: () => any): void;
|
|
9
11
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"graceful-shutdown.service.d.ts","sourceRoot":"","sources":["../src/graceful-shutdown.service.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAGhD,KAAK,UAAU,GAAG,UAAU,CAAC,OAAO,GAAG,CAAC,KAAK,CAAC,CAAC;AAE/C,qBAAa,gBAAgB;
|
|
1
|
+
{"version":3,"file":"graceful-shutdown.service.d.ts","sourceRoot":"","sources":["../src/graceful-shutdown.service.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAGhD,KAAK,UAAU,GAAG,UAAU,CAAC,OAAO,GAAG,CAAC,KAAK,CAAC,CAAC;AAE/C,qBAAa,gBAAgB;IAKzB,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,MAAM;IALzB,OAAO,CAAC,QAAQ,CAAC,IAAI,CAA0D;IAC/E,OAAO,CAAC,cAAc,CAAS;gBAGZ,MAAM,EAAE,UAAU,EAClB,MAAM,GAAE,CAAC,IAAI,EAAE,MAAM,KAAK,KAAuD;IAGpG,OAAO,CAAC,QAAQ;IAqBhB,OAAO,CAAC,MAAM,EAAE,UAAU,EAAE,OAAO,GAAE,MAAM,GAAgB;CAqB5D"}
|
|
@@ -2,40 +2,47 @@ import * as tools from "@bgord/tools";
|
|
|
2
2
|
import { formatError } from "./logger-format-error.service";
|
|
3
3
|
export class GracefulShutdown {
|
|
4
4
|
logger;
|
|
5
|
-
|
|
5
|
+
exitFn;
|
|
6
|
+
base = { operation: "shutdown", component: "infra" };
|
|
7
|
+
isShuttingDown = false;
|
|
8
|
+
constructor(logger, exitFn = ((code) => process.exit(code))) {
|
|
6
9
|
this.logger = logger;
|
|
10
|
+
this.exitFn = exitFn;
|
|
7
11
|
}
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
12
|
+
shutdown(server, cleanup, exitCode) {
|
|
13
|
+
if (this.isShuttingDown)
|
|
14
|
+
return;
|
|
15
|
+
this.isShuttingDown = true;
|
|
11
16
|
try {
|
|
12
|
-
|
|
13
|
-
this.logger.info({ message: "HTTP server closed", ...this.base });
|
|
17
|
+
server.stop();
|
|
14
18
|
}
|
|
15
19
|
catch (error) {
|
|
16
|
-
this.logger.error({ message: "
|
|
20
|
+
this.logger.error({ message: "Server stop failed", error: formatError(error), ...this.base });
|
|
17
21
|
}
|
|
22
|
+
Promise.resolve()
|
|
23
|
+
.then(() => cleanup())
|
|
24
|
+
.then(() => this.logger.info({ message: "HTTP server closed", ...this.base }))
|
|
25
|
+
.catch((error) => this.logger.error({ message: "Cleanup hook failed", error: formatError(error), ...this.base }))
|
|
26
|
+
.finally(() => {
|
|
27
|
+
this.exitFn(exitCode);
|
|
28
|
+
});
|
|
18
29
|
}
|
|
19
30
|
applyTo(server, cleanup = tools.noop) {
|
|
20
|
-
process.once("SIGTERM",
|
|
31
|
+
process.once("SIGTERM", () => {
|
|
21
32
|
this.logger.info({ message: "SIGTERM received", ...this.base });
|
|
22
|
-
|
|
23
|
-
process.exitCode = 0;
|
|
33
|
+
this.shutdown(server, cleanup, 0);
|
|
24
34
|
});
|
|
25
|
-
process.once("SIGINT",
|
|
35
|
+
process.once("SIGINT", () => {
|
|
26
36
|
this.logger.info({ message: "SIGINT received", ...this.base });
|
|
27
|
-
|
|
28
|
-
process.exitCode = 0;
|
|
37
|
+
this.shutdown(server, cleanup, 0);
|
|
29
38
|
});
|
|
30
|
-
process.once("unhandledRejection",
|
|
31
|
-
this.logger.error({ message: "UnhandledRejection received", error: formatError(
|
|
32
|
-
|
|
33
|
-
process.exitCode = 1;
|
|
39
|
+
process.once("unhandledRejection", (reason) => {
|
|
40
|
+
this.logger.error({ message: "UnhandledRejection received", error: formatError(reason), ...this.base });
|
|
41
|
+
this.shutdown(server, cleanup, 1);
|
|
34
42
|
});
|
|
35
|
-
process.once("uncaughtException",
|
|
43
|
+
process.once("uncaughtException", (error) => {
|
|
36
44
|
this.logger.error({ message: "UncaughtException received", error: formatError(error), ...this.base });
|
|
37
|
-
|
|
38
|
-
process.exitCode = 1;
|
|
45
|
+
this.shutdown(server, cleanup, 1);
|
|
39
46
|
});
|
|
40
47
|
}
|
|
41
48
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"graceful-shutdown.service.js","sourceRoot":"","sources":["../src/graceful-shutdown.service.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,cAAc,CAAC;AAEtC,OAAO,EAAE,WAAW,EAAE,MAAM,+BAA+B,CAAC;AAI5D,MAAM,OAAO,gBAAgB;
|
|
1
|
+
{"version":3,"file":"graceful-shutdown.service.js","sourceRoot":"","sources":["../src/graceful-shutdown.service.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,cAAc,CAAC;AAEtC,OAAO,EAAE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