@n8io/baseline-shutdown 3.1.0 → 3.2.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/README.md CHANGED
@@ -46,12 +46,16 @@ const shutdown = shutdownHandleCreate({
46
46
 
47
47
  <details><summary><b>Exports</b></summary>
48
48
 
49
- | Export | Does |
50
- | -------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
51
- | `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. A second signal is ignored. A `deadlineMs` that is not an integer over 5000, or a `drainDelayMs` that is not a whole number under `deadlineMs` minus 5000, throws a `TypeError`. |
52
- | `ShutdownOptions` | The options object above. `null` means the default, as leaving it out does. |
53
- | `ShutdownHandle` | `isShuttingDown()`, for `/readyz`. |
54
- | `ShutdownStep` | `{ name, close }`: one intake (HTTP server, live streams, Event handlers) or resource (queue, pools, clients, health, telemetry). |
49
+ | Export | Does |
50
+ | -------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
51
+ | `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. A second signal is ignored. A `deadlineMs` that is not an integer over 5000 and at most 2147483647, or a `drainDelayMs` that is not a whole number under `deadlineMs` minus 5000, throws a `TypeError`. |
52
+ | `ShutdownOptions` | The options object above. `null` means the default, as leaving it out does. |
53
+ | `ShutdownHandle` | `isShuttingDown()`, for `/readyz`. |
54
+ | `ShutdownStep` | `{ name, close }`: one intake (HTTP server, live streams, Event handlers) or resource (queue, pools, clients, health, telemetry). |
55
+
56
+ </details>
57
+
58
+ <details><summary><b>The steps</b></summary>
55
59
 
56
60
  The steps, counted from the signal:
57
61
 
@@ -59,6 +63,7 @@ The steps, counted from the signal:
59
63
  2. Closes every intake (HTTP server, live streams, Event handlers) at once. One still open at the cutoff (`deadlineMs` minus 5 s) is logged at warn, and shutdown moves on.
60
64
  3. Closes each resource (queue, pools, clients, health, telemetry last) in order. One still open at the deadline is logged at error, with every resource after it.
61
65
  4. Gives up at `deadlineMs` (default 25 s), keeping the last 5 s for the resources, and logs `Shutdown finished` with the exit code.
66
+
62
67
  </details>
63
68
 
64
- **Needs** no peers (it logs through `@n8io/baseline-logger`, a dependency); `SHUTDOWN_DRAIN_DELAY_MS` by the Template's convention (read by the caller, not this Package).
69
+ **Needs** `SHUTDOWN_DRAIN_DELAY_MS` by the Template's convention (read by the caller, not this Package); no peers.
package/dist/index.d.ts CHANGED
@@ -20,9 +20,9 @@ type ShutdownOptions = {
20
20
  */
21
21
  drainDelayMs?: number | null;
22
22
  /** Closed together; each stops accepting, then waits for its active work. */
23
- intakes: ShutdownStep[];
23
+ intakes: readonly ShutdownStep[];
24
24
  /** Closed one by one, in order, after every intake is done; telemetry last. */
25
- resources: ShutdownStep[];
25
+ resources: readonly ShutdownStep[];
26
26
  };
27
27
  /** The running shutdown handle. `isShuttingDown()` turns true at the signal, for `/readyz`. */
28
28
  type ShutdownHandle = {
@@ -31,7 +31,7 @@ type ShutdownHandle = {
31
31
  /**
32
32
  * Owns SIGTERM, SIGINT, uncaughtException and unhandledRejection, and exits the process: 0 when
33
33
  * every step closed in time, 1 otherwise or after a crash. Throws a `TypeError` on a
34
- * `deadlineMs` that is not an integer over 5000, or a `drainDelayMs` that is not a whole number
34
+ * `deadlineMs` that is not an integer over 5000 and at most 2147483647, or a `drainDelayMs` that is not a whole number
35
35
  * under `deadlineMs` minus 5000.
36
36
  */
37
37
  export declare const shutdownHandleCreate: ({ deadlineMs: deadlineMsGiven, drainDelayMs: drainDelayMsGiven, intakes, resources }: ShutdownOptions) => ShutdownHandle;
package/dist/index.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { logger } from "@n8io/baseline-logger";
2
2
  // src/shutdown-handle.ts
3
3
  const resourceReserveMs = 5e3;
4
+ const timerMax = 2147483647;
4
5
  const timedOut = Symbol("timedOut");
5
6
  const timeout = (ms) => new Promise((resolve) => {
6
7
  setTimeout(() => resolve(timedOut), Math.max(0, ms));
@@ -19,15 +20,15 @@ const drainDelayMsDefault = 5e3;
19
20
  /**
20
21
  * Owns SIGTERM, SIGINT, uncaughtException and unhandledRejection, and exits the process: 0 when
21
22
  * every step closed in time, 1 otherwise or after a crash. Throws a `TypeError` on a
22
- * `deadlineMs` that is not an integer over 5000, or a `drainDelayMs` that is not a whole number
23
+ * `deadlineMs` that is not an integer over 5000 and at most 2147483647, or a `drainDelayMs` that is not a whole number
23
24
  * under `deadlineMs` minus 5000.
24
25
  */
25
26
  const shutdownHandleCreate = ({ deadlineMs: deadlineMsGiven, drainDelayMs: drainDelayMsGiven, intakes, resources }) => {
26
27
  const deadlineMs = deadlineMsGiven ?? deadlineMsDefault;
27
28
  const drainDelayMs = drainDelayMsGiven ?? drainDelayMsDefault;
28
- if (!Number.isInteger(deadlineMs) || deadlineMs <= resourceReserveMs) throw new TypeError(`shutdownHandleCreate: deadlineMs must be an integer over ${resourceReserveMs}, not ${deadlineMs}`);
29
- if (!Number.isInteger(drainDelayMs) || drainDelayMs < 0) throw new TypeError(`shutdownHandleCreate: drainDelayMs must be a whole number, not ${drainDelayMs}`);
30
- if (drainDelayMs >= deadlineMs - resourceReserveMs) throw new TypeError(`shutdownHandleCreate: drainDelayMs must be under deadlineMs minus ${resourceReserveMs} (${deadlineMs - resourceReserveMs}), not ${drainDelayMs}`);
29
+ if (!Number.isInteger(deadlineMs) || deadlineMs <= resourceReserveMs || deadlineMs > timerMax) throw new TypeError(`shutdownHandleCreate: deadlineMs must be an integer over ${resourceReserveMs}, ${timerMax} at most, found ${deadlineMs}`);
30
+ if (!Number.isInteger(drainDelayMs) || drainDelayMs < 0) throw new TypeError(`shutdownHandleCreate: drainDelayMs must be a whole number, found ${drainDelayMs}`);
31
+ if (drainDelayMs >= deadlineMs - resourceReserveMs) throw new TypeError(`shutdownHandleCreate: drainDelayMs must be under deadlineMs minus ${resourceReserveMs} (${deadlineMs - resourceReserveMs}), found ${drainDelayMs}`);
31
32
  let shuttingDown = false;
32
33
  let crashed = false;
33
34
  const shutdownRun = async (drainMs) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@n8io/baseline-shutdown",
3
- "version": "3.1.0",
3
+ "version": "3.2.0",
4
4
  "description": "Graceful shutdown on SIGTERM: stop taking work, let running work finish, then close resources in order inside a deadline.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -27,7 +27,7 @@
27
27
  "registry": "https://registry.npmjs.org"
28
28
  },
29
29
  "dependencies": {
30
- "@n8io/baseline-logger": "^3.1.2"
30
+ "@n8io/baseline-logger": "^3.1.3"
31
31
  },
32
32
  "scripts": {
33
33
  "build": "tsdown",