GeezerVison is showing github.com — fix(infra): task-cache telemetry no longer strands an event queued du… · pingdotgg/t3code@e2d3ded
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: infra/task-cache/src/runHook.test.ts +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -162,7 +162,7 @@ it("uses one machine hook across fresh worktrees without modifying Vite+", async | 162 | 162 | expect(await run("build", secondWorktree)).toBe(0); | 163 | 163 | await report(4); | 164 | 164 | expect(await run("build", secondWorktree, true)).toBe(0); | 165 | | - expect(completedReports).toBe(5); | | 165 | + await report(5); | 166 | 166 | expect(await NodeFSP.readFile(entrypoint, "utf8")).toBe(originalEntrypoint); | 167 | 167 | expect(await run("nested", root, false, "--no-cache")).toBe(0); | 168 | 168 | expect(await run("build", nested)).toBe(0); | |
Collapse file Copy file name to clipboardExpand all lines: infra/task-cache/src/telemetry.test.ts +42Lines changed: 42 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -5,6 +5,11 @@ import * as NodePath from "node:path"; | 5 | 5 | import { expect, it, vi } from "vite-plus/test"; | 6 | 6 | import { flushTelemetry, observeSummaries, summarize } from "./telemetry.ts"; | 7 | 7 |
| | 8 | +vi.mock("node:fs", async (importOriginal) => { | | 9 | + const fs = await importOriginal<typeof import("node:fs")>(); | | 10 | + return { ...fs, rmdirSync: vi.fn(fs.rmdirSync) }; | | 11 | +}); | | 12 | + | 8 | 13 | const task = (result: unknown) => ({ | 9 | 14 | package_name: "@team/server", | 10 | 15 | task_name: "typecheck", | @@ -160,3 +165,40 @@ it("drops revoked credentials and continues delivering other queued endpoints", | 160 | 165 | NodeFS.rmSync(home, { recursive: true, force: true }); | 161 | 166 | } | 162 | 167 | }); | | 168 | + | | 169 | +it("delivers an event queued while the previous flusher releases its lock", async () => { | | 170 | + const home = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "cache-handoff-")); | | 171 | + const endpoint = "https://cache.example/projects/t3code"; | | 172 | + try { | | 173 | + const queue = NodePath.join(home, ".cache/t3-task-cache/telemetry"); | | 174 | + const state = NodePath.join(home, ".config/t3-task-cache"); | | 175 | + NodeFS.mkdirSync(queue, { recursive: true }); | | 176 | + NodeFS.mkdirSync(state, { recursive: true }); | | 177 | + NodeFS.writeFileSync( | | 178 | + NodePath.join(state, "credentials.json"), | | 179 | + JSON.stringify({ [endpoint]: { name: "alice", token: "secret-token" } }), | | 180 | + ); | | 181 | + using request = vi | | 182 | + .spyOn(globalThis, "fetch") | | 183 | + .mockImplementation(async () => new Response(null, { status: 204 })); | | 184 | + const rmdirSync = vi.mocked(NodeFS.rmdirSync); | | 185 | + const release = rmdirSync.getMockImplementation()!; | | 186 | + let competing: Promise<void> | undefined; | | 187 | + // The next command queues its event after this flusher's last scan, and its own flusher | | 188 | + // gives up because the lock is still held. | | 189 | + rmdirSync.mockImplementationOnce((path, options) => { | | 190 | + NodeFS.writeFileSync( | | 191 | + NodePath.join(queue, "late.json"), | | 192 | + JSON.stringify({ endpoint, event: { id: "late" } }), | | 193 | + ); | | 194 | + competing = flushTelemetry(home, {}); | | 195 | + release(path, options); | | 196 | + }); | | 197 | + await flushTelemetry(home, {}); | | 198 | + await competing; | | 199 | + expect(request).toHaveBeenCalledOnce(); | | 200 | + expect(NodeFS.readdirSync(queue)).toEqual([]); | | 201 | + } finally { | | 202 | + NodeFS.rmSync(home, { recursive: true, force: true }); | | 203 | + } | | 204 | +}); |
Collapse file Copy file name to clipboardExpand all lines: infra/task-cache/src/telemetry.ts +26-6Lines changed: 26 additions & 6 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -161,6 +161,28 @@ export function observeSummaries( | 161 | 161 |
| 162 | 162 | export async function flushTelemetry(home = NodeOS.homedir(), env = process.env) { | 163 | 163 | const queue = NodePath.join(home, ".cache/t3-task-cache/telemetry"); | | 164 | + const deadline = Date.now() + 15_000; | | 165 | + const skipped = new Set<string>(); | | 166 | + // A flusher that finds the lock taken exits and leaves its event to the holder. Re-check after | | 167 | + // releasing, or an event queued after the holder's last scan waits for the next command. | | 168 | + try { | | 169 | + while ( | | 170 | + (await drainTelemetry(queue, home, env, deadline, skipped)) && | | 171 | + Date.now() < deadline && | | 172 | + NodeFS.readdirSync(queue).some((entry) => entry.endsWith(".json") && !skipped.has(entry)) | | 173 | + ); | | 174 | + } catch { | | 175 | + /* Leave unsent events for a later invocation. */ | | 176 | + } | | 177 | +} | | 178 | + | | 179 | +async function drainTelemetry( | | 180 | + queue: string, | | 181 | + home: string, | | 182 | + env: NodeJS.ProcessEnv, | | 183 | + deadline: number, | | 184 | + skipped: Set<string>, | | 185 | +) { | 164 | 186 | const lock = NodePath.join(queue, "flush.lock"); | 165 | 187 | try { | 166 | 188 | NodeFS.mkdirSync(queue, { recursive: true, mode: 0o700 }); | @@ -171,7 +193,7 @@ export async function flushTelemetry(home = NodeOS.homedir(), env = process.env) | 171 | 193 | } | 172 | 194 | NodeFS.mkdirSync(lock); | 173 | 195 | } catch { | 174 | | - return; | | 196 | + return false; | 175 | 197 | } | 176 | 198 | try { | 177 | 199 | const files = NodeFS.readdirSync(queue) | @@ -188,14 +210,12 @@ export async function flushTelemetry(home = NodeOS.homedir(), env = process.env) | 188 | 210 | const credentials = NodeFS.existsSync(credentialsFile) | 189 | 211 | ? JSON.parse(NodeFS.readFileSync(credentialsFile, "utf8")) | 190 | 212 | : {}; | 191 | | - const deadline = Date.now() + 15_000; | 192 | | - const skipped = new Set<string>(); | 193 | 213 | // Re-scan between sends so a concurrent command's final summary joins this flush. | 194 | 214 | for (let sent = 0; sent < 100 && Date.now() < deadline;) { | 195 | 215 | const file = NodeFS.readdirSync(queue) | 196 | 216 | .filter((entry) => entry.endsWith(".json") && !skipped.has(entry)) | 197 | 217 | .sort()[0]; | 198 | | - if (!file) break; | | 218 | + if (!file) return true; | 199 | 219 | const path = NodePath.join(queue, file); | 200 | 220 | const { endpoint, event } = JSON.parse(NodeFS.readFileSync(path, "utf8")); | 201 | 221 | const url = new URL(endpoint); | @@ -229,7 +249,7 @@ export async function flushTelemetry(home = NodeOS.homedir(), env = process.env) | 229 | 249 | headers: { Authorization: `Bearer ${env.ACTIONS_ID_TOKEN_REQUEST_TOKEN}` }, | 230 | 250 | signal: AbortSignal.timeout(3000), | 231 | 251 | }); | 232 | | - if (!response.ok) return; | | 252 | + if (!response.ok) return false; | 233 | 253 | const token = (await response.json()) as { value: string }; | 234 | 254 | authorization = `Bearer ${token.value}`; | 235 | 255 | } else { | @@ -246,7 +266,7 @@ export async function flushTelemetry(home = NodeOS.homedir(), env = process.env) | 246 | 266 | }); | 247 | 267 | await response.body?.cancel(); | 248 | 268 | if (response.ok || [400, 401, 403, 413].includes(response.status)) NodeFS.unlinkSync(path); | 249 | | - else return; | | 269 | + else return false; | 250 | 270 | } | 251 | 271 | } catch { | 252 | 272 | /* Leave unsent events for a later invocation. */ | |
|