@leitwerk-dev/worker-runners 0.4.0 → 0.4.2
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/{chunk-ZGFD76I6.js → chunk-GF3H4C3J.js} +1 -1
- package/dist/chunk-GF3H4C3J.js.map +1 -0
- package/dist/{chunk-SFI5TMFN.js → chunk-GMZOYHQD.js} +121 -8
- package/dist/chunk-GMZOYHQD.js.map +1 -0
- package/dist/{chunk-DSSMRFB4.js → chunk-LZOVBKHR.js} +1 -1
- package/dist/{chunk-DSSMRFB4.js.map → chunk-LZOVBKHR.js.map} +1 -1
- package/dist/index.js +15 -2
- package/dist/index.js.map +1 -1
- package/dist/{kubernetes-api-client-BeZPZqmj.d.ts → kubernetes-api-client-BMWAgyfW.d.ts} +14 -1
- package/dist/kubernetes-client-public.js +1 -1
- package/dist/kubernetes-http-client.js +1 -1
- package/dist/kubernetes-preparation.d.ts +1 -1
- package/dist/kubernetes-preparation.d.ts.map +1 -1
- package/dist/kubernetes-public.js +2 -2
- package/dist/kubernetes-volume-pool-api.d.ts +14 -1
- package/dist/kubernetes-volume-pool-api.d.ts.map +1 -1
- package/dist/kubernetes-volume-pool.d.ts +3 -1
- package/dist/kubernetes-volume-pool.d.ts.map +1 -1
- package/dist/kubernetes-volume-retirement.d.ts +20 -0
- package/dist/kubernetes-volume-retirement.d.ts.map +1 -0
- package/dist/kubernetes-worker-runner.d.ts +3 -1
- package/dist/kubernetes-worker-runner.d.ts.map +1 -1
- package/dist/kubernetes-worker-runner.js +2 -2
- package/dist/process-volume-retention.d.ts +4 -0
- package/dist/process-volume-retention.d.ts.map +1 -1
- package/dist/types-public.js +1 -1
- package/dist/types.d.ts +11 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +1 -1
- package/package.json +5 -5
- package/src/kubernetes-preparation.ts +3 -3
- package/src/kubernetes-volume-pool-api.ts +14 -1
- package/src/kubernetes-volume-pool.ts +26 -4
- package/src/kubernetes-volume-retirement.ts +139 -0
- package/src/kubernetes-worker-runner.ts +29 -2
- package/src/process-volume-retention.ts +27 -4
- package/src/types.ts +15 -1
- package/dist/chunk-SFI5TMFN.js.map +0 -1
- package/dist/chunk-ZGFD76I6.js.map +0 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leitwerk-dev/worker-runners",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.2",
|
|
4
4
|
"repository": {
|
|
5
5
|
"type": "git",
|
|
6
6
|
"url": "git+https://github.com/leitwerk-dev/leitwerk.git",
|
|
@@ -48,10 +48,10 @@
|
|
|
48
48
|
"build": "tsup --silent"
|
|
49
49
|
},
|
|
50
50
|
"dependencies": {
|
|
51
|
-
"@leitwerk-dev/domain": "0.4.
|
|
52
|
-
"@leitwerk-dev/session-transfer": "0.4.
|
|
53
|
-
"@leitwerk-dev/watcher-utils": "0.4.
|
|
54
|
-
"@leitwerk-dev/worker-protocol": "0.4.
|
|
51
|
+
"@leitwerk-dev/domain": "0.4.2",
|
|
52
|
+
"@leitwerk-dev/session-transfer": "0.4.2",
|
|
53
|
+
"@leitwerk-dev/watcher-utils": "0.4.2",
|
|
54
|
+
"@leitwerk-dev/worker-protocol": "0.4.2"
|
|
55
55
|
},
|
|
56
56
|
"devDependencies": {
|
|
57
57
|
"@types/node": "^22.15.30",
|
|
@@ -50,7 +50,7 @@ export interface PreparationController {
|
|
|
50
50
|
export function createPreparationController(
|
|
51
51
|
reconcile: (signal: AbortSignal) => Promise<void>,
|
|
52
52
|
intervalMs: number,
|
|
53
|
-
onError?: () => void,
|
|
53
|
+
onError?: (error?: unknown) => void,
|
|
54
54
|
errorIntervalMs = intervalMs,
|
|
55
55
|
): PreparationController {
|
|
56
56
|
let controller: AbortController | undefined;
|
|
@@ -65,9 +65,9 @@ export function createPreparationController(
|
|
|
65
65
|
let interval = intervalMs;
|
|
66
66
|
try {
|
|
67
67
|
await reconcile(signal);
|
|
68
|
-
} catch {
|
|
68
|
+
} catch (error) {
|
|
69
69
|
interval = errorIntervalMs;
|
|
70
|
-
if (!signal.aborted) onError?.();
|
|
70
|
+
if (!signal.aborted) onError?.(error);
|
|
71
71
|
}
|
|
72
72
|
try {
|
|
73
73
|
await delay(interval, undefined, { signal });
|
|
@@ -19,6 +19,8 @@ export interface PoolObject {
|
|
|
19
19
|
/** @internal */
|
|
20
20
|
deletionTimestamp?: string;
|
|
21
21
|
/** @internal */
|
|
22
|
+
creationTimestamp?: string;
|
|
23
|
+
/** @internal */
|
|
22
24
|
labels?: Record<string, string>;
|
|
23
25
|
/** @internal */
|
|
24
26
|
annotations?: Record<string, string>;
|
|
@@ -54,6 +56,17 @@ export interface PoolObject {
|
|
|
54
56
|
status?: {
|
|
55
57
|
/** @internal */
|
|
56
58
|
phase?: string;
|
|
59
|
+
/** @internal */
|
|
60
|
+
reason?: string;
|
|
61
|
+
/** @internal */
|
|
62
|
+
conditions?: Array<{
|
|
63
|
+
/** @internal */
|
|
64
|
+
type?: string;
|
|
65
|
+
/** @internal */
|
|
66
|
+
status?: string;
|
|
67
|
+
/** @internal */
|
|
68
|
+
reason?: string;
|
|
69
|
+
}>;
|
|
57
70
|
};
|
|
58
71
|
}
|
|
59
72
|
|
|
@@ -93,7 +106,7 @@ export interface VolumePoolApi {
|
|
|
93
106
|
deleteStorageClass(value: PoolStorageClass, signal: AbortSignal): Promise<void>;
|
|
94
107
|
/** @internal */
|
|
95
108
|
list(
|
|
96
|
-
kind: "persistentvolumes" | "persistentvolumeclaims",
|
|
109
|
+
kind: "persistentvolumes" | "persistentvolumeclaims" | "pods",
|
|
97
110
|
namespace: string,
|
|
98
111
|
selector: string,
|
|
99
112
|
signal: AbortSignal,
|
|
@@ -36,7 +36,9 @@ export interface VolumePoolOptions {
|
|
|
36
36
|
/** @internal */
|
|
37
37
|
tolerations?: unknown[];
|
|
38
38
|
/** @internal */
|
|
39
|
-
onError?: () => void;
|
|
39
|
+
onError?: (error?: unknown) => void;
|
|
40
|
+
/** Clock seam; Kubernetes creation times survive controller restarts. @internal */
|
|
41
|
+
now?: () => number;
|
|
40
42
|
}
|
|
41
43
|
|
|
42
44
|
/** Only the staging claim's UID authorizes rebinding; process claims are never recycled. @internal */
|
|
@@ -74,6 +76,7 @@ export function createKubernetesVolumePool(options: VolumePoolOptions) {
|
|
|
74
76
|
},
|
|
75
77
|
spec: {
|
|
76
78
|
...buildPreparationPodSpec(options, [{ name: "volume", mountPath: "/volume" }]),
|
|
79
|
+
activeDeadlineSeconds: 900,
|
|
77
80
|
volumes: [{ name: "volume", persistentVolumeClaim: { claimName: claim.metadata.name } }],
|
|
78
81
|
},
|
|
79
82
|
};
|
|
@@ -169,6 +172,7 @@ export function createKubernetesVolumePool(options: VolumePoolOptions) {
|
|
|
169
172
|
for (const pv of volumes) {
|
|
170
173
|
if (await advanceVolume(pv, signal)) active.add(pv.metadata.annotations?.[CLAIM_NAME] ?? "");
|
|
171
174
|
}
|
|
175
|
+
const failures: string[] = [];
|
|
172
176
|
const claims = await api.list("persistentvolumeclaims", namespace, selector, signal);
|
|
173
177
|
for (const claim of claims) {
|
|
174
178
|
active.add(claim.metadata.name);
|
|
@@ -189,8 +193,20 @@ export function createKubernetesVolumePool(options: VolumePoolOptions) {
|
|
|
189
193
|
!pod.metadata.ownerReferences?.some((owner) => owner.uid === claim.metadata.uid)
|
|
190
194
|
)
|
|
191
195
|
throw new Error("Preparation Pod ownership mismatch");
|
|
192
|
-
|
|
193
|
-
|
|
196
|
+
const created = Date.parse(claim.metadata.creationTimestamp ?? "");
|
|
197
|
+
const ageSeconds = Number.isFinite(created)
|
|
198
|
+
? Math.max(0, Math.floor(((options.now?.() ?? Date.now()) - created) / 1_000))
|
|
199
|
+
: undefined;
|
|
200
|
+
if (
|
|
201
|
+
pod.status?.phase === "Failed" ||
|
|
202
|
+
(pod.status?.phase !== "Succeeded" && ageSeconds !== undefined && ageSeconds >= 900)
|
|
203
|
+
) {
|
|
204
|
+
// Keep the failed receipt and reservation across restarts. An operator retries
|
|
205
|
+
// the same PVC by deleting its Pod only after fixing storage; never allocate
|
|
206
|
+
// another volume to work around replica scheduling or attach failures.
|
|
207
|
+
failures.push(
|
|
208
|
+
`${namespace}/${pod.metadata.name}: age=${ageSeconds ?? "unknown"}s phase=${pod.status?.phase ?? "unknown"} reason=${pod.status?.reason ?? pod.status?.conditions?.find((condition) => condition.status === "False")?.reason ?? "inspect Pod events and CSI/Longhorn scheduling conditions"}`,
|
|
209
|
+
);
|
|
194
210
|
continue;
|
|
195
211
|
}
|
|
196
212
|
if (pod.status?.phase !== "Succeeded" || !claim.spec.volumeName) continue;
|
|
@@ -223,6 +239,11 @@ export function createKubernetesVolumePool(options: VolumePoolOptions) {
|
|
|
223
239
|
[CLAIM_NAME]: claim.metadata.name,
|
|
224
240
|
[POLICY]: policy,
|
|
225
241
|
[STATE]: "preparing",
|
|
242
|
+
"leitwerk.dev/preparation-pod-uid": pod.metadata.uid ?? "",
|
|
243
|
+
"leitwerk.dev/preparation-image": options.image,
|
|
244
|
+
"leitwerk.dev/preparation-completed-at": new Date(
|
|
245
|
+
options.now?.() ?? Date.now(),
|
|
246
|
+
).toISOString(),
|
|
226
247
|
[TARGET_CLASS]: options.storageClassName,
|
|
227
248
|
[SIZE]: claim.metadata.annotations?.[SIZE],
|
|
228
249
|
[MODES]: claim.metadata.annotations?.[MODES],
|
|
@@ -311,6 +332,7 @@ export function createKubernetesVolumePool(options: VolumePoolOptions) {
|
|
|
311
332
|
);
|
|
312
333
|
}
|
|
313
334
|
}
|
|
335
|
+
if (failures.length) throw new Error(`Volume pool preparation blocked: ${failures.join("; ")}`);
|
|
314
336
|
}
|
|
315
337
|
|
|
316
338
|
return {
|
|
@@ -318,6 +340,6 @@ export function createKubernetesVolumePool(options: VolumePoolOptions) {
|
|
|
318
340
|
poolId,
|
|
319
341
|
/** @internal */
|
|
320
342
|
reconcile,
|
|
321
|
-
...createPreparationController(reconcile, 2_000,
|
|
343
|
+
...createPreparationController(reconcile, 2_000, options.onError, 30_000),
|
|
322
344
|
};
|
|
323
345
|
}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { POOL_LABEL } from "./kubernetes-volume-pool.js";
|
|
2
|
+
import type { VolumePoolApi } from "./kubernetes-volume-pool-api.js";
|
|
3
|
+
import type { ProcessVolumeReclamation } from "./types.js";
|
|
4
|
+
|
|
5
|
+
/** Persist ownership and any terminal disposal authorization before PVC deletion. @internal */
|
|
6
|
+
export async function retireKubernetesProcessVolume(input: {
|
|
7
|
+
/** @internal */
|
|
8
|
+
api: VolumePoolApi;
|
|
9
|
+
/** @internal */
|
|
10
|
+
namespace: string;
|
|
11
|
+
/** @internal */
|
|
12
|
+
name: string;
|
|
13
|
+
/** @internal */
|
|
14
|
+
instanceId: string;
|
|
15
|
+
/** @internal */
|
|
16
|
+
signal: AbortSignal;
|
|
17
|
+
/** @internal */
|
|
18
|
+
reason?: "retention-expired" | "process-deleted";
|
|
19
|
+
/** @internal */
|
|
20
|
+
reclaimBackingStorage?: ProcessVolumeReclamation | "process-deleted";
|
|
21
|
+
}): Promise<void> {
|
|
22
|
+
const { api, namespace, name, instanceId, signal } = input;
|
|
23
|
+
const reclamation = input.reclaimBackingStorage;
|
|
24
|
+
if (
|
|
25
|
+
typeof reclamation === "object" &&
|
|
26
|
+
(!["completed", "aborted"].includes(reclamation.lifecycleStatus) ||
|
|
27
|
+
!Number.isFinite(Date.parse(reclamation.closedAt)) ||
|
|
28
|
+
Date.parse(reclamation.closedAt) > Date.now())
|
|
29
|
+
)
|
|
30
|
+
throw new Error("Backing-storage reclamation requires a closed terminal process");
|
|
31
|
+
if (reclamation === "process-deleted" && input.reason !== "process-deleted")
|
|
32
|
+
throw new Error("Explicit storage deletion requires a process deletion request");
|
|
33
|
+
const pvc = await api.get("persistentvolumeclaims", namespace, name, signal);
|
|
34
|
+
if (!pvc) return;
|
|
35
|
+
const labels = pvc.metadata.labels;
|
|
36
|
+
if (
|
|
37
|
+
!pvc.metadata.uid ||
|
|
38
|
+
labels?.["leitwerk.dev/managed-by"] !== "leitwerk" ||
|
|
39
|
+
labels["leitwerk.dev/component"] !== "process-volume" ||
|
|
40
|
+
labels["leitwerk.dev/instance-id"] !== instanceId ||
|
|
41
|
+
pvc.metadata.ownerReferences?.length
|
|
42
|
+
)
|
|
43
|
+
throw new Error("Cannot retire a foreign or ambiguous process PVC");
|
|
44
|
+
if (reclamation && !pvc.spec.volumeName)
|
|
45
|
+
throw new Error(
|
|
46
|
+
"PVC is still provisioning; preserve its identity until the backing PV can be audited",
|
|
47
|
+
);
|
|
48
|
+
const pods = await api.list("pods", namespace, "", signal);
|
|
49
|
+
if (
|
|
50
|
+
pods.some(
|
|
51
|
+
(pod) =>
|
|
52
|
+
!["Succeeded", "Failed"].includes(pod.status?.phase ?? "") &&
|
|
53
|
+
Array.isArray(pod.spec.volumes) &&
|
|
54
|
+
pod.spec.volumes.some(
|
|
55
|
+
(volume) =>
|
|
56
|
+
(volume as { persistentVolumeClaim?: { claimName?: string } }).persistentVolumeClaim
|
|
57
|
+
?.claimName === name,
|
|
58
|
+
),
|
|
59
|
+
)
|
|
60
|
+
)
|
|
61
|
+
throw new Error("Cannot retire a process volume referenced by a live Pod");
|
|
62
|
+
if (pvc.spec.volumeName) {
|
|
63
|
+
const pv = await api.get("persistentvolumes", "", pvc.spec.volumeName, signal);
|
|
64
|
+
if (
|
|
65
|
+
!pv ||
|
|
66
|
+
(pv.metadata.labels?.["leitwerk.dev/managed-by"] !== undefined &&
|
|
67
|
+
pv.metadata.labels["leitwerk.dev/managed-by"] !== "leitwerk") ||
|
|
68
|
+
(pv.metadata.labels?.["leitwerk.dev/instance-id"] !== undefined &&
|
|
69
|
+
pv.metadata.labels["leitwerk.dev/instance-id"] !== instanceId) ||
|
|
70
|
+
pv.metadata.ownerReferences?.length ||
|
|
71
|
+
pv.metadata.deletionTimestamp ||
|
|
72
|
+
pv.spec.claimRef?.uid !== pvc.metadata.uid ||
|
|
73
|
+
pv.spec.claimRef?.name !== name ||
|
|
74
|
+
pv.spec.claimRef?.namespace !== namespace
|
|
75
|
+
)
|
|
76
|
+
throw new Error("Cannot retire a volume with ambiguous claim ownership");
|
|
77
|
+
const csi = pv.spec.csi as { driver?: string; volumeHandle?: string } | undefined;
|
|
78
|
+
if (
|
|
79
|
+
reclamation &&
|
|
80
|
+
(!csi?.driver ||
|
|
81
|
+
!csi.volumeHandle ||
|
|
82
|
+
pv.metadata.annotations?.["pv.kubernetes.io/provisioned-by"] !== csi.driver ||
|
|
83
|
+
!["Retain", "Delete"].includes(pv.spec.persistentVolumeReclaimPolicy ?? ""))
|
|
84
|
+
)
|
|
85
|
+
throw new Error(
|
|
86
|
+
"Backing-storage reclamation requires an owned dynamically provisioned CSI volume",
|
|
87
|
+
);
|
|
88
|
+
const volumeLabels: Record<string, string> = {
|
|
89
|
+
...pv.metadata.labels,
|
|
90
|
+
"leitwerk.dev/managed-by": "leitwerk",
|
|
91
|
+
"leitwerk.dev/component": "process-volume",
|
|
92
|
+
"leitwerk.dev/instance-id": instanceId,
|
|
93
|
+
...(reclamation ? { "leitwerk.dev/reclamation": "terminal" } : {}),
|
|
94
|
+
};
|
|
95
|
+
// End pool ownership atomically: a stale pool reconciler must not restore
|
|
96
|
+
// Retain after a fast process closes. Both writers use resourceVersion tests.
|
|
97
|
+
delete volumeLabels[POOL_LABEL];
|
|
98
|
+
await api.patchVolume(
|
|
99
|
+
pv,
|
|
100
|
+
[
|
|
101
|
+
{
|
|
102
|
+
op: "add",
|
|
103
|
+
path: "/metadata/labels",
|
|
104
|
+
value: volumeLabels,
|
|
105
|
+
},
|
|
106
|
+
{
|
|
107
|
+
op: "add",
|
|
108
|
+
path: "/metadata/annotations",
|
|
109
|
+
value: {
|
|
110
|
+
...pv.metadata.annotations,
|
|
111
|
+
"leitwerk.dev/retirement": JSON.stringify({
|
|
112
|
+
instanceId,
|
|
113
|
+
namespace,
|
|
114
|
+
claimName: name,
|
|
115
|
+
claimUid: pvc.metadata.uid,
|
|
116
|
+
volumeUid: pv.metadata.uid,
|
|
117
|
+
reason: input.reason ?? "retention-expired",
|
|
118
|
+
...(reclamation
|
|
119
|
+
? {
|
|
120
|
+
reclamation:
|
|
121
|
+
typeof reclamation === "object"
|
|
122
|
+
? { policy: "terminal-process", ...reclamation }
|
|
123
|
+
: { policy: "process-deletion" },
|
|
124
|
+
}
|
|
125
|
+
: {}),
|
|
126
|
+
}),
|
|
127
|
+
},
|
|
128
|
+
},
|
|
129
|
+
// Arm CSI reclamation before deleting the claim. A crash after this
|
|
130
|
+
// patch is safe: retry either repeats it or CSI completes independently.
|
|
131
|
+
...(reclamation
|
|
132
|
+
? [{ op: "add", path: "/spec/persistentVolumeReclaimPolicy", value: "Delete" }]
|
|
133
|
+
: []),
|
|
134
|
+
],
|
|
135
|
+
signal,
|
|
136
|
+
);
|
|
137
|
+
}
|
|
138
|
+
await api.delete("persistentvolumeclaims", namespace, pvc, signal);
|
|
139
|
+
}
|
|
@@ -30,6 +30,7 @@ import {
|
|
|
30
30
|
import { createKubernetesPreparation } from "./kubernetes-preparation.js";
|
|
31
31
|
import { sampleKubernetesStartup, startupReceipt } from "./kubernetes-startup-sampler.js";
|
|
32
32
|
import { createKubernetesVolumePool } from "./kubernetes-volume-pool.js";
|
|
33
|
+
import { retireKubernetesProcessVolume } from "./kubernetes-volume-retirement.js";
|
|
33
34
|
import { UnitExitNotifier } from "./runner-utils.js";
|
|
34
35
|
import {
|
|
35
36
|
SESSION_TRANSFER_HELPER_ENTRY_PATH,
|
|
@@ -73,6 +74,8 @@ function boundedKubernetesStartDiagnostic(
|
|
|
73
74
|
|
|
74
75
|
/** @internal */
|
|
75
76
|
export interface KubernetesWorkerRunnerOptions {
|
|
77
|
+
/** Enable CSI backing reclamation for explicit process deletion. @internal */
|
|
78
|
+
reclaimOnClose?: boolean;
|
|
76
79
|
/** @internal */
|
|
77
80
|
imagePreload?: {
|
|
78
81
|
/** @internal */
|
|
@@ -85,7 +88,7 @@ export interface KubernetesWorkerRunnerOptions {
|
|
|
85
88
|
/** @internal */
|
|
86
89
|
count: number;
|
|
87
90
|
/** @internal */
|
|
88
|
-
onError?: () => void;
|
|
91
|
+
onError?: (error?: unknown) => void;
|
|
89
92
|
};
|
|
90
93
|
/** @internal */
|
|
91
94
|
client: KubernetesApiClient;
|
|
@@ -263,7 +266,20 @@ export function createKubernetesWorkerRunner(options: KubernetesWorkerRunnerOpti
|
|
|
263
266
|
namespace: manifest.metadata.namespace,
|
|
264
267
|
});
|
|
265
268
|
},
|
|
266
|
-
async release(instanceId) {
|
|
269
|
+
async release(instanceId, releaseOptions) {
|
|
270
|
+
if (client.volumePool) {
|
|
271
|
+
await retireKubernetesProcessVolume({
|
|
272
|
+
api: client.volumePool,
|
|
273
|
+
namespace: namespaceForProcess(instanceId),
|
|
274
|
+
name: kubernetesProcessPvcName(instanceId, options.volume.namePrefix),
|
|
275
|
+
instanceId,
|
|
276
|
+
signal: AbortSignal.timeout(30_000),
|
|
277
|
+
reclaimBackingStorage: releaseOptions?.reclaimBackingStorage,
|
|
278
|
+
});
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
if (releaseOptions?.reclaimBackingStorage)
|
|
282
|
+
throw new Error("Backing-storage reclamation requires Kubernetes volume inspection");
|
|
267
283
|
await client.deletePersistentVolumeClaim(
|
|
268
284
|
kubernetesProcessPvcName(instanceId, options.volume.namePrefix),
|
|
269
285
|
namespaceForProcess(instanceId),
|
|
@@ -272,6 +288,17 @@ export function createKubernetesWorkerRunner(options: KubernetesWorkerRunnerOpti
|
|
|
272
288
|
async deleteProcessResources(instanceId) {
|
|
273
289
|
samplers.get(instanceId)?.stop();
|
|
274
290
|
samplers.delete(instanceId);
|
|
291
|
+
if (client.volumePool) {
|
|
292
|
+
await retireKubernetesProcessVolume({
|
|
293
|
+
api: client.volumePool,
|
|
294
|
+
namespace: namespaceForProcess(instanceId),
|
|
295
|
+
name: kubernetesProcessPvcName(instanceId, options.volume.namePrefix),
|
|
296
|
+
instanceId,
|
|
297
|
+
reason: "process-deleted",
|
|
298
|
+
reclaimBackingStorage: options.reclaimOnClose ? "process-deleted" : undefined,
|
|
299
|
+
signal: AbortSignal.timeout(30_000),
|
|
300
|
+
});
|
|
301
|
+
}
|
|
275
302
|
await client.deleteNamespace(namespaceForProcess(instanceId));
|
|
276
303
|
},
|
|
277
304
|
};
|
|
@@ -6,6 +6,8 @@ import type { ProcessVolume } from "./types.js";
|
|
|
6
6
|
export interface ProcessVolumeRetentionPolicy {
|
|
7
7
|
/** @internal */
|
|
8
8
|
completedProcessRetention: string;
|
|
9
|
+
/** Defaults to error retention for callers retaining non-volume artifacts. @internal */
|
|
10
|
+
abortedProcessRetention?: string;
|
|
9
11
|
/** @internal */
|
|
10
12
|
errorProcessRetention: string;
|
|
11
13
|
}
|
|
@@ -39,6 +41,9 @@ export function planProcessVolumeRetentionCleanup(
|
|
|
39
41
|
input: PlanProcessVolumeRetentionCleanupInput,
|
|
40
42
|
): string[] {
|
|
41
43
|
const completedRetentionMs = retentionMs(input.policy.completedProcessRetention);
|
|
44
|
+
const abortedRetentionMs = retentionMs(
|
|
45
|
+
input.policy.abortedProcessRetention ?? input.policy.errorProcessRetention,
|
|
46
|
+
);
|
|
42
47
|
const errorRetentionMs = retentionMs(input.policy.errorProcessRetention);
|
|
43
48
|
const nowMs = input.now.getTime();
|
|
44
49
|
const alreadyReleased = input.alreadyReleased ?? new Set<string>();
|
|
@@ -54,9 +59,11 @@ export function planProcessVolumeRetentionCleanup(
|
|
|
54
59
|
const retention =
|
|
55
60
|
process.lifecycleStatus === "completed"
|
|
56
61
|
? completedRetentionMs
|
|
57
|
-
: process.lifecycleStatus === "aborted"
|
|
58
|
-
?
|
|
59
|
-
:
|
|
62
|
+
: process.lifecycleStatus === "aborted"
|
|
63
|
+
? abortedRetentionMs
|
|
64
|
+
: process.lifecycleStatus === "error"
|
|
65
|
+
? errorRetentionMs
|
|
66
|
+
: Number.POSITIVE_INFINITY;
|
|
60
67
|
if (nowMs - closed >= retention) {
|
|
61
68
|
candidates.push(process.id);
|
|
62
69
|
}
|
|
@@ -68,6 +75,8 @@ export function planProcessVolumeRetentionCleanup(
|
|
|
68
75
|
export async function cleanupRetainedProcessVolumes(input: {
|
|
69
76
|
/** @internal */
|
|
70
77
|
volume?: ProcessVolume;
|
|
78
|
+
/** Terminal-process storage disposal policy; error retention is unaffected. @internal */
|
|
79
|
+
reclaimTerminalBackingStorage?: boolean;
|
|
71
80
|
/** @internal */
|
|
72
81
|
processes: readonly ProcessInstance[];
|
|
73
82
|
/** @internal */
|
|
@@ -80,7 +89,21 @@ export async function cleanupRetainedProcessVolumes(input: {
|
|
|
80
89
|
const candidates = planProcessVolumeRetentionCleanup(input);
|
|
81
90
|
if (input.volume) {
|
|
82
91
|
for (const instanceId of candidates) {
|
|
83
|
-
|
|
92
|
+
const process = input.processes.find((process) => process.id === instanceId);
|
|
93
|
+
if (
|
|
94
|
+
input.reclaimTerminalBackingStorage &&
|
|
95
|
+
process?.closedAt &&
|
|
96
|
+
(process.lifecycleStatus === "completed" || process.lifecycleStatus === "aborted")
|
|
97
|
+
) {
|
|
98
|
+
await input.volume.release(instanceId, {
|
|
99
|
+
reclaimBackingStorage: {
|
|
100
|
+
lifecycleStatus: process.lifecycleStatus,
|
|
101
|
+
closedAt: process.closedAt,
|
|
102
|
+
},
|
|
103
|
+
});
|
|
104
|
+
} else {
|
|
105
|
+
await input.volume.release(instanceId);
|
|
106
|
+
}
|
|
84
107
|
}
|
|
85
108
|
}
|
|
86
109
|
return candidates;
|
package/src/types.ts
CHANGED
|
@@ -104,6 +104,14 @@ export interface ProcessStateExportHelperRelayProvider {
|
|
|
104
104
|
}): ProcessStateExportHelperRelay;
|
|
105
105
|
}
|
|
106
106
|
|
|
107
|
+
/** Server authorization to discard a closed process workspace. @internal */
|
|
108
|
+
export interface ProcessVolumeReclamation {
|
|
109
|
+
/** @internal */
|
|
110
|
+
lifecycleStatus: "completed" | "aborted";
|
|
111
|
+
/** @internal */
|
|
112
|
+
closedAt: string;
|
|
113
|
+
}
|
|
114
|
+
|
|
107
115
|
/** @internal */
|
|
108
116
|
export interface ProcessVolume {
|
|
109
117
|
/** Idempotently provisions durable storage for a process instance. @internal */
|
|
@@ -113,7 +121,13 @@ export interface ProcessVolume {
|
|
|
113
121
|
observer?: WorkerStartObserver,
|
|
114
122
|
): Promise<VolumeRef>;
|
|
115
123
|
/** Retention cleanup only. Stopping a worker never calls this. @internal */
|
|
116
|
-
release(
|
|
124
|
+
release(
|
|
125
|
+
instanceId: string,
|
|
126
|
+
options?: {
|
|
127
|
+
/** Reclaim only after the server's terminal-state and reference checks. @internal */
|
|
128
|
+
reclaimBackingStorage?: ProcessVolumeReclamation;
|
|
129
|
+
},
|
|
130
|
+
): Promise<void>;
|
|
117
131
|
/** Permanently deletes every runner-managed resource owned by a deleted process. @internal */
|
|
118
132
|
deleteProcessResources(instanceId: string): Promise<void>;
|
|
119
133
|
}
|