@n8io/baseline-shutdown 2.0.0 → 3.0.0
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/LICENSE +21 -0
- package/README.md +43 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +2 -2
- package/package.json +5 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Nate Clark
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
# @n8io/baseline-shutdown
|
|
2
|
+
|
|
3
|
+
Graceful shutdown in a fixed order: stop taking work, let running work finish, then close resources one by one.
|
|
4
|
+
|
|
5
|
+
```sh
|
|
6
|
+
pnpm add @n8io/baseline-shutdown
|
|
7
|
+
```
|
|
8
|
+
|
|
9
|
+
## Exports
|
|
10
|
+
|
|
11
|
+
- `shutdownHandleCreate({ intakes, resources, deadlineMs?, drainDelayMs? })`: owns `SIGTERM`, `SIGINT`, `uncaughtException` and `unhandledRejection`, and exits the process: 0 when every step closed in time, 1 otherwise or after a crash.
|
|
12
|
+
1. Waits `drainDelayMs` (default 5 s; pass 0 outside production) while readiness answers 503.
|
|
13
|
+
2. Closes every intake (HTTP server, live streams, Consumers) at once.
|
|
14
|
+
3. Closes each resource (queue, pools, clients, health, telemetry last) in order.
|
|
15
|
+
4. Gives up at `deadlineMs` (default 25 s), keeping the last 5 s for the resources.
|
|
16
|
+
- `ShutdownHandle`: `isShuttingDown()`, for `/readyz`.
|
|
17
|
+
- `ShutdownStep`: `{ name, close }`.
|
|
18
|
+
|
|
19
|
+
## Example
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { httpServerClose } from "@n8io/baseline-http";
|
|
23
|
+
import { shutdownHandleCreate } from "@n8io/baseline-shutdown";
|
|
24
|
+
import { telemetryShutdown } from "@n8io/baseline-telemetry";
|
|
25
|
+
|
|
26
|
+
const shutdown = shutdownHandleCreate({
|
|
27
|
+
drainDelayMs: envLocal ? 0 : 5_000,
|
|
28
|
+
intakes: [
|
|
29
|
+
{ close: () => httpServerClose(server), name: "http" },
|
|
30
|
+
{ close: () => liveHub.close(), name: "live" },
|
|
31
|
+
],
|
|
32
|
+
resources: [
|
|
33
|
+
{ close: () => queue.stop(), name: "queue" },
|
|
34
|
+
{ close: () => pgPool.end(), name: "database" },
|
|
35
|
+
{ close: () => health.close(), name: "health" },
|
|
36
|
+
{ close: () => telemetryShutdown(), name: "telemetry" },
|
|
37
|
+
],
|
|
38
|
+
});
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## Needs
|
|
42
|
+
|
|
43
|
+
Nothing beyond `@n8io/baseline-logger`, which it logs each failed step through. No env.
|
package/dist/index.d.ts
CHANGED
|
@@ -11,5 +11,5 @@ type ShutdownOptions = {
|
|
|
11
11
|
export type ShutdownHandle = {
|
|
12
12
|
isShuttingDown(): boolean;
|
|
13
13
|
};
|
|
14
|
-
export declare const
|
|
14
|
+
export declare const shutdownHandleCreate: ({ deadlineMs, drainDelayMs, intakes, resources }: ShutdownOptions) => ShutdownHandle;
|
|
15
15
|
export {};
|
package/dist/index.js
CHANGED
|
@@ -14,7 +14,7 @@ const stepClose = async (kind, step) => {
|
|
|
14
14
|
return false;
|
|
15
15
|
}
|
|
16
16
|
};
|
|
17
|
-
const
|
|
17
|
+
const shutdownHandleCreate = ({ deadlineMs = 25e3, drainDelayMs = 5e3, intakes, resources }) => {
|
|
18
18
|
let shuttingDown = false;
|
|
19
19
|
let crashed = false;
|
|
20
20
|
const shutdownRun = async (drainMs) => {
|
|
@@ -69,4 +69,4 @@ const shutdownHandle = ({ deadlineMs = 25e3, drainDelayMs = 5e3, intakes, resour
|
|
|
69
69
|
process.on("unhandledRejection", onCrash);
|
|
70
70
|
return { isShuttingDown: () => shuttingDown };
|
|
71
71
|
};
|
|
72
|
-
export {
|
|
72
|
+
export { shutdownHandleCreate };
|
package/package.json
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@n8io/baseline-shutdown",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "Graceful shutdown: drain, close intakes together, then resources in order.",
|
|
5
|
+
"license": "MIT",
|
|
5
6
|
"type": "module",
|
|
6
7
|
"engines": {
|
|
7
8
|
"node": ">=26"
|
|
@@ -18,14 +19,15 @@
|
|
|
18
19
|
}
|
|
19
20
|
},
|
|
20
21
|
"files": [
|
|
21
|
-
"dist"
|
|
22
|
+
"dist",
|
|
23
|
+
"README.md"
|
|
22
24
|
],
|
|
23
25
|
"publishConfig": {
|
|
24
26
|
"access": "public",
|
|
25
27
|
"registry": "https://registry.npmjs.org"
|
|
26
28
|
},
|
|
27
29
|
"dependencies": {
|
|
28
|
-
"@n8io/baseline-logger": "^2.
|
|
30
|
+
"@n8io/baseline-logger": "^2.1.0"
|
|
29
31
|
},
|
|
30
32
|
"scripts": {
|
|
31
33
|
"build": "tsdown",
|