Public: anyone can read this; the crew can change it.
A sleeping forge wakes when its app's own timer falls due #17105
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/16918-a-sleeping-forge-wakes-when-its-app-s-ow → main 0 commits fork
2 files
changed +48
−8
src/spawn/child_runs.ts +18 −8
| @@ -57,9 +57,10 @@ export function crewAtWork(dir: string, now = Date.now()): boolean { | ||
| 57 | 57 | } |
| 58 | 58 | |
| 59 | 59 | /** |
| 60 | * When the earliest run paused on a limit in the child forge whose data is | |
| 61 | * `dir` falls due, in ms; undefined when none waits (or it cannot be read). | |
| 62 | * The host arms a wake for it when the forge goes to sleep (`wakeWhenDue`). | |
| 60 | * When the earliest thing a sleeping child forge owns falls due, in ms: a run paused on a limit, | |
| 61 | * or a timer its own app armed (`core/timers.ts`, `due_at`); undefined when none waits (or it | |
| 62 | * cannot be read). The host arms a wake for it when the forge goes to sleep (`wakeWhenDue`), so | |
| 63 | * scheduled work waits for no visitor. A schema without timers answers by its runs alone. | |
| 63 | 64 | */ |
| 64 | 65 | export function nextRunDue(dir: string): number | undefined { |
| 65 | 66 | const path = join(dir, "forge.db"); |
| @@ -67,11 +68,20 @@ export function nextRunDue(dir: string): number | undefined { | ||
| 67 | 68 | let db: Database | undefined; |
| 68 | 69 | try { |
| 69 | 70 | db = new Database(path, { readonly: true }); |
| 70 | const row = db | |
| 71 | .query<{ at: string | null }, []>("SELECT MIN(not_before) AS at FROM run WHERE state = 'queued' AND not_before IS NOT NULL") | |
| 72 | .get(); | |
| 73 | const at = row?.at == null ? NaN : Date.parse(row.at); | |
| 74 | return Number.isFinite(at) ? at : undefined; | |
| 71 | const first = (sql: string): number | undefined => { | |
| 72 | try { | |
| 73 | const at = db!.query<{ at: string | number | null }, []>(sql).get()?.at; | |
| 74 | const ms = typeof at === "number" ? at : typeof at === "string" ? Date.parse(at) : NaN; | |
| 75 | return Number.isFinite(ms) ? ms : undefined; | |
| 76 | } catch { | |
| 77 | return undefined; | |
| 78 | } | |
| 79 | }; | |
| 80 | const times = [ | |
| 81 | first("SELECT MIN(not_before) AS at FROM run WHERE state = 'queued' AND not_before IS NOT NULL"), | |
| 82 | first("SELECT MIN(due_at) AS at FROM timer WHERE paused = 0"), | |
| 83 | ].filter((t): t is number => t !== undefined); | |
| 84 | return times.length === 0 ? undefined : Math.min(...times); | |
| 75 | 85 | } catch { |
| 76 | 86 | return undefined; |
| 77 | 87 | } finally { |
test/spawn-cell.test.ts +30
| @@ -8,6 +8,7 @@ import { Database } from "bun:sqlite"; | ||
| 8 | 8 | import { childDirOf } from "../src/actions/spawn.ts"; |
| 9 | 9 | import { createRun, queueDepth } from "../src/core/runs.ts"; |
| 10 | 10 | import { createApp } from "../src/app.ts"; |
| 11 | import { armTimer, type Arm } from "../src/core/timers.ts"; | |
| 11 | 12 | import { childEnv } from "../src/apps/process.ts"; |
| 12 | 13 | import { watchedLimits } from "../src/apps/contain.ts"; |
| 13 | 14 | import { childLog, plainLine, cellChildren, crewAtWork, nextRunDue, type CellChild, stopGently, cellEnv, childEnvFor, cellLimits, cellPlacement, cellSpecFor, forward, forwardHeaders, launchSpecFor, ownsBody, socketHeaders } from "../src/spawn/cell.ts"; |
| @@ -341,6 +342,35 @@ describe("a child forge's crew inside its cell", () => { | ||
| 341 | 342 | parent.close(); |
| 342 | 343 | } |
| 343 | 344 | }); |
| 345 | test("an asleep cell whose app's own timer falls due is started at that time, by the same wake (#16918)", async () => { | |
| 346 | const dataDir = join(tmp, "timer-wake"); | |
| 347 | mkdirSync(join(dataDir, "spawn"), { recursive: true }); | |
| 348 | writeFileSync(join(dataDir, "spawn", "kid.json"), JSON.stringify({ name: "kid", bodyId: "cell-timer", size: "small", done: true, placement: "cell" })); | |
| 349 | const parent = createApp({ dataDir, url: "https://f.test" }); | |
| 350 | const dir = childDirOf(parent, "cell-timer"); | |
| 351 | const child = createApp({ dataDir: dir, url: "https://kid--f.test" }); | |
| 352 | const at = Date.now() + 1500; | |
| 353 | const arm: Arm = { appId: "app1", env: "production", key: "digest", at, path: "/jobs/digest", timeoutMs: 1000, retries: 0 }; | |
| 354 | armTimer(child.db, arm, Date.now()); | |
| 355 | child.close(); | |
| 356 | const cells = cellChildren(parent); | |
| 357 | let wokeAt: number | undefined; | |
| 358 | cells.sleepIdle = async () => ["kid"]; | |
| 359 | cells.start = async (input) => { | |
| 360 | wokeAt ??= Date.now(); | |
| 361 | return { ...input, port: 1 } as unknown as CellChild; | |
| 362 | }; | |
| 363 | try { | |
| 364 | expect(nextRunDue(dir)).toBe(at); | |
| 365 | expect(await sleepIdleChildren(parent, 0)).toEqual(["kid"]); | |
| 366 | expect(wokeAt).toBeUndefined(); | |
| 367 | await waitFor(() => wokeAt !== undefined, { within: 5_000 }); | |
| 368 | expect(wokeAt).toBeGreaterThanOrEqual(at); | |
| 369 | } finally { | |
| 370 | closeHostedChildren(parent); | |
| 371 | parent.close(); | |
| 372 | } | |
| 373 | }); | |
| 344 | 374 | }); |
| 345 | 375 | |
| 346 | 376 | describe("a child reaching its parent from inside its cell", () => { |
new above
offline