diff --git a/examples/runner-status/collector.test.ts b/examples/runner-status/collector.test.ts index 3765fb6..f809f40 100644 --- a/examples/runner-status/collector.test.ts +++ b/examples/runner-status/collector.test.ts @@ -1,5 +1,5 @@ import { expect, test } from "bun:test"; -import { boundedSnapshot, emptyCache, eligible, githubList, readConfig, readRepo, refresh, relevant, type List } from "./collector"; +import { boundedSnapshot, emptyCache, eligible, githubList, readConfig, readRepo, refresh, relevant, storedContent, type List } from "./collector"; import type { Job, Run, Runner } from "./types"; const config = { org: "example", repos: ["example/app"], runnerPrefix: "" }; @@ -47,6 +47,16 @@ test("preserves per-source data and timestamps on failure, then clears errors on expect(first.snapshot!.errors).toEqual([]); }); +test("stored content ignores renewed fetch times but not changed data or errors", async () => { + const first = await refresh(config, emptyCache(), fixture, () => "2026-09-07T00:00:00Z"); + const same = await refresh(config, first, fixture, () => "2026-09-07T00:01:00Z"); + expect(storedContent(same)).toBe(storedContent(first)); + const idle = await refresh(config, first, async (endpoint: string) => (endpoint.includes("/actions/runners") ? [{ ...runner, busy: false }] : endpoint.includes("/jobs?") ? [job] : [run]) as T[], () => "2026-09-07T00:01:00Z"); + expect(storedContent(idle)).not.toBe(storedContent(first)); + const failed = await refresh(config, first, async () => { throw new Error("offline"); }, () => "2026-09-07T00:01:00Z"); + expect(storedContent(failed)).not.toBe(storedContent(first)); +}); + test("follows pagination with server credentials and bounds concurrency across callers", async () => { let active = 0, peak = 0, calls = 0; const list = githubList("test-token", new AbortController().signal, (async (input, init) => { diff --git a/examples/runner-status/collector.ts b/examples/runner-status/collector.ts index 81be224..607ef28 100644 --- a/examples/runner-status/collector.ts +++ b/examples/runner-status/collector.ts @@ -109,6 +109,14 @@ export async function refresh(config: Config, previous: Cache, list: List, now = return cache; } +/** The cache without the timestamps that every refresh renews, so a refresh that found nothing new can skip storage. */ +export function storedContent(cache: Cache): string { + return JSON.stringify({ ...cache, runnerFetchedAt: null, + repos: Object.fromEntries(Object.entries(cache.repos).map(([repo, data]) => [repo, { ...data, fetchedAt: null }])), + snapshot: cache.snapshot && { ...cache.snapshot, runnerFetchedAt: null, checkedAt: null, + sources: cache.snapshot.sources.map(source => ({ ...source, fetchedAt: null })) } }); +} + /** Leave room for the MCP result envelope within the bridge's 256 KiB limit. */ export function boundedSnapshot(snapshot: Snapshot): Snapshot { const result = { ...snapshot, runners: [...snapshot.runners], jobs: [...snapshot.jobs], errors: [...snapshot.errors] }; diff --git a/examples/runner-status/worker.test.ts b/examples/runner-status/worker.test.ts index efa252c..94a4218 100644 --- a/examples/runner-status/worker.test.ts +++ b/examples/runner-status/worker.test.ts @@ -46,6 +46,10 @@ test("authenticated viewers share a persisted collector; unauthorized calls neve const deadline = Date.now() + 50_000; while (calls < 12 && Date.now() < deadline) await Bun.sleep(250); expect(calls).toBe(12); + // GitHub reported nothing new, so the alarm poll left the stored cache as it was. + await mf.dispose(); + mf = new Miniflare(options); + expect(await (await mf.dispatchFetch("https://collector/api/status", { headers: auth })).json()).toEqual(responses[0]); } finally { await mf.dispose(); await rm(directory, { recursive: true, force: true }); } }, 60000); diff --git a/examples/runner-status/worker.ts b/examples/runner-status/worker.ts index 6710f3d..ad89e6a 100644 --- a/examples/runner-status/worker.ts +++ b/examples/runner-status/worker.ts @@ -1,7 +1,9 @@ -import { boundedSnapshot, emptyCache, githubList, readConfig, refresh, refreshSeconds, type Cache } from "./collector"; +import { boundedSnapshot, emptyCache, githubList, readConfig, refresh, refreshSeconds, storedContent, type Cache, type Config } from "./collector"; export type Env = { RUNNER_STATUS: DurableObjectNamespace; GITHUB_TOKEN: string; RUNNER_STATUS_TOKEN: string; RUNNER_ORG: string; RUNNER_REPOS: string; RUNNER_NAME_PREFIX?: string }; const headers = { "cache-control": "no-store", "content-type": "application/json", "x-content-type-options": "nosniff" }; +/** Store an unchanged cache at most this often, so a restart restores recent fetch times. */ +const unchangedStoreMs = 10 * 60 * 1000; export default { async fetch(request: Request, env: Env): Promise { @@ -16,6 +18,9 @@ export default { export class RunnerStatus { private cache = emptyCache(); private inFlight: Promise | null = null; + // Each stored cache is about 100 KiB, so storing it on every 45-second poll + // dominates the object's write volume even when GitHub reports nothing new. + private stored: { content: string; at: number } | null = null; constructor(private state: DurableObjectState, private env: Env) { state.blockConcurrencyWhile(async () => { const saved = await state.storage.get<{ config: string; chunks: number }>("manifest"); @@ -27,6 +32,7 @@ export class RunnerStatus { parts.push(part); } this.cache = JSON.parse(parts.join("")) as Cache; + this.stored = { content: storedContent(this.cache), at: Date.now() }; } }); } @@ -40,23 +46,30 @@ export class RunnerStatus { try { const config = readConfig(this.env); this.cache = await refresh(config, this.cache, githubList(this.env.GITHUB_TOKEN, controller.signal)); - // Cache source data can exceed the per-value DO storage limit even when - // the response is small. Commit bounded chunks and their manifest atomically. - const serialized = JSON.stringify(this.cache); - const parts: string[] = []; - // A UTF-16 code unit can encode to three UTF-8 bytes (including lone - // surrogates at a chunk boundary); leave room below the storage limit. - for (let offset = 0; offset < serialized.length; offset += 16 * 1024) parts.push(serialized.slice(offset, offset + 16 * 1024)); - await this.state.storage.transaction(async storage => { - const old = await storage.get<{ chunks: number }>("manifest"); - for (let index = 0; index < parts.length; index++) await storage.put(`cache:${index}`, parts[index]!); - for (let index = parts.length; index < (old?.chunks ?? 0); index++) await storage.delete(`cache:${index}`); - await storage.put("manifest", { config: JSON.stringify(config), chunks: parts.length }); - }); + await this.storeCache(config); } finally { clearTimeout(timer); } })().finally(() => { this.inFlight = null; }); return this.inFlight; } + /** Skips storage when the poll changed nothing but fetch times, unless the stored copy is getting old. */ + private async storeCache(config: Config): Promise { + const content = storedContent(this.cache); + if (this.stored && content === this.stored.content && Date.now() - this.stored.at < unchangedStoreMs) return; + // Cache source data can exceed the per-value DO storage limit even when + // the response is small. Commit bounded chunks and their manifest atomically. + const serialized = JSON.stringify(this.cache); + const parts: string[] = []; + // A UTF-16 code unit can encode to three UTF-8 bytes (including lone + // surrogates at a chunk boundary); leave room below the storage limit. + for (let offset = 0; offset < serialized.length; offset += 16 * 1024) parts.push(serialized.slice(offset, offset + 16 * 1024)); + await this.state.storage.transaction(async storage => { + const old = await storage.get<{ chunks: number }>("manifest"); + for (let index = 0; index < parts.length; index++) await storage.put(`cache:${index}`, parts[index]!); + for (let index = parts.length; index < (old?.chunks ?? 0); index++) await storage.delete(`cache:${index}`); + await storage.put("manifest", { config: JSON.stringify(config), chunks: parts.length }); + }); + this.stored = { content, at: Date.now() }; + } async fetch(): Promise { if (!this.cache.snapshot) await this.poll(); else if (Date.now() - Date.parse(this.cache.snapshot.checkedAt) >= refreshSeconds * 1000) this.state.waitUntil(this.poll());