@n8io/baseline-shutdown 0.0.0-stage → 2.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.
@@ -0,0 +1,15 @@
1
+ export type ShutdownStep = {
2
+ name: string;
3
+ close: () => Promise<void>;
4
+ };
5
+ type ShutdownOptions = {
6
+ deadlineMs?: number;
7
+ drainDelayMs?: number;
8
+ intakes: ShutdownStep[];
9
+ resources: ShutdownStep[];
10
+ };
11
+ export type ShutdownHandle = {
12
+ isShuttingDown(): boolean;
13
+ };
14
+ export declare const shutdownHandle: ({ deadlineMs, drainDelayMs, intakes, resources }: ShutdownOptions) => ShutdownHandle;
15
+ export {};
package/dist/index.js ADDED
@@ -0,0 +1,72 @@
1
+ import { logger } from "@n8io/baseline-logger";
2
+ // src/index.ts
3
+ const RESOURCE_RESERVE_MS = 5e3;
4
+ const TIMED_OUT = Symbol("timedOut");
5
+ const timeout = (ms) => new Promise((resolve) => {
6
+ setTimeout(() => resolve(TIMED_OUT), Math.max(0, ms));
7
+ });
8
+ const stepClose = async (kind, step) => {
9
+ try {
10
+ await step.close();
11
+ return true;
12
+ } catch (error) {
13
+ logger.error({ err: error }, `${kind} ${step.name} failed to close`);
14
+ return false;
15
+ }
16
+ };
17
+ const shutdownHandle = ({ deadlineMs = 25e3, drainDelayMs = 5e3, intakes, resources }) => {
18
+ let shuttingDown = false;
19
+ let crashed = false;
20
+ const shutdownRun = async (drainMs) => {
21
+ let clean = true;
22
+ const cutoff = timeout(deadlineMs - RESOURCE_RESERVE_MS);
23
+ const deadline = timeout(deadlineMs);
24
+ await Promise.race([timeout(drainMs), cutoff]);
25
+ const open = new Set(intakes);
26
+ const intakesDone = Promise.all(intakes.map(async (intake) => {
27
+ const closed = await stepClose("Intake", intake);
28
+ open.delete(intake);
29
+ return closed;
30
+ }));
31
+ const intakeResult = await Promise.race([intakesDone, cutoff]);
32
+ if (intakeResult === TIMED_OUT) {
33
+ clean = false;
34
+ for (const intake of open) logger.warn(`Intake ${intake.name} still open at the cutoff`);
35
+ } else if (intakeResult.includes(false)) clean = false;
36
+ for (const [index, resource] of resources.entries()) {
37
+ const closed = await Promise.race([stepClose("Resource", resource), deadline]);
38
+ if (closed === TIMED_OUT) {
39
+ clean = false;
40
+ for (const left of resources.slice(index)) logger.error(`Resource ${left.name} still open at the deadline`);
41
+ break;
42
+ }
43
+ if (!closed) clean = false;
44
+ }
45
+ return clean;
46
+ };
47
+ const shutdownExit = async (drainMs) => {
48
+ const code = await shutdownRun(drainMs).catch(() => false) && !crashed ? 0 : 1;
49
+ logger.info({ code }, "Shutdown finished");
50
+ process.exit(code);
51
+ };
52
+ const start = (drainMs) => {
53
+ if (shuttingDown) return false;
54
+ shuttingDown = true;
55
+ shutdownExit(drainMs);
56
+ return true;
57
+ };
58
+ const onSignal = (signal) => {
59
+ if (start(drainDelayMs)) logger.info(`Shutdown started on ${signal}`);
60
+ };
61
+ const onCrash = (error) => {
62
+ logger.fatal({ err: error }, "Crashed, shutting down");
63
+ crashed = true;
64
+ start(0);
65
+ };
66
+ process.on("SIGTERM", onSignal);
67
+ process.on("SIGINT", onSignal);
68
+ process.on("uncaughtException", onCrash);
69
+ process.on("unhandledRejection", onCrash);
70
+ return { isShuttingDown: () => shuttingDown };
71
+ };
72
+ export { shutdownHandle };
package/package.json CHANGED
@@ -1,6 +1,35 @@
1
1
  {
2
2
  "name": "@n8io/baseline-shutdown",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "2.0.0",
4
+ "description": "Graceful shutdown: drain, close intakes together, then resources in order.",
5
+ "type": "module",
6
+ "engines": {
7
+ "node": ">=26"
8
+ },
9
+ "repository": {
10
+ "type": "git",
11
+ "url": "git+https://github.com/n8io/baseline.git",
12
+ "directory": "packages/shutdown"
13
+ },
14
+ "exports": {
15
+ ".": {
16
+ "types": "./dist/index.d.ts",
17
+ "default": "./dist/index.js"
18
+ }
19
+ },
20
+ "files": [
21
+ "dist"
22
+ ],
23
+ "publishConfig": {
24
+ "access": "public",
25
+ "registry": "https://registry.npmjs.org"
26
+ },
27
+ "dependencies": {
28
+ "@n8io/baseline-logger": "^2.0.0"
29
+ },
30
+ "scripts": {
31
+ "build": "tsdown",
32
+ "check": "tsc --noEmit && pnpm pack --out dist/package.tgz && publint dist/package.tgz --strict && attw dist/package.tgz --profile esm-only",
33
+ "test": "vitest run"
34
+ }
6
35
  }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.