@moinulmoin/eden-runtime-cloudflare 0.4.1 → 0.6.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/NOTICE +0 -9
- package/dist/eden-eve-host-worker.mjs +415 -1637
- package/dist/eve-host-runtime.d.ts +82 -24
- package/dist/eve-host-runtime.js +335 -49
- package/dist/eve-host.d.ts +46 -29
- package/dist/eve-host.js +162 -114
- package/dist/index.d.ts +1 -1
- package/package.json +3 -4
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { DurableObject, WorkerEntrypoint } from "cloudflare:workers";
|
|
2
2
|
import { type EveHostContainerEnvironment, type EveHostForwardingMetadata, type EveHostReadinessEvidence, type EveScheduleCronEntry } from "./eve-host.js";
|
|
3
|
-
export { ContainerProxy };
|
|
4
3
|
/** The deployment's single SQLite-backed Workflow World. */
|
|
5
4
|
export declare class EdenWorldDurableObject {
|
|
6
5
|
private readonly core;
|
|
@@ -9,13 +8,16 @@ export declare class EdenWorldDurableObject {
|
|
|
9
8
|
alarm(): Promise<void>;
|
|
10
9
|
}
|
|
11
10
|
/**
|
|
12
|
-
* Delivers the container's own HTTPS requests to its public workers.dev
|
|
13
|
-
* back into the
|
|
14
|
-
*
|
|
15
|
-
*
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
*
|
|
11
|
+
* Delivers the container's own HTTPS requests to its public workers.dev
|
|
12
|
+
* origin back into the deployment without crossing the public edge. The
|
|
13
|
+
* Workflow local World posts queue deliveries to `WORKFLOW_LOCAL_BASE_URL`,
|
|
14
|
+
* which must remain the public origin so Eve's externally visible callback
|
|
15
|
+
* URLs stay correct; the DO registers an outbound-HTTPS intercept for that
|
|
16
|
+
* hostname aimed at `EveHostLoopback`, and this function routes the request:
|
|
17
|
+
* non-public hosts egress plainly, `/__eden/world/rpc` reaches the World
|
|
18
|
+
* Durable Object, and everything else re-enters the container over the
|
|
19
|
+
* internal port so the public Worker path can keep refusing the
|
|
20
|
+
* unauthenticated queue endpoints.
|
|
19
21
|
*/
|
|
20
22
|
export declare function routeEveOutboundRequest(request: Request, env: EveHostContainerEnvironment): Promise<Response>;
|
|
21
23
|
/**
|
|
@@ -24,27 +26,83 @@ export declare function routeEveOutboundRequest(request: Request, env: EveHostCo
|
|
|
24
26
|
* package's public declaration surface, so plain-Node consumers never load
|
|
25
27
|
* `cloudflare:workers`-dependent code.
|
|
26
28
|
*/
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
29
|
+
/**
|
|
30
|
+
* The WorkerEntrypoint the container's intercepted outbound HTTPS traffic is
|
|
31
|
+
* delivered to (registered by `EveHostDurableContainer` right after every
|
|
32
|
+
* `ctx.container.start()`). Props carry the DO instance name because
|
|
33
|
+
* entrypoint props cannot hold a Fetcher.
|
|
34
|
+
*/
|
|
35
|
+
export declare class EveHostLoopback extends WorkerEntrypoint<EveHostContainerEnvironment> {
|
|
36
|
+
fetch(request: Request): Promise<Response>;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* The Eve host Durable Object. It drives `ctx.container` directly: the
|
|
40
|
+
* image is selected at `start()` from this Worker version's `images.eve`
|
|
41
|
+
* entry, the writable filesystem is preserved across sleep via a container
|
|
42
|
+
* snapshot, and an update restarts the instance when `inspect()` shows the
|
|
43
|
+
* running image predates the configured one.
|
|
44
|
+
*/
|
|
45
|
+
export declare class EveHostDurableContainer extends DurableObject<EveHostContainerEnvironment> {
|
|
35
46
|
readonly lifecycle: import("./eve-host.js").EveHostLifecycleObserver;
|
|
36
47
|
private readinessStarted;
|
|
48
|
+
/** Evidence is cached only while the container it was proven on keeps running. */
|
|
37
49
|
private readinessEvidence;
|
|
50
|
+
/** True once the running container's image was proven to equal `images.eve`. */
|
|
51
|
+
private imageVerified;
|
|
52
|
+
/**
|
|
53
|
+
* Bumped whenever the Container stops or is reset. Readiness work captures
|
|
54
|
+
* the epoch it started under and may only cache evidence if no stop
|
|
55
|
+
* happened in between.
|
|
56
|
+
*/
|
|
57
|
+
private readinessEpoch;
|
|
58
|
+
private monitorAttached;
|
|
59
|
+
private readonly envVars;
|
|
60
|
+
private readonly sleepAfterMs;
|
|
38
61
|
private readonly readiness;
|
|
39
|
-
constructor(ctx:
|
|
62
|
+
constructor(ctx: DurableObjectState, env: EveHostContainerEnvironment);
|
|
63
|
+
private containerRuntime;
|
|
64
|
+
/** Container lifetime the platform keeps after the DO goes inactive. */
|
|
65
|
+
private get inactivityTimeoutMs();
|
|
66
|
+
private attachMonitor;
|
|
67
|
+
private recordContainerStopped;
|
|
68
|
+
/** Routes the container's own requests to its public origin back inside. */
|
|
69
|
+
private registerLoopbackIntercept;
|
|
70
|
+
/**
|
|
71
|
+
* Records request activity and arms the sleep alarm at
|
|
72
|
+
* `lastActivityAt + sleepAfter`. The alarm — not the inactivity timeout —
|
|
73
|
+
* is what snapshots the writable filesystem before the container stops.
|
|
74
|
+
*/
|
|
75
|
+
private touchActivity;
|
|
76
|
+
/**
|
|
77
|
+
* Starts the container when it is not running. A stored workspace snapshot
|
|
78
|
+
* is restored only when it was captured from the exact image this Worker
|
|
79
|
+
* version configures — restoring an older snapshot silently runs the
|
|
80
|
+
* previous image — so after an image update the container starts fresh and
|
|
81
|
+
* workspace files intentionally do not carry across.
|
|
82
|
+
*/
|
|
83
|
+
private ensureContainerStarted;
|
|
84
|
+
/**
|
|
85
|
+
* Replaces the running container when `inspect()` proves it still serves a
|
|
86
|
+
* pre-update image: this Worker version's `images.eve` is selected at
|
|
87
|
+
* `start()`, so an in-place update must stop the stale instance itself.
|
|
88
|
+
*/
|
|
89
|
+
private reconcileRunningImage;
|
|
40
90
|
ensureEveReady(signal?: AbortSignal): Promise<EveHostReadinessEvidence>;
|
|
41
91
|
fetch(request: Request): Promise<Response>;
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
92
|
+
/**
|
|
93
|
+
* Queue deliveries and loopback traffic enter here: same readiness and
|
|
94
|
+
* forwarding as `fetch` without the deployment-header echo rules (the
|
|
95
|
+
* loopback path is not a deployment-health signal).
|
|
96
|
+
*/
|
|
97
|
+
loopbackFetch(request: Request): Promise<Response>;
|
|
98
|
+
/**
|
|
99
|
+
* The idle-sleep path: once the container has been quiet for `sleepAfter`,
|
|
100
|
+
* snapshot the writable filesystem (so `/workspace` survives the sleep)
|
|
101
|
+
* and destroy the instance. A snapshot failure retries up to
|
|
102
|
+
* `EVE_HOST_SNAPSHOT_MAX_ATTEMPTS` times, then destroys anyway so billing
|
|
103
|
+
* for the running container stops.
|
|
104
|
+
*/
|
|
105
|
+
alarm(): Promise<void>;
|
|
48
106
|
}
|
|
49
107
|
export type EveHostWorkerEnvironment = EveHostContainerEnvironment;
|
|
50
108
|
export interface EveHostWorkerOptions extends EveHostForwardingMetadata {
|
package/dist/eve-host-runtime.js
CHANGED
|
@@ -1,8 +1,33 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { DurableObject, WorkerEntrypoint } from "cloudflare:workers";
|
|
2
2
|
import { WorldCore } from "@moinulmoin/eden-world-cloudflare/core";
|
|
3
3
|
import { EVE_HOST_DEFAULTS, EVE_SCHEDULE_WAKE_LEAD_MS, EveHostError, assertNonEmpty, assertStableOrigin, createEveHostLifecycleObserver, createEveReadinessGate, createTrustedEveRequest, eveScheduleCronFiresWithin, IDENTIFIER_PATTERN, readProtectedRuntimeVariables, WORKER_NAME_PATTERN, isEveWorkflowInternalRoute, } from "./eve-host.js";
|
|
4
|
-
export { ContainerProxy };
|
|
5
4
|
const EVE_CLOUDFLARE_CONTAINERS_CA_PATH = "/etc/cloudflare/certs/cloudflare-containers-ca.crt";
|
|
5
|
+
/** Durable Object storage keys owned by `EveHostDurableContainer`. */
|
|
6
|
+
const EVE_HOST_CONTAINER_ACTIVITY_KEY = "eden.eve.host.last-activity-at";
|
|
7
|
+
const EVE_HOST_CONTAINER_SNAPSHOT_KEY = "eden.eve.host.workspace-snapshot";
|
|
8
|
+
const EVE_HOST_CONTAINER_SNAPSHOT_ATTEMPTS_KEY = "eden.eve.host.snapshot-attempts";
|
|
9
|
+
/** Snapshot attempts the alarm retries before destroying anyway so a stuck
|
|
10
|
+
* snapshot path never keeps billing alive forever. */
|
|
11
|
+
const EVE_HOST_SNAPSHOT_MAX_ATTEMPTS = 3;
|
|
12
|
+
/** Parses the `sleepAfter` duration spellings wrangler accepts ("300ms",
|
|
13
|
+
* "30s", "15m", "24h", "7d", or a bare number of seconds). */
|
|
14
|
+
function sleepAfterDurationMs(value) {
|
|
15
|
+
const match = /^([0-9]+)(ms|s|m|h|d)?$/u.exec(value.trim());
|
|
16
|
+
if (match === null)
|
|
17
|
+
return undefined;
|
|
18
|
+
const amount = Number.parseInt(match[1] ?? "", 10);
|
|
19
|
+
const unit = match[2] ?? "s";
|
|
20
|
+
const factor = unit === "ms"
|
|
21
|
+
? 1
|
|
22
|
+
: unit === "s"
|
|
23
|
+
? 1_000
|
|
24
|
+
: unit === "m"
|
|
25
|
+
? 60_000
|
|
26
|
+
: unit === "h"
|
|
27
|
+
? 3_600_000
|
|
28
|
+
: 86_400_000;
|
|
29
|
+
return amount * factor;
|
|
30
|
+
}
|
|
6
31
|
/** The deployment's single SQLite-backed Workflow World. */
|
|
7
32
|
export class EdenWorldDurableObject {
|
|
8
33
|
core;
|
|
@@ -28,7 +53,7 @@ export class EdenWorldDurableObject {
|
|
|
28
53
|
throw new Error("The World queue Container identity is unavailable.");
|
|
29
54
|
}
|
|
30
55
|
const namespace = env[binding];
|
|
31
|
-
const response = await namespace.getByName(instance).
|
|
56
|
+
const response = await namespace.getByName(instance).loopbackFetch(new Request(new URL(`/.well-known/workflow/v1/${message.path}`, origin), {
|
|
32
57
|
method: "POST",
|
|
33
58
|
headers: {
|
|
34
59
|
...message.headers,
|
|
@@ -38,7 +63,7 @@ export class EdenWorldDurableObject {
|
|
|
38
63
|
"x-vqs-message-attempt": String(message.attempt),
|
|
39
64
|
},
|
|
40
65
|
body: message.body,
|
|
41
|
-
})
|
|
66
|
+
}));
|
|
42
67
|
// Body transport errors must remain retryable; an HTTP status alone
|
|
43
68
|
// is not an acknowledgement of a streamed Container response.
|
|
44
69
|
const body = await response.text();
|
|
@@ -74,13 +99,16 @@ export class EdenWorldDurableObject {
|
|
|
74
99
|
}
|
|
75
100
|
}
|
|
76
101
|
/**
|
|
77
|
-
* Delivers the container's own HTTPS requests to its public workers.dev
|
|
78
|
-
* back into the
|
|
79
|
-
*
|
|
80
|
-
*
|
|
81
|
-
*
|
|
82
|
-
*
|
|
83
|
-
*
|
|
102
|
+
* Delivers the container's own HTTPS requests to its public workers.dev
|
|
103
|
+
* origin back into the deployment without crossing the public edge. The
|
|
104
|
+
* Workflow local World posts queue deliveries to `WORKFLOW_LOCAL_BASE_URL`,
|
|
105
|
+
* which must remain the public origin so Eve's externally visible callback
|
|
106
|
+
* URLs stay correct; the DO registers an outbound-HTTPS intercept for that
|
|
107
|
+
* hostname aimed at `EveHostLoopback`, and this function routes the request:
|
|
108
|
+
* non-public hosts egress plainly, `/__eden/world/rpc` reaches the World
|
|
109
|
+
* Durable Object, and everything else re-enters the container over the
|
|
110
|
+
* internal port so the public Worker path can keep refusing the
|
|
111
|
+
* unauthenticated queue endpoints.
|
|
84
112
|
*/
|
|
85
113
|
export async function routeEveOutboundRequest(request, env) {
|
|
86
114
|
const publicOrigin = env.EVE_PUBLIC_ORIGIN;
|
|
@@ -108,20 +136,48 @@ export async function routeEveOutboundRequest(request, env) {
|
|
|
108
136
|
if (namespace === undefined) {
|
|
109
137
|
throw new EveHostError("HOST_READINESS_UNPROVEN", "The configured Container binding is unavailable to the outbound handler.");
|
|
110
138
|
}
|
|
111
|
-
return namespace
|
|
112
|
-
|
|
113
|
-
|
|
139
|
+
return namespace.getByName(instanceName).loopbackFetch(request);
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* This module is the only Eve-host surface that depends on provider runtime
|
|
143
|
+
* packages. It is bundled into the deployed Worker directory instead of the
|
|
144
|
+
* package's public declaration surface, so plain-Node consumers never load
|
|
145
|
+
* `cloudflare:workers`-dependent code.
|
|
146
|
+
*/
|
|
147
|
+
/**
|
|
148
|
+
* The WorkerEntrypoint the container's intercepted outbound HTTPS traffic is
|
|
149
|
+
* delivered to (registered by `EveHostDurableContainer` right after every
|
|
150
|
+
* `ctx.container.start()`). Props carry the DO instance name because
|
|
151
|
+
* entrypoint props cannot hold a Fetcher.
|
|
152
|
+
*/
|
|
153
|
+
export class EveHostLoopback extends WorkerEntrypoint {
|
|
154
|
+
async fetch(request) {
|
|
155
|
+
return routeEveOutboundRequest(request, this.env);
|
|
156
|
+
}
|
|
114
157
|
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
158
|
+
/**
|
|
159
|
+
* The Eve host Durable Object. It drives `ctx.container` directly: the
|
|
160
|
+
* image is selected at `start()` from this Worker version's `images.eve`
|
|
161
|
+
* entry, the writable filesystem is preserved across sleep via a container
|
|
162
|
+
* snapshot, and an update restarts the instance when `inspect()` shows the
|
|
163
|
+
* running image predates the configured one.
|
|
164
|
+
*/
|
|
165
|
+
export class EveHostDurableContainer extends DurableObject {
|
|
122
166
|
lifecycle = createEveHostLifecycleObserver();
|
|
123
167
|
readinessStarted = false;
|
|
168
|
+
/** Evidence is cached only while the container it was proven on keeps running. */
|
|
124
169
|
readinessEvidence;
|
|
170
|
+
/** True once the running container's image was proven to equal `images.eve`. */
|
|
171
|
+
imageVerified = false;
|
|
172
|
+
/**
|
|
173
|
+
* Bumped whenever the Container stops or is reset. Readiness work captures
|
|
174
|
+
* the epoch it started under and may only cache evidence if no stop
|
|
175
|
+
* happened in between.
|
|
176
|
+
*/
|
|
177
|
+
readinessEpoch = 0;
|
|
178
|
+
monitorAttached = false;
|
|
179
|
+
envVars;
|
|
180
|
+
sleepAfterMs;
|
|
125
181
|
readiness;
|
|
126
182
|
constructor(ctx, env) {
|
|
127
183
|
super(ctx, env);
|
|
@@ -135,7 +191,6 @@ export class EveHostContainer extends Container {
|
|
|
135
191
|
}
|
|
136
192
|
assertStableOrigin(publicOrigin);
|
|
137
193
|
assertNonEmpty(deploymentId, "deployment identity");
|
|
138
|
-
assertNonEmpty(generationId, "generation identity");
|
|
139
194
|
const runtimeEnv = readProtectedRuntimeVariables(env);
|
|
140
195
|
this.envVars = {
|
|
141
196
|
...runtimeEnv,
|
|
@@ -161,18 +216,154 @@ export class EveHostContainer extends Container {
|
|
|
161
216
|
: { EDEN_EVE_RUNTIME_REVISION: env.EDEN_EVE_RUNTIME_REVISION }),
|
|
162
217
|
};
|
|
163
218
|
const sleepAfterOverride = env.EDEN_EVE_CONTAINER_SLEEP_AFTER;
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
219
|
+
this.sleepAfterMs =
|
|
220
|
+
sleepAfterOverride === undefined
|
|
221
|
+
? sleepAfterDurationMs(EVE_HOST_DEFAULTS.sleepAfter) ?? 86_400_000
|
|
222
|
+
: sleepAfterDurationMs(sleepAfterOverride) ??
|
|
223
|
+
(() => {
|
|
224
|
+
throw new EveHostError("HOST_READINESS_UNPROVEN", "The Container sleepAfter override is not a parseable duration.");
|
|
225
|
+
})();
|
|
170
226
|
this.readiness = createEveReadinessGate({
|
|
171
|
-
|
|
172
|
-
|
|
227
|
+
start: async () => {
|
|
228
|
+
await this.ensureContainerStarted();
|
|
229
|
+
},
|
|
230
|
+
healthFetch: (request) => {
|
|
231
|
+
const container = this.containerRuntime();
|
|
232
|
+
if (container === undefined) {
|
|
233
|
+
throw new EveHostError("HOST_READINESS_UNPROVEN", "The Container runtime is unavailable.");
|
|
234
|
+
}
|
|
235
|
+
const url = new URL(request.url);
|
|
236
|
+
return container
|
|
237
|
+
.getTcpPort(EVE_HOST_DEFAULTS.internalPort)
|
|
238
|
+
.fetch(new Request(`http://container${url.pathname}${url.search}`, request));
|
|
239
|
+
},
|
|
173
240
|
});
|
|
241
|
+
const runtime = this.containerRuntime();
|
|
242
|
+
if (runtime?.running === true) {
|
|
243
|
+
// DO restarts lose the inactivity timeout; the monitor for this
|
|
244
|
+
// container generation is also gone, so both are re-attached here.
|
|
245
|
+
this.ctx.blockConcurrencyWhile(async () => {
|
|
246
|
+
await runtime.setInactivityTimeout(this.inactivityTimeoutMs);
|
|
247
|
+
this.attachMonitor(runtime);
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
containerRuntime() {
|
|
252
|
+
return this.ctx.container;
|
|
253
|
+
}
|
|
254
|
+
/** Container lifetime the platform keeps after the DO goes inactive. */
|
|
255
|
+
get inactivityTimeoutMs() {
|
|
256
|
+
return this.sleepAfterMs + 15 * 60_000;
|
|
257
|
+
}
|
|
258
|
+
attachMonitor(runtime) {
|
|
259
|
+
if (this.monitorAttached)
|
|
260
|
+
return;
|
|
261
|
+
this.monitorAttached = true;
|
|
262
|
+
runtime.monitor().then(() => {
|
|
263
|
+
this.monitorAttached = false;
|
|
264
|
+
this.recordContainerStopped("exit");
|
|
265
|
+
}, (error) => {
|
|
266
|
+
this.monitorAttached = false;
|
|
267
|
+
this.recordContainerStopped(error instanceof Error ? error.name : "runtime_signal");
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
recordContainerStopped(reason) {
|
|
271
|
+
this.readinessEpoch += 1;
|
|
272
|
+
this.readinessStarted = false;
|
|
273
|
+
this.readinessEvidence = undefined;
|
|
274
|
+
this.imageVerified = false;
|
|
275
|
+
this.readiness.reset();
|
|
276
|
+
this.lifecycle.record("stopped", reason);
|
|
277
|
+
}
|
|
278
|
+
/** Routes the container's own requests to its public origin back inside. */
|
|
279
|
+
async registerLoopbackIntercept(runtime) {
|
|
280
|
+
const exportsMap = this.ctx.exports;
|
|
281
|
+
const entryFactory = exportsMap?.["EveHostLoopback"];
|
|
282
|
+
if (typeof entryFactory !== "function")
|
|
283
|
+
return;
|
|
284
|
+
const entrypoint = entryFactory({ props: {} });
|
|
285
|
+
await runtime.interceptOutboundHttps(new URL(this.env.EVE_PUBLIC_ORIGIN ?? "").hostname, entrypoint);
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* Records request activity and arms the sleep alarm at
|
|
289
|
+
* `lastActivityAt + sleepAfter`. The alarm — not the inactivity timeout —
|
|
290
|
+
* is what snapshots the writable filesystem before the container stops.
|
|
291
|
+
*/
|
|
292
|
+
async touchActivity() {
|
|
293
|
+
const now = Date.now();
|
|
294
|
+
await this.ctx.storage.put(EVE_HOST_CONTAINER_ACTIVITY_KEY, now);
|
|
295
|
+
await this.ctx.storage.setAlarm(now + this.sleepAfterMs);
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Starts the container when it is not running. A stored workspace snapshot
|
|
299
|
+
* is restored only when it was captured from the exact image this Worker
|
|
300
|
+
* version configures — restoring an older snapshot silently runs the
|
|
301
|
+
* previous image — so after an image update the container starts fresh and
|
|
302
|
+
* workspace files intentionally do not carry across.
|
|
303
|
+
*/
|
|
304
|
+
async ensureContainerStarted() {
|
|
305
|
+
const runtime = this.containerRuntime();
|
|
306
|
+
if (runtime === undefined) {
|
|
307
|
+
throw new EveHostError("HOST_READINESS_UNPROVEN", "The Container runtime is unavailable on this Durable Object.");
|
|
308
|
+
}
|
|
309
|
+
if (runtime.running)
|
|
310
|
+
return;
|
|
311
|
+
const expectedImage = runtime.images["eve"];
|
|
312
|
+
if (expectedImage === undefined) {
|
|
313
|
+
throw new EveHostError("HOST_READINESS_UNPROVEN", "The configured Container image is unavailable to this Worker version.");
|
|
314
|
+
}
|
|
315
|
+
const storedSnapshot = await this.ctx.storage.get(EVE_HOST_CONTAINER_SNAPSHOT_KEY);
|
|
316
|
+
if (this.lifecycle.events.some((event) => event.type === "started")) {
|
|
317
|
+
this.lifecycle.record("replaced");
|
|
318
|
+
}
|
|
319
|
+
runtime.start({
|
|
320
|
+
...(storedSnapshot !== undefined && storedSnapshot.image === expectedImage
|
|
321
|
+
? { containerSnapshot: { id: storedSnapshot.id } }
|
|
322
|
+
: { image: expectedImage }),
|
|
323
|
+
instance: EVE_HOST_DEFAULTS.instance,
|
|
324
|
+
enableInternet: true,
|
|
325
|
+
env: this.envVars,
|
|
326
|
+
});
|
|
327
|
+
// Intercepts do not survive a container start and late registration lags;
|
|
328
|
+
// register inside the same block before readiness polling.
|
|
329
|
+
await this.registerLoopbackIntercept(runtime);
|
|
330
|
+
this.attachMonitor(runtime);
|
|
331
|
+
await runtime.setInactivityTimeout(this.inactivityTimeoutMs);
|
|
332
|
+
this.lifecycle.record("started");
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* Replaces the running container when `inspect()` proves it still serves a
|
|
336
|
+
* pre-update image: this Worker version's `images.eve` is selected at
|
|
337
|
+
* `start()`, so an in-place update must stop the stale instance itself.
|
|
338
|
+
*/
|
|
339
|
+
async reconcileRunningImage() {
|
|
340
|
+
if (this.imageVerified)
|
|
341
|
+
return;
|
|
342
|
+
const runtime = this.containerRuntime();
|
|
343
|
+
if (runtime === undefined || !runtime.running)
|
|
344
|
+
return;
|
|
345
|
+
const expectedImage = runtime.images["eve"];
|
|
346
|
+
let info;
|
|
347
|
+
try {
|
|
348
|
+
info = await runtime.inspect();
|
|
349
|
+
}
|
|
350
|
+
catch {
|
|
351
|
+
info = null;
|
|
352
|
+
}
|
|
353
|
+
if (info?.image === expectedImage) {
|
|
354
|
+
this.imageVerified = true;
|
|
355
|
+
return;
|
|
356
|
+
}
|
|
357
|
+
this.lifecycle.record("image_mismatch");
|
|
358
|
+
this.recordContainerStopped("replaced");
|
|
359
|
+
await runtime.destroy();
|
|
360
|
+
const stoppedAt = Date.now();
|
|
361
|
+
while (runtime.running && Date.now() - stoppedAt < 10_000) {
|
|
362
|
+
await new Promise((resolve) => setTimeout(resolve, 25));
|
|
363
|
+
}
|
|
174
364
|
}
|
|
175
365
|
async ensureEveReady(signal = new AbortController().signal) {
|
|
366
|
+
await this.reconcileRunningImage();
|
|
176
367
|
if (this.readinessEvidence !== undefined) {
|
|
177
368
|
return this.readinessEvidence;
|
|
178
369
|
}
|
|
@@ -180,33 +371,122 @@ export class EveHostContainer extends Container {
|
|
|
180
371
|
this.readinessStarted = true;
|
|
181
372
|
this.lifecycle.record("start_requested");
|
|
182
373
|
}
|
|
374
|
+
const epoch = this.readinessEpoch;
|
|
183
375
|
const evidence = await this.readiness(signal);
|
|
184
|
-
this.
|
|
376
|
+
if (epoch === this.readinessEpoch) {
|
|
377
|
+
this.readinessEvidence = evidence;
|
|
378
|
+
// Readiness under this Worker version proves the running container
|
|
379
|
+
// serves `images.eve`: it was either verified by inspect() or freshly
|
|
380
|
+
// started from the configured image above.
|
|
381
|
+
this.imageVerified = true;
|
|
382
|
+
}
|
|
185
383
|
this.lifecycle.record("health_ready", evidence.healthStatus);
|
|
186
384
|
return evidence;
|
|
187
385
|
}
|
|
188
386
|
async fetch(request) {
|
|
387
|
+
const requestDeploymentId = request.headers.get("x-eden-eve-deployment-id") ?? undefined;
|
|
189
388
|
await this.ensureEveReady(request.signal);
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
this.lifecycle.record("replaced");
|
|
389
|
+
await this.touchActivity();
|
|
390
|
+
const runtime = this.containerRuntime();
|
|
391
|
+
if (runtime === undefined) {
|
|
392
|
+
throw new EveHostError("HOST_READINESS_UNPROVEN", "The Container runtime is unavailable on this Durable Object.");
|
|
195
393
|
}
|
|
196
|
-
|
|
394
|
+
const url = new URL(request.url);
|
|
395
|
+
const response = await runtime
|
|
396
|
+
.getTcpPort(EVE_HOST_DEFAULTS.internalPort)
|
|
397
|
+
.fetch(new Request(`http://container${url.pathname}${url.search}`, request));
|
|
398
|
+
// Reporting the verified start back lets deploy health-gate promotion on
|
|
399
|
+
// the served container actually running this generation's image: the
|
|
400
|
+
// header echoes only when the running container's image equals this
|
|
401
|
+
// Worker version's configured image and the request names this
|
|
402
|
+
// deployment.
|
|
403
|
+
if (requestDeploymentId !== undefined &&
|
|
404
|
+
requestDeploymentId === this.env.EDEN_EVE_DEPLOYMENT_ID &&
|
|
405
|
+
this.readinessEvidence !== undefined &&
|
|
406
|
+
this.imageVerified) {
|
|
407
|
+
const headers = new Headers(response.headers);
|
|
408
|
+
headers.set("x-eden-eve-started-deployment", requestDeploymentId);
|
|
409
|
+
return new Response(response.body, {
|
|
410
|
+
status: response.status,
|
|
411
|
+
statusText: response.statusText,
|
|
412
|
+
headers,
|
|
413
|
+
});
|
|
414
|
+
}
|
|
415
|
+
return response;
|
|
197
416
|
}
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
417
|
+
/**
|
|
418
|
+
* Queue deliveries and loopback traffic enter here: same readiness and
|
|
419
|
+
* forwarding as `fetch` without the deployment-header echo rules (the
|
|
420
|
+
* loopback path is not a deployment-health signal).
|
|
421
|
+
*/
|
|
422
|
+
async loopbackFetch(request) {
|
|
423
|
+
await this.ensureEveReady(request.signal);
|
|
424
|
+
await this.touchActivity();
|
|
425
|
+
const runtime = this.containerRuntime();
|
|
426
|
+
if (runtime === undefined) {
|
|
427
|
+
throw new EveHostError("HOST_READINESS_UNPROVEN", "The Container runtime is unavailable on this Durable Object.");
|
|
428
|
+
}
|
|
429
|
+
const url = new URL(request.url);
|
|
430
|
+
return runtime
|
|
431
|
+
.getTcpPort(EVE_HOST_DEFAULTS.internalPort)
|
|
432
|
+
.fetch(new Request(`http://container${url.pathname}${url.search}`, request));
|
|
203
433
|
}
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
434
|
+
/**
|
|
435
|
+
* The idle-sleep path: once the container has been quiet for `sleepAfter`,
|
|
436
|
+
* snapshot the writable filesystem (so `/workspace` survives the sleep)
|
|
437
|
+
* and destroy the instance. A snapshot failure retries up to
|
|
438
|
+
* `EVE_HOST_SNAPSHOT_MAX_ATTEMPTS` times, then destroys anyway so billing
|
|
439
|
+
* for the running container stops.
|
|
440
|
+
*/
|
|
441
|
+
async alarm() {
|
|
442
|
+
const runtime = this.containerRuntime();
|
|
443
|
+
const lastActivity = (await this.ctx.storage.get(EVE_HOST_CONTAINER_ACTIVITY_KEY)) ?? 0;
|
|
444
|
+
const now = Date.now();
|
|
445
|
+
if (now - lastActivity < this.sleepAfterMs) {
|
|
446
|
+
await this.ctx.storage.setAlarm(lastActivity + this.sleepAfterMs);
|
|
447
|
+
return;
|
|
448
|
+
}
|
|
449
|
+
if (runtime === undefined || !runtime.running)
|
|
450
|
+
return;
|
|
451
|
+
const attempts = (await this.ctx.storage.get(EVE_HOST_CONTAINER_SNAPSHOT_ATTEMPTS_KEY)) ?? 0;
|
|
452
|
+
try {
|
|
453
|
+
const snapshot = await runtime.snapshotContainer({
|
|
454
|
+
name: "eden-workspace",
|
|
455
|
+
});
|
|
456
|
+
const image = (await runtime.inspect().catch(() => null))?.image ??
|
|
457
|
+
runtime.images["eve"] ??
|
|
458
|
+
"";
|
|
459
|
+
const record = {
|
|
460
|
+
id: snapshot.id,
|
|
461
|
+
image,
|
|
462
|
+
at: now,
|
|
463
|
+
};
|
|
464
|
+
await this.ctx.storage.put(EVE_HOST_CONTAINER_SNAPSHOT_KEY, record);
|
|
465
|
+
await this.ctx.storage.put(EVE_HOST_CONTAINER_SNAPSHOT_ATTEMPTS_KEY, 0);
|
|
466
|
+
// The snapshot takes seconds and requests may interleave with it; a
|
|
467
|
+
// request that arrived meanwhile must not have its container destroyed
|
|
468
|
+
// underneath it. Keep running and re-arm sleep from that activity.
|
|
469
|
+
const activityAfterSnapshot = (await this.ctx.storage.get(EVE_HOST_CONTAINER_ACTIVITY_KEY)) ?? 0;
|
|
470
|
+
if (activityAfterSnapshot !== lastActivity) {
|
|
471
|
+
await this.ctx.storage.setAlarm(activityAfterSnapshot + this.sleepAfterMs);
|
|
472
|
+
return;
|
|
473
|
+
}
|
|
474
|
+
await runtime.destroy(new Error("idle-snapshot-done"));
|
|
475
|
+
this.recordContainerStopped("snapshot:idle");
|
|
476
|
+
}
|
|
477
|
+
catch (error) {
|
|
478
|
+
const message = error instanceof Error ? error.message : "unknown";
|
|
479
|
+
if (attempts + 1 >= EVE_HOST_SNAPSHOT_MAX_ATTEMPTS) {
|
|
480
|
+
console.error(`The Eve workspace snapshot failed ${attempts + 1} times; destroying the container without it: ${message}`);
|
|
481
|
+
await this.ctx.storage.put(EVE_HOST_CONTAINER_SNAPSHOT_ATTEMPTS_KEY, 0);
|
|
482
|
+
await runtime.destroy(new Error("idle-snapshot-abandoned"));
|
|
483
|
+
this.recordContainerStopped("snapshot:abandoned");
|
|
484
|
+
return;
|
|
485
|
+
}
|
|
486
|
+
console.error(`The Eve workspace snapshot failed (attempt ${attempts + 1}): ${message}`);
|
|
487
|
+
await this.ctx.storage.put(EVE_HOST_CONTAINER_SNAPSHOT_ATTEMPTS_KEY, attempts + 1);
|
|
488
|
+
await this.ctx.storage.setAlarm(now + 60_000);
|
|
489
|
+
}
|
|
210
490
|
}
|
|
211
491
|
}
|
|
212
492
|
export function createEveHostWorker(options) {
|
|
@@ -270,7 +550,13 @@ export function createEveHostWorker(options) {
|
|
|
270
550
|
// Wake only: the in-process Nitro runner inside the container
|
|
271
551
|
// fires the tick itself, so the schedule runs exactly once.
|
|
272
552
|
ctx.waitUntil(resolveContainer(env)
|
|
273
|
-
.fetch(new Request(new URL(EVE_HOST_DEFAULTS.healthPath, options.publicOrigin)
|
|
553
|
+
.fetch(new Request(new URL(EVE_HOST_DEFAULTS.healthPath, options.publicOrigin), {
|
|
554
|
+
headers: {
|
|
555
|
+
// Stamp the Worker's own deployment so a stale Durable
|
|
556
|
+
// Object never falls back to its own environment.
|
|
557
|
+
"x-eden-eve-deployment-id": options.deploymentId,
|
|
558
|
+
},
|
|
559
|
+
}))
|
|
274
560
|
.catch((error) => {
|
|
275
561
|
console.error("The scheduled wake request failed:", error instanceof Error ? error.message : "unknown");
|
|
276
562
|
}));
|