target-supabase-sdk 0.2.2 → 0.2.4
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 +24 -18
- package/dist/browser.d.ts +11 -4
- package/dist/browser.d.ts.map +1 -1
- package/dist/browser.js +3 -3
- package/dist/chunks/{enable-log-persist-BH-tbKEj.js → enable-log-persist-B8rg_wXG.js} +41 -8
- package/dist/chunks/enable-log-persist-B8rg_wXG.js.map +1 -0
- package/dist/chunks/{create-logger-DyH3mZ8p.js → list.api-Ab9v8yOg.js} +66 -57
- package/dist/chunks/list.api-Ab9v8yOg.js.map +1 -0
- package/dist/chunks/{word.interface-DLgRobKV.js → word.interface-B_fbVFOW.js} +1078 -97
- package/dist/chunks/word.interface-B_fbVFOW.js.map +1 -0
- package/dist/extraction/extraction.api.d.ts +117 -0
- package/dist/extraction/extraction.api.d.ts.map +1 -0
- package/dist/extraction/extraction.interface.d.ts +43 -0
- package/dist/extraction/extraction.interface.d.ts.map +1 -0
- package/dist/list/list.interface.d.ts +2 -2
- package/dist/list/list.interface.d.ts.map +1 -1
- package/dist/node/env/index.d.ts +2 -2
- package/dist/node/env/index.d.ts.map +1 -1
- package/dist/node/env/init-supabase-from-env.d.ts +1 -1
- package/dist/node/env/init-supabase-from-env.d.ts.map +1 -1
- package/dist/node/env/load-env.d.ts +15 -1
- package/dist/node/env/load-env.d.ts.map +1 -1
- package/dist/node/node-runtime.base.d.ts +13 -1
- package/dist/node/node-runtime.base.d.ts.map +1 -1
- package/dist/node/preload/run-service-preload.d.ts +1 -1
- package/dist/node/preload/run-service-preload.d.ts.map +1 -1
- package/dist/node/process/create-managed-child-processes.d.ts +12 -0
- package/dist/node/process/create-managed-child-processes.d.ts.map +1 -0
- package/dist/node/process/index.d.ts +3 -1
- package/dist/node/process/index.d.ts.map +1 -1
- package/dist/node/process/managed-child-processes.d.ts +15 -2
- package/dist/node/process/managed-child-processes.d.ts.map +1 -1
- package/dist/node/runtime-state/create-service-runtime-state-store.d.ts +23 -0
- package/dist/node/runtime-state/create-service-runtime-state-store.d.ts.map +1 -0
- package/dist/node/runtime-state/index.d.ts +4 -0
- package/dist/node/runtime-state/index.d.ts.map +1 -0
- package/dist/node/runtime-state/service-runtime-state.types.d.ts +57 -0
- package/dist/node/runtime-state/service-runtime-state.types.d.ts.map +1 -0
- package/dist/node/service-guard/index.d.ts +9 -0
- package/dist/node/service-guard/index.d.ts.map +1 -0
- package/dist/node/service-guard/register-service-guard-runner.d.ts +9 -0
- package/dist/node/service-guard/register-service-guard-runner.d.ts.map +1 -0
- package/dist/node/service-guard/run-readiness-gate.d.ts +11 -0
- package/dist/node/service-guard/run-readiness-gate.d.ts.map +1 -0
- package/dist/node/service-guard/run-service-guard-tick.d.ts +7 -0
- package/dist/node/service-guard/run-service-guard-tick.d.ts.map +1 -0
- package/dist/node/service-guard/service-guard-node.d.ts +18 -0
- package/dist/node/service-guard/service-guard-node.d.ts.map +1 -0
- package/dist/node/service-guard/service-guard.interface.d.ts +58 -0
- package/dist/node/service-guard/service-guard.interface.d.ts.map +1 -0
- package/dist/node/service-guard/worker-spawn-cooldown.d.ts +4 -0
- package/dist/node/service-guard/worker-spawn-cooldown.d.ts.map +1 -0
- package/dist/node/service-host/index.d.ts +9 -0
- package/dist/node/service-host/index.d.ts.map +1 -0
- package/dist/node/service-host/registry-slot-guard-step.d.ts +21 -0
- package/dist/node/service-host/registry-slot-guard-step.d.ts.map +1 -0
- package/dist/node/service-host/service-host.d.ts +36 -0
- package/dist/node/service-host/service-host.d.ts.map +1 -0
- package/dist/node/service-host/single-process-service.d.ts +21 -0
- package/dist/node/service-host/single-process-service.d.ts.map +1 -0
- package/dist/node.d.ts +17 -4
- package/dist/node.d.ts.map +1 -1
- package/dist/node.js +2088 -1495
- package/dist/node.js.map +1 -1
- package/dist/preload.js +5 -4
- package/dist/preload.js.map +1 -1
- package/dist/service/config.api.d.ts +143 -0
- package/dist/service/config.api.d.ts.map +1 -0
- package/dist/service/config.interface.d.ts +22 -0
- package/dist/service/config.interface.d.ts.map +1 -0
- package/dist/service/index.d.ts +16 -17
- package/dist/service/index.d.ts.map +1 -1
- package/dist/service/registry-lifecycle.d.ts +43 -0
- package/dist/service/registry-lifecycle.d.ts.map +1 -0
- package/dist/service/registry.service.d.ts +94 -0
- package/dist/service/registry.service.d.ts.map +1 -0
- package/dist/service/service-bootstrap.d.ts +28 -0
- package/dist/service/service-bootstrap.d.ts.map +1 -0
- package/dist/service/service.api.d.ts +160 -0
- package/dist/service/service.api.d.ts.map +1 -1
- package/dist/service/service.interface.d.ts +32 -1
- package/dist/service/service.interface.d.ts.map +1 -1
- package/dist/shared/log/index.d.ts +4 -0
- package/dist/shared/log/index.d.ts.map +1 -1
- package/dist/shared/log/log-batch.d.ts +44 -0
- package/dist/shared/log/log-batch.d.ts.map +1 -0
- package/dist/shared/log/log-persist-coordinator.d.ts +37 -0
- package/dist/shared/log/log-persist-coordinator.d.ts.map +1 -0
- package/dist/shared/log/log-persist-flush.d.ts.map +1 -1
- package/dist/shared/log/log-persist.config.d.ts +0 -1
- package/dist/shared/log/log-persist.config.d.ts.map +1 -1
- package/dist/shared/log/log-persist.interface.d.ts +8 -0
- package/dist/shared/log/log-persist.interface.d.ts.map +1 -1
- package/dist/shared/utils/config-path.utils.d.ts +21 -0
- package/dist/shared/utils/config-path.utils.d.ts.map +1 -1
- package/dist/shared/utils/get-value-at-path.d.ts +3 -0
- package/dist/shared/utils/get-value-at-path.d.ts.map +1 -0
- package/dist/task/index.d.ts +2 -0
- package/dist/task/index.d.ts.map +1 -1
- package/dist/task/task-manager.d.ts +1 -1
- package/dist/task/task-manager.d.ts.map +1 -1
- package/dist/task/task-post-retry.api.d.ts +24 -0
- package/dist/task/task-post-retry.api.d.ts.map +1 -0
- package/dist/task/task-repo-context.d.ts +7 -1
- package/dist/task/task-repo-context.d.ts.map +1 -1
- package/dist/trigger/trigger-manager.d.ts +1 -1
- package/dist/trigger/trigger-manager.d.ts.map +1 -1
- package/dist/trigger/trigger-node.d.ts +1 -0
- package/dist/trigger/trigger-node.d.ts.map +1 -1
- package/dist/trigger/trigger.constant.d.ts +1 -1
- package/dist/trigger/trigger.constant.d.ts.map +1 -1
- package/dist/trigger/trigger.interface.d.ts +10 -0
- package/dist/trigger/trigger.interface.d.ts.map +1 -1
- package/package.json +4 -2
- package/src/browser.ts +98 -4
- package/src/extraction/extraction.api.ts +232 -0
- package/src/extraction/extraction.interface.ts +45 -0
- package/src/list/list.interface.ts +2 -2
- package/src/node/env/index.ts +13 -2
- package/src/node/env/init-supabase-from-env.ts +1 -1
- package/src/node/env/load-env.ts +48 -2
- package/src/node/node-runtime.base.ts +37 -6
- package/src/node/preload/run-service-preload.ts +3 -2
- package/src/node/process/create-managed-child-processes.ts +27 -0
- package/src/node/process/index.ts +4 -0
- package/src/node/process/managed-child-processes.ts +48 -15
- package/src/node/runtime-state/create-service-runtime-state-store.ts +125 -0
- package/src/node/runtime-state/index.ts +17 -0
- package/src/node/runtime-state/service-runtime-state.types.ts +64 -0
- package/src/node/service-guard/index.ts +17 -0
- package/src/node/service-guard/register-service-guard-runner.ts +64 -0
- package/src/node/service-guard/run-readiness-gate.ts +34 -0
- package/src/node/service-guard/run-service-guard-tick.ts +106 -0
- package/src/node/service-guard/service-guard-node.ts +52 -0
- package/src/node/service-guard/service-guard.interface.ts +64 -0
- package/src/node/service-guard/worker-spawn-cooldown.ts +13 -0
- package/src/node/service-host/index.ts +30 -0
- package/src/node/service-host/registry-slot-guard-step.ts +57 -0
- package/src/node/service-host/service-host.ts +168 -0
- package/src/node/service-host/single-process-service.ts +58 -0
- package/src/node.ts +76 -0
- package/src/service/config.api.ts +224 -0
- package/src/service/config.interface.ts +25 -0
- package/src/service/index.ts +74 -149
- package/src/service/registry-lifecycle.ts +144 -0
- package/src/service/registry.service.ts +564 -0
- package/src/service/service-bootstrap.ts +74 -0
- package/src/service/service.api.ts +13 -0
- package/src/service/service.interface.ts +36 -1
- package/src/shared/log/index.ts +13 -0
- package/src/shared/log/log-batch.ts +156 -0
- package/src/shared/log/log-persist-coordinator.ts +107 -0
- package/src/shared/log/log-persist-flush.ts +3 -3
- package/src/shared/log/log-persist.config.ts +0 -2
- package/src/shared/log/log-persist.interface.ts +10 -0
- package/src/shared/utils/config-path.utils.ts +35 -0
- package/src/shared/utils/get-value-at-path.ts +17 -0
- package/src/task/index.ts +2 -0
- package/src/task/task-manager.ts +10 -4
- package/src/task/task-post-retry.api.ts +61 -0
- package/src/task/task-repo-context.ts +8 -1
- package/src/trigger/trigger-manager.ts +4 -2
- package/src/trigger/trigger-node.ts +10 -4
- package/src/trigger/trigger.constant.ts +1 -1
- package/src/trigger/trigger.interface.ts +10 -0
- package/dist/chunks/create-logger-DyH3mZ8p.js.map +0 -1
- package/dist/chunks/enable-log-persist-BH-tbKEj.js.map +0 -1
- package/dist/chunks/word.interface-DLgRobKV.js.map +0 -1
|
@@ -21,6 +21,16 @@ export interface NodeLoopContext {
|
|
|
21
21
|
nodeId: string;
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
+
/** Shared options for {@link BaseNodeRuntime} and subclasses (e.g. TriggerNode). */
|
|
25
|
+
export interface BaseNodeRuntimeOptions {
|
|
26
|
+
/**
|
|
27
|
+
* Called after node logout (or when the node was never registered),
|
|
28
|
+
* immediately before `process.exit`. Use for best-effort cleanup such as
|
|
29
|
+
* {@link unregisterServiceAtShutdown}. Errors are logged and ignored.
|
|
30
|
+
*/
|
|
31
|
+
beforeProcessExit?: () => void | Promise<void>;
|
|
32
|
+
}
|
|
33
|
+
|
|
24
34
|
export const LOG_TOPIC_NODE = "node";
|
|
25
35
|
const LOG_TOPIC_COMMAND = "command";
|
|
26
36
|
const LOG_TOPIC_PROCESS = "process";
|
|
@@ -35,6 +45,7 @@ abstract class BaseNodeRuntime {
|
|
|
35
45
|
private static readonly NODE_ID_PENDING_ASSIGNMENT = "pending-node-id";
|
|
36
46
|
|
|
37
47
|
protected readonly runtimeModule: string;
|
|
48
|
+
private readonly beforeProcessExitHook: (() => void | Promise<void>) | null;
|
|
38
49
|
|
|
39
50
|
private _localNodeId: string | null = null;
|
|
40
51
|
private _isNodeIdLocked = false;
|
|
@@ -63,14 +74,34 @@ abstract class BaseNodeRuntime {
|
|
|
63
74
|
this._isNodeIdLocked = true;
|
|
64
75
|
}
|
|
65
76
|
|
|
66
|
-
protected constructor(runtimeModule: string) {
|
|
77
|
+
protected constructor(runtimeModule: string, options?: BaseNodeRuntimeOptions) {
|
|
67
78
|
this.runtimeModule = runtimeModule;
|
|
79
|
+
this.beforeProcessExitHook = options?.beforeProcessExit ?? null;
|
|
68
80
|
this.isRunning = false;
|
|
69
81
|
this.loopCount = 0;
|
|
70
82
|
this.startupTraceId = logManager.generateTraceId();
|
|
71
83
|
this.startupScope = createScope({ module: runtimeModule, traceId: this.startupTraceId });
|
|
72
84
|
}
|
|
73
85
|
|
|
86
|
+
private async runBeforeProcessExit(logger: LoggerWithScope): Promise<void> {
|
|
87
|
+
if (this.beforeProcessExitHook == null) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
try {
|
|
91
|
+
await this.beforeProcessExitHook();
|
|
92
|
+
} catch (error) {
|
|
93
|
+
logger.warn("beforeProcessExit 钩子失败(best-effort)", {
|
|
94
|
+
topic: LOG_TOPIC_PROCESS,
|
|
95
|
+
data: { error: getErrorMessage(error) },
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
private async exitProcess(code: number, logger: LoggerWithScope): Promise<never> {
|
|
101
|
+
await this.runBeforeProcessExit(logger);
|
|
102
|
+
process.exit(code);
|
|
103
|
+
}
|
|
104
|
+
|
|
74
105
|
/** Hook: task registration, trigger config load, etc. Throw to abort bootstrap. */
|
|
75
106
|
protected abstract onBeforeRegisterNode(logger: LoggerWithScope): Promise<void>;
|
|
76
107
|
|
|
@@ -296,7 +327,7 @@ abstract class BaseNodeRuntime {
|
|
|
296
327
|
}
|
|
297
328
|
|
|
298
329
|
this.isRunning = false;
|
|
299
|
-
this.shutdownPromise = this.runShutdown(options?.trigger).catch((error) => {
|
|
330
|
+
this.shutdownPromise = this.runShutdown(options?.trigger).catch(async (error) => {
|
|
300
331
|
const logger = createLogger({
|
|
301
332
|
scope: patchScope({ scope: this.startupScope, patch: { labels: { nodeId: this.getLogNodeId() } } }),
|
|
302
333
|
});
|
|
@@ -307,7 +338,7 @@ abstract class BaseNodeRuntime {
|
|
|
307
338
|
error: getErrorMessage(error),
|
|
308
339
|
},
|
|
309
340
|
});
|
|
310
|
-
|
|
341
|
+
await this.exitProcess(1, logger);
|
|
311
342
|
});
|
|
312
343
|
return this.shutdownPromise;
|
|
313
344
|
}
|
|
@@ -324,7 +355,7 @@ abstract class BaseNodeRuntime {
|
|
|
324
355
|
|
|
325
356
|
if (this.localNodeId == null) {
|
|
326
357
|
logger.warn("本地節點未註冊,進程退出", { topic: LOG_TOPIC_NODE });
|
|
327
|
-
|
|
358
|
+
await this.exitProcess(0, logger);
|
|
328
359
|
return;
|
|
329
360
|
}
|
|
330
361
|
|
|
@@ -344,7 +375,7 @@ abstract class BaseNodeRuntime {
|
|
|
344
375
|
data: { error: getErrorMessage(error) },
|
|
345
376
|
});
|
|
346
377
|
} finally {
|
|
347
|
-
|
|
378
|
+
await this.exitProcess(0, logger);
|
|
348
379
|
}
|
|
349
380
|
}
|
|
350
381
|
|
|
@@ -391,7 +422,7 @@ abstract class BaseNodeRuntime {
|
|
|
391
422
|
}
|
|
392
423
|
|
|
393
424
|
this.consecutiveHeartbeatFailures = 0;
|
|
394
|
-
logger.
|
|
425
|
+
logger.info("心跳更新成功", {
|
|
395
426
|
topic: LOG_TOPIC_NODE,
|
|
396
427
|
data: { lastHeartBeat: formatHeartbeat(lastHeartBeat) },
|
|
397
428
|
});
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { resolve } from "node:path";
|
|
2
2
|
import { validateLogPersistPreloadEnv } from "../../shared/log/enable-log-persist";
|
|
3
3
|
import { logPersistEnabledFromEnv } from "../../shared/log/log-persist.config";
|
|
4
|
-
import { loadEnvFiles } from "../env/load-env";
|
|
4
|
+
import { loadEnvFiles, pinEnvProfileFromArgv } from "../env/load-env";
|
|
5
5
|
import { resolveProjectRootByPackageName } from "../env/project-root";
|
|
6
6
|
import type { ServicePreloadOptions } from "./service-preload.interface";
|
|
7
7
|
|
|
@@ -26,7 +26,7 @@ function applyServiceEnvDefaults(projectRoot: string, options: ServicePreloadOpt
|
|
|
26
26
|
* Unified Node `--import` preload runner (sync only).
|
|
27
27
|
*
|
|
28
28
|
* Phase 1 — Resolve project root from service package name
|
|
29
|
-
* Phase 2 — Load `.env.local` / `.env`
|
|
29
|
+
* Phase 2 — Load env files (`.env.local` / `.env`, or `.env.prod` when `--prod`)
|
|
30
30
|
* Phase 3 — Apply SDK service env defaults + optional L3 hook
|
|
31
31
|
* Phase 4 — Validate log-persist env when enabled
|
|
32
32
|
* Phase 5 — Process diagnostics (TODO)
|
|
@@ -36,6 +36,7 @@ function applyServiceEnvDefaults(projectRoot: string, options: ServicePreloadOpt
|
|
|
36
36
|
export function runServicePreload(options: ServicePreloadOptions): void {
|
|
37
37
|
const projectRoot = resolveProjectRootByPackageName(options.callerImportMetaUrl, options.packageName);
|
|
38
38
|
|
|
39
|
+
pinEnvProfileFromArgv();
|
|
39
40
|
loadEnvFiles(projectRoot, { afterLoad: options.afterLoadEnv });
|
|
40
41
|
|
|
41
42
|
applyServiceEnvDefaults(projectRoot, options);
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { createLogger } from "../../shared/log/create-logger";
|
|
2
|
+
import type { ManagedChildProcessesOptions } from "./managed-child-processes";
|
|
3
|
+
import { ManagedChildProcesses } from "./managed-child-processes";
|
|
4
|
+
|
|
5
|
+
export type CreateManagedChildProcessesOptions = Pick<
|
|
6
|
+
ManagedChildProcessesOptions,
|
|
7
|
+
"projectRoot" | "preloadModules" | "useTsx" | "graceMs"
|
|
8
|
+
> & {
|
|
9
|
+
/** Defaults to `createLogger({ module: "launcher" })`. */
|
|
10
|
+
logger?: ManagedChildProcessesOptions["logger"];
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
const DEFAULT_PRELOAD_MODULES = ["./scripts/preload.mjs"] as const;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Standard L3 service child-process registry: node (not tsx), service preload, launcher logger.
|
|
17
|
+
* Each service keeps a singleton in `src/startup/child-processes.ts`.
|
|
18
|
+
*/
|
|
19
|
+
export function createManagedChildProcesses(options: CreateManagedChildProcessesOptions): ManagedChildProcesses {
|
|
20
|
+
return new ManagedChildProcesses({
|
|
21
|
+
projectRoot: options.projectRoot,
|
|
22
|
+
preloadModules: options.preloadModules ?? DEFAULT_PRELOAD_MODULES,
|
|
23
|
+
useTsx: options.useTsx ?? false,
|
|
24
|
+
graceMs: options.graceMs,
|
|
25
|
+
logger: options.logger ?? createLogger({ module: "launcher" }),
|
|
26
|
+
});
|
|
27
|
+
}
|
|
@@ -1,5 +1,9 @@
|
|
|
1
|
+
export type { CreateManagedChildProcessesOptions } from "./create-managed-child-processes";
|
|
2
|
+
export { createManagedChildProcesses } from "./create-managed-child-processes";
|
|
1
3
|
export type {
|
|
4
|
+
CriticalExitHandler,
|
|
2
5
|
ManagedChildProcessesOptions,
|
|
6
|
+
SpawnChildOptions,
|
|
3
7
|
SpawnChildResult,
|
|
4
8
|
StopAllChildrenOptions,
|
|
5
9
|
} from "./managed-child-processes";
|
|
@@ -8,7 +8,13 @@ export interface ManagedChildProcessesOptions {
|
|
|
8
8
|
preloadModules?: readonly string[];
|
|
9
9
|
/** SIGTERM -> wait -> SIGKILL delay (default 5000 ms). */
|
|
10
10
|
graceMs?: number;
|
|
11
|
-
logger?: Pick<LoggerWithScope, "info" | "warn" | "error">;
|
|
11
|
+
logger?: Pick<LoggerWithScope, "info" | "warn" | "error" | "critical">;
|
|
12
|
+
useTsx?: boolean;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface SpawnChildOptions {
|
|
16
|
+
env?: Record<string, string>;
|
|
17
|
+
useTsx?: boolean;
|
|
12
18
|
}
|
|
13
19
|
|
|
14
20
|
export interface SpawnChildResult {
|
|
@@ -22,6 +28,11 @@ export interface StopAllChildrenOptions {
|
|
|
22
28
|
extraPids?: readonly number[];
|
|
23
29
|
}
|
|
24
30
|
|
|
31
|
+
export interface CriticalExitHandler {
|
|
32
|
+
labels: readonly string[];
|
|
33
|
+
onCriticalExit: (label: string, code: number | null, signal: NodeJS.Signals | null) => void;
|
|
34
|
+
}
|
|
35
|
+
|
|
25
36
|
/**
|
|
26
37
|
* Process-local registry for tsx child processes (supervisor, worker, etc.).
|
|
27
38
|
* Each OS process has its own instance - do not expect cross-process PID tracking.
|
|
@@ -30,16 +41,24 @@ export class ManagedChildProcesses {
|
|
|
30
41
|
private readonly projectRoot: string;
|
|
31
42
|
private readonly preloadModules: readonly string[];
|
|
32
43
|
private readonly graceMs: number;
|
|
33
|
-
private readonly
|
|
44
|
+
private readonly defaultUseTsx: boolean;
|
|
45
|
+
private readonly logger?: Pick<LoggerWithScope, "info" | "warn" | "error" | "critical">;
|
|
34
46
|
private readonly byLabel = new Map<string, ChildProcess>();
|
|
47
|
+
private criticalExitHandler: CriticalExitHandler | null = null;
|
|
48
|
+
private suppressCriticalExit = false;
|
|
35
49
|
|
|
36
50
|
constructor(options: ManagedChildProcessesOptions) {
|
|
37
51
|
this.projectRoot = options.projectRoot;
|
|
38
52
|
this.preloadModules = options.preloadModules ?? [];
|
|
39
53
|
this.graceMs = options.graceMs ?? 5_000;
|
|
54
|
+
this.defaultUseTsx = options.useTsx ?? true;
|
|
40
55
|
this.logger = options.logger;
|
|
41
56
|
}
|
|
42
57
|
|
|
58
|
+
setCriticalExitHandler(handler: CriticalExitHandler): void {
|
|
59
|
+
this.criticalExitHandler = handler;
|
|
60
|
+
}
|
|
61
|
+
|
|
43
62
|
get(label: string): ChildProcess | null {
|
|
44
63
|
return this.byLabel.get(label) ?? null;
|
|
45
64
|
}
|
|
@@ -55,7 +74,7 @@ export class ManagedChildProcesses {
|
|
|
55
74
|
.map(([label]) => label);
|
|
56
75
|
}
|
|
57
76
|
|
|
58
|
-
spawn(label: string, entryScript: string): SpawnChildResult {
|
|
77
|
+
spawn(label: string, entryScript: string, spawnOptions?: SpawnChildOptions): SpawnChildResult {
|
|
59
78
|
const existing = this.getRunning(label);
|
|
60
79
|
if (existing != null) {
|
|
61
80
|
return { child: existing, created: false };
|
|
@@ -65,14 +84,23 @@ export class ManagedChildProcesses {
|
|
|
65
84
|
projectRoot: this.projectRoot,
|
|
66
85
|
entryScript,
|
|
67
86
|
preloadModules: this.preloadModules,
|
|
87
|
+
useTsx: spawnOptions?.useTsx ?? this.defaultUseTsx,
|
|
88
|
+
env: spawnOptions?.env != null ? { ...process.env, ...spawnOptions.env } : process.env,
|
|
68
89
|
});
|
|
69
90
|
|
|
70
|
-
child.on("exit", (code, signal) => {
|
|
91
|
+
child.on("exit", (code: number | null, signal: NodeJS.Signals | null) => {
|
|
71
92
|
this.logger?.info("child exited", { topic: "process", data: { label, code, signal } });
|
|
72
93
|
const current = this.byLabel.get(label);
|
|
73
94
|
if (current === child) {
|
|
74
95
|
this.byLabel.delete(label);
|
|
75
96
|
}
|
|
97
|
+
if (
|
|
98
|
+
!this.suppressCriticalExit &&
|
|
99
|
+
this.criticalExitHandler != null &&
|
|
100
|
+
this.criticalExitHandler.labels.includes(label)
|
|
101
|
+
) {
|
|
102
|
+
this.criticalExitHandler.onCriticalExit(label, code, signal);
|
|
103
|
+
}
|
|
76
104
|
});
|
|
77
105
|
|
|
78
106
|
this.byLabel.set(label, child);
|
|
@@ -90,20 +118,25 @@ export class ManagedChildProcesses {
|
|
|
90
118
|
}
|
|
91
119
|
|
|
92
120
|
async stopAll(options?: StopAllChildrenOptions): Promise<void> {
|
|
93
|
-
|
|
121
|
+
this.suppressCriticalExit = true;
|
|
122
|
+
try {
|
|
123
|
+
const tracked = [...this.byLabel.values()].filter((child) => isChildProcessRunning(child));
|
|
94
124
|
|
|
95
|
-
|
|
125
|
+
await Promise.all(tracked.map((child) => this.stopChild(child)));
|
|
96
126
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
127
|
+
const extraPids = options?.extraPids ?? [];
|
|
128
|
+
for (const pid of extraPids) {
|
|
129
|
+
if (pid == null || tracked.some((child) => child.pid === pid)) {
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
try {
|
|
133
|
+
process.kill(pid, "SIGTERM");
|
|
134
|
+
} catch {
|
|
135
|
+
// already exited
|
|
136
|
+
}
|
|
106
137
|
}
|
|
138
|
+
} finally {
|
|
139
|
+
this.suppressCriticalExit = false;
|
|
107
140
|
}
|
|
108
141
|
}
|
|
109
142
|
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
import { EMPTY_REGISTRY_SLOT_RUNTIME_STATE } from "../../service/registry-lifecycle";
|
|
2
|
+
import { createJsonFileStateStore, type JsonFileStateStore, type JsonStatePatch } from "../fs/json-state-store";
|
|
3
|
+
import type {
|
|
4
|
+
DefaultExtraRuntimeSlices,
|
|
5
|
+
GuardRuntimeSlice,
|
|
6
|
+
ReadinessRuntimeSlice,
|
|
7
|
+
SchedulerRuntimeSlice,
|
|
8
|
+
ServiceRuntimeExtraSlicePatch,
|
|
9
|
+
ServiceRuntimeNestedKeys,
|
|
10
|
+
ServiceRuntimeState,
|
|
11
|
+
WorkerRuntimeSlice,
|
|
12
|
+
} from "./service-runtime-state.types";
|
|
13
|
+
|
|
14
|
+
const EPOCH_ISO = new Date(0).toISOString();
|
|
15
|
+
|
|
16
|
+
export interface CreateServiceRuntimeStateStoreOptions<
|
|
17
|
+
TExtraSlices extends Record<string, object> = DefaultExtraRuntimeSlices,
|
|
18
|
+
> {
|
|
19
|
+
filePath: string;
|
|
20
|
+
/** Default values for service-specific top-level nested slices. */
|
|
21
|
+
extraDefaults?: TExtraSlices;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export interface FinishRunnerTickOptions<TExtraSlices extends Record<string, object> = DefaultExtraRuntimeSlices> {
|
|
25
|
+
nowMs: number;
|
|
26
|
+
lastFired: Record<string, string>;
|
|
27
|
+
postedCount?: number;
|
|
28
|
+
/** Patch extra slices in the same atomic write as the scheduler tick. */
|
|
29
|
+
extra?: ServiceRuntimeExtraSlicePatch<TExtraSlices>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface ServiceRuntimeStateStore<TExtraSlices extends Record<string, object> = DefaultExtraRuntimeSlices> {
|
|
33
|
+
readRuntimeState(): Promise<ServiceRuntimeState<TExtraSlices>>;
|
|
34
|
+
writeRuntimeState(
|
|
35
|
+
patch: JsonStatePatch<ServiceRuntimeState<TExtraSlices>, ServiceRuntimeNestedKeys<TExtraSlices>>,
|
|
36
|
+
): Promise<ServiceRuntimeState<TExtraSlices>>;
|
|
37
|
+
resetRuntimeStateForStartup(): Promise<ServiceRuntimeState<TExtraSlices>>;
|
|
38
|
+
finishRunnerTick(options: FinishRunnerTickOptions<TExtraSlices>): Promise<void>;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function createDefaultServiceRuntimeState<TExtraSlices extends Record<string, object>>(options: {
|
|
42
|
+
extraDefaults?: TExtraSlices;
|
|
43
|
+
}): ServiceRuntimeState<TExtraSlices> {
|
|
44
|
+
const readiness: ReadinessRuntimeSlice = {
|
|
45
|
+
status: "pending",
|
|
46
|
+
checkedAt: null,
|
|
47
|
+
message: null,
|
|
48
|
+
checks: [],
|
|
49
|
+
};
|
|
50
|
+
const guard: GuardRuntimeSlice = {
|
|
51
|
+
nodeId: null,
|
|
52
|
+
lastCheckAt: null,
|
|
53
|
+
lastDecision: null,
|
|
54
|
+
lastSpawnAt: null,
|
|
55
|
+
spawnCount: 0,
|
|
56
|
+
};
|
|
57
|
+
const scheduler: SchedulerRuntimeSlice = {
|
|
58
|
+
lastTickAt: null,
|
|
59
|
+
lastPostedCount: 0,
|
|
60
|
+
lastFired: {},
|
|
61
|
+
};
|
|
62
|
+
const worker: WorkerRuntimeSlice = {
|
|
63
|
+
pid: null,
|
|
64
|
+
spawnedAt: null,
|
|
65
|
+
ready: false,
|
|
66
|
+
registeredTasks: [],
|
|
67
|
+
readyAt: null,
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
return {
|
|
71
|
+
updatedAt: EPOCH_ISO,
|
|
72
|
+
readiness,
|
|
73
|
+
guard,
|
|
74
|
+
scheduler,
|
|
75
|
+
worker,
|
|
76
|
+
registry: { ...EMPTY_REGISTRY_SLOT_RUNTIME_STATE },
|
|
77
|
+
...(options.extraDefaults ?? ({} as TExtraSlices)),
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** JSON file-backed service runtime state (L3 blueprint) with typed core slices and extensions. */
|
|
82
|
+
export function createServiceRuntimeStateStore<TExtraSlices extends Record<string, object> = DefaultExtraRuntimeSlices>(
|
|
83
|
+
options: CreateServiceRuntimeStateStoreOptions<TExtraSlices>,
|
|
84
|
+
): ServiceRuntimeStateStore<TExtraSlices> {
|
|
85
|
+
type State = ServiceRuntimeState<TExtraSlices>;
|
|
86
|
+
type NK = ServiceRuntimeNestedKeys<TExtraSlices>;
|
|
87
|
+
|
|
88
|
+
const defaultState = createDefaultServiceRuntimeState(options);
|
|
89
|
+
const nestedKeys = [
|
|
90
|
+
"readiness",
|
|
91
|
+
"guard",
|
|
92
|
+
"scheduler",
|
|
93
|
+
"worker",
|
|
94
|
+
"registry",
|
|
95
|
+
...(Object.keys(options.extraDefaults ?? {}) as (keyof TExtraSlices & string)[]),
|
|
96
|
+
] as NK[];
|
|
97
|
+
|
|
98
|
+
let store: JsonFileStateStore<State, NK> | undefined;
|
|
99
|
+
|
|
100
|
+
function instance(): JsonFileStateStore<State, NK> {
|
|
101
|
+
store ??= createJsonFileStateStore<State, NK>({
|
|
102
|
+
filePath: options.filePath,
|
|
103
|
+
defaultState,
|
|
104
|
+
nestedKeys,
|
|
105
|
+
updatedAtKey: "updatedAt",
|
|
106
|
+
});
|
|
107
|
+
return store;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return {
|
|
111
|
+
readRuntimeState: () => instance().read(),
|
|
112
|
+
writeRuntimeState: (patch) => instance().write(patch),
|
|
113
|
+
resetRuntimeStateForStartup: () => instance().reset(),
|
|
114
|
+
finishRunnerTick: async ({ nowMs, lastFired, postedCount, extra }) => {
|
|
115
|
+
await instance().write({
|
|
116
|
+
scheduler: {
|
|
117
|
+
lastTickAt: new Date(nowMs).toISOString(),
|
|
118
|
+
lastFired,
|
|
119
|
+
...(postedCount != null ? { lastPostedCount: postedCount } : {}),
|
|
120
|
+
},
|
|
121
|
+
...extra,
|
|
122
|
+
} as JsonStatePatch<State, NK>);
|
|
123
|
+
},
|
|
124
|
+
};
|
|
125
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export type {
|
|
2
|
+
CreateServiceRuntimeStateStoreOptions,
|
|
3
|
+
FinishRunnerTickOptions,
|
|
4
|
+
ServiceRuntimeStateStore,
|
|
5
|
+
} from "./create-service-runtime-state-store";
|
|
6
|
+
export { createServiceRuntimeStateStore } from "./create-service-runtime-state-store";
|
|
7
|
+
export type {
|
|
8
|
+
GuardRuntimeSlice,
|
|
9
|
+
ReadinessRuntimeSlice,
|
|
10
|
+
ReadinessStatus,
|
|
11
|
+
SchedulerRuntimeSlice,
|
|
12
|
+
ServiceRuntimeCoreNestedKey,
|
|
13
|
+
ServiceRuntimeExtraSlicePatch,
|
|
14
|
+
ServiceRuntimeNestedKeys,
|
|
15
|
+
ServiceRuntimeState,
|
|
16
|
+
WorkerRuntimeSlice,
|
|
17
|
+
} from "./service-runtime-state.types";
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { RegistrySlotRuntimeState } from "../../service/registry-lifecycle";
|
|
2
|
+
|
|
3
|
+
export type ReadinessStatus = "pending" | "passed" | "failed";
|
|
4
|
+
|
|
5
|
+
export interface ReadinessRuntimeSlice {
|
|
6
|
+
status: ReadinessStatus;
|
|
7
|
+
checkedAt: string | null;
|
|
8
|
+
message: string | null;
|
|
9
|
+
checks: { name: string; ok: boolean; detail?: string }[];
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface GuardRuntimeSlice {
|
|
13
|
+
nodeId: string | null;
|
|
14
|
+
lastCheckAt: string | null;
|
|
15
|
+
lastDecision: string | null;
|
|
16
|
+
lastSpawnAt: string | null;
|
|
17
|
+
spawnCount: number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface SchedulerRuntimeSlice {
|
|
21
|
+
lastTickAt: string | null;
|
|
22
|
+
lastPostedCount: number;
|
|
23
|
+
lastFired: Record<string, string>;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface WorkerRuntimeSlice {
|
|
27
|
+
pid: number | null;
|
|
28
|
+
spawnedAt: string | null;
|
|
29
|
+
ready: boolean;
|
|
30
|
+
registeredTasks: string[];
|
|
31
|
+
readyAt: string | null;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Default when an L3 service has no service-specific runtime state slices.
|
|
36
|
+
* Use an empty object type (not `Record<string, never>`) so intersecting with core
|
|
37
|
+
* slices does not add a string index signature that collapses property types to `never`.
|
|
38
|
+
*/
|
|
39
|
+
export type DefaultExtraRuntimeSlices = Record<never, never>;
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Cross-process service runtime state persisted to `state.json`.
|
|
43
|
+
* L3 blueprint: readiness + guard + scheduler + worker + registry.
|
|
44
|
+
* Extend via extra top-level slices only.
|
|
45
|
+
*/
|
|
46
|
+
export type ServiceRuntimeState<TExtraSlices extends Record<string, object> = DefaultExtraRuntimeSlices> = {
|
|
47
|
+
updatedAt: string;
|
|
48
|
+
readiness: ReadinessRuntimeSlice;
|
|
49
|
+
guard: GuardRuntimeSlice;
|
|
50
|
+
scheduler: SchedulerRuntimeSlice;
|
|
51
|
+
worker: WorkerRuntimeSlice;
|
|
52
|
+
registry: RegistrySlotRuntimeState;
|
|
53
|
+
} & TExtraSlices;
|
|
54
|
+
|
|
55
|
+
export type ServiceRuntimeCoreNestedKey = "readiness" | "guard" | "scheduler" | "worker" | "registry";
|
|
56
|
+
|
|
57
|
+
export type ServiceRuntimeNestedKeys<TExtraSlices extends Record<string, object>> =
|
|
58
|
+
| ServiceRuntimeCoreNestedKey
|
|
59
|
+
| (keyof TExtraSlices & string);
|
|
60
|
+
|
|
61
|
+
/** Patch shape for extra top-level slices (one level deep). */
|
|
62
|
+
export type ServiceRuntimeExtraSlicePatch<TExtraSlices extends Record<string, object>> = {
|
|
63
|
+
[K in keyof TExtraSlices]?: TExtraSlices[K] extends object ? Partial<TExtraSlices[K]> : TExtraSlices[K];
|
|
64
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
export { registerServiceGuardRunner } from "./register-service-guard-runner";
|
|
2
|
+
export type { RunReadinessGateInput } from "./run-readiness-gate";
|
|
3
|
+
export { runReadinessGate } from "./run-readiness-gate";
|
|
4
|
+
export { runServiceGuardTick } from "./run-service-guard-tick";
|
|
5
|
+
export type {
|
|
6
|
+
RegisterServiceGuardRunnerOptions,
|
|
7
|
+
ServiceGuardNodeOptions,
|
|
8
|
+
ServiceGuardTickInput,
|
|
9
|
+
ServiceGuardTickResult,
|
|
10
|
+
} from "./service-guard.interface";
|
|
11
|
+
export { SERVICE_GUARD_RUNNER_KEY } from "./service-guard.interface";
|
|
12
|
+
export { ServiceGuardNode } from "./service-guard-node";
|
|
13
|
+
export {
|
|
14
|
+
getWorkerSpawnCooldownLastAt,
|
|
15
|
+
markWorkerSpawned,
|
|
16
|
+
resetWorkerSpawnCooldownForTests,
|
|
17
|
+
} from "./worker-spawn-cooldown";
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import type { TriggerRunnerContext } from "../../trigger/trigger.interface";
|
|
2
|
+
import { TriggerManager } from "../../trigger/trigger-manager";
|
|
3
|
+
import { runServiceGuardTick } from "./run-service-guard-tick";
|
|
4
|
+
import { type RegisterServiceGuardRunnerOptions, SERVICE_GUARD_RUNNER_KEY } from "./service-guard.interface";
|
|
5
|
+
|
|
6
|
+
async function runServiceGuardRunner(
|
|
7
|
+
ctx: TriggerRunnerContext,
|
|
8
|
+
options: RegisterServiceGuardRunnerOptionsWithClock,
|
|
9
|
+
): Promise<void> {
|
|
10
|
+
const { loopTraceId, nodeId, logger } = ctx;
|
|
11
|
+
const nowMs = options.now?.() ?? Date.now();
|
|
12
|
+
const checkedAt = new Date(nowMs).toISOString();
|
|
13
|
+
const serviceId = await options.getServiceId();
|
|
14
|
+
|
|
15
|
+
const result = await runServiceGuardTick({
|
|
16
|
+
serviceValue: options.serviceValue,
|
|
17
|
+
serviceId,
|
|
18
|
+
nodeId,
|
|
19
|
+
loopTraceId,
|
|
20
|
+
logger,
|
|
21
|
+
logTopic: options.logTopic,
|
|
22
|
+
checkedAt,
|
|
23
|
+
nowMs,
|
|
24
|
+
taskNodeStaleMs: options.taskNodeStaleMs,
|
|
25
|
+
workerSpawnCooldownMs: options.workerSpawnCooldownMs,
|
|
26
|
+
maxNodes: options.maxNodes,
|
|
27
|
+
spawnWorker: options.spawnWorker,
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
if (result.registryPatch != null) {
|
|
31
|
+
await options.onRegistryPatch(result.registryPatch);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (!result.continueTick) {
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
await options.onGuardPatch({
|
|
39
|
+
nodeId,
|
|
40
|
+
lastCheckAt: checkedAt,
|
|
41
|
+
lastDecision: result.decision,
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
if (options.onDecision != null) {
|
|
45
|
+
await options.onDecision(result.decision);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface RegisterServiceGuardRunnerOptionsWithClock extends RegisterServiceGuardRunnerOptions {
|
|
50
|
+
/** Test hook — defaults to Date.now(). */
|
|
51
|
+
now?: () => number;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** Register the standard L3 guard runner (registry slot + TaskNode + Service runtime). */
|
|
55
|
+
export function registerServiceGuardRunner(options: RegisterServiceGuardRunnerOptionsWithClock): void {
|
|
56
|
+
TriggerManager.registerRunner({
|
|
57
|
+
key: options.runnerKey ?? SERVICE_GUARD_RUNNER_KEY,
|
|
58
|
+
intervalMs: options.intervalMs,
|
|
59
|
+
initialDelayMs: options.initialDelayMs,
|
|
60
|
+
fn: (ctx) => runServiceGuardRunner(ctx, options),
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export { SERVICE_GUARD_RUNNER_KEY };
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { LoggerWithScope } from "../../shared/log";
|
|
2
|
+
import type { ReadinessCheck, ReadinessReport } from "../readiness/readiness.types";
|
|
3
|
+
import { runReadinessChecks } from "../readiness/run-readiness-checks";
|
|
4
|
+
|
|
5
|
+
export interface RunReadinessGateInput {
|
|
6
|
+
checks: ReadinessCheck[];
|
|
7
|
+
onReport: (report: ReadinessReport) => Promise<void>;
|
|
8
|
+
logger: LoggerWithScope;
|
|
9
|
+
logTopic?: string;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** Run readiness checks, persist report via callback, fail-fast when not ok. */
|
|
13
|
+
export async function runReadinessGate(input: RunReadinessGateInput): Promise<ReadinessReport> {
|
|
14
|
+
const { checks, onReport, logger, logTopic = "guard" } = input;
|
|
15
|
+
|
|
16
|
+
logger.info("Running readiness checks", { topic: logTopic });
|
|
17
|
+
const report = await runReadinessChecks(checks);
|
|
18
|
+
await onReport(report);
|
|
19
|
+
|
|
20
|
+
if (!report.ok) {
|
|
21
|
+
logger.error("Readiness checks failed", {
|
|
22
|
+
topic: logTopic,
|
|
23
|
+
data: { message: report.message, checks: report.checks },
|
|
24
|
+
});
|
|
25
|
+
throw new Error(report.message ?? "Readiness checks failed");
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
logger.debug("Readiness checks passed", {
|
|
29
|
+
topic: logTopic,
|
|
30
|
+
data: { checks: report.checks.map((check) => check.name) },
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
return report;
|
|
34
|
+
}
|