|
| 1 | +import { startTestServer, type TestServer } from "@internal/testcontainers/webapp"; |
| 2 | +import { ApiClient } from "@trigger.dev/core/v3"; |
| 3 | +import { SupervisorHttpClient } from "@trigger.dev/core/v3/runEngineWorker"; |
| 4 | +import { setTimeout } from "node:timers/promises"; |
| 5 | +import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; |
| 6 | +import { |
| 7 | + MANAGED_WORKER_SECRET, |
| 8 | + seedEngineFixtures, |
| 9 | + workerHeaders, |
| 10 | + type EngineFixtures, |
| 11 | +} from "../../test/bench/lib/engineFixtures"; |
| 12 | + |
| 13 | +const INSTANCE_NAME = "attempt-complete-e2e"; |
| 14 | + |
| 15 | +let server: TestServer; |
| 16 | +let fixtures: EngineFixtures; |
| 17 | +let api: ApiClient; |
| 18 | +let supervisor: SupervisorHttpClient; |
| 19 | + |
| 20 | +beforeAll(async () => { |
| 21 | + server = await startTestServer({ overrideEnv: { RUN_ENGINE_WORKER_ENABLED: "1" } }); |
| 22 | + fixtures = await seedEngineFixtures(server.prisma, { taskCount: 1 }); |
| 23 | + api = new ApiClient(server.webapp.baseUrl, fixtures.environmentApiKey); |
| 24 | + supervisor = new SupervisorHttpClient({ |
| 25 | + apiUrl: server.webapp.baseUrl, |
| 26 | + workerToken: fixtures.workerGroupToken, |
| 27 | + instanceName: INSTANCE_NAME, |
| 28 | + managedWorkerSecret: MANAGED_WORKER_SECRET, |
| 29 | + }); |
| 30 | +}, 180_000); |
| 31 | + |
| 32 | +afterAll(async () => { |
| 33 | + await server?.stop(); |
| 34 | +}, 120_000); |
| 35 | + |
| 36 | +describe("attempt completion replayed after it was applied", () => { |
| 37 | + it("refuses the replay with the latest snapshot and applies its metadata only once", async () => { |
| 38 | + const { runId, snapshotId } = await startAttempt(); |
| 39 | + const completion = { |
| 40 | + ok: false, |
| 41 | + id: runId, |
| 42 | + error: { type: "BUILT_IN_ERROR", name: "UserError", message: "boom", stackTrace: "" }, |
| 43 | + retry: { timestamp: Date.now(), delay: 0 }, |
| 44 | + flushedMetadata: { |
| 45 | + data: JSON.stringify({ operations: [{ type: "increment", key: "count", value: 1 }] }), |
| 46 | + dataType: "application/json", |
| 47 | + }, |
| 48 | + }; |
| 49 | + |
| 50 | + const first = await completeAttempt(runId, snapshotId, completion); |
| 51 | + expect(first.status).toBe(200); |
| 52 | + expect(first.body.result.attemptStatus).toBe("RETRY_IMMEDIATELY"); |
| 53 | + await vi.waitFor(async () => expect(await metadataCount(runId)).toBe(1), { |
| 54 | + timeout: 5_000, |
| 55 | + interval: 50, |
| 56 | + }); |
| 57 | + |
| 58 | + const replay = await completeAttempt(runId, snapshotId, completion); |
| 59 | + await setTimeout(500); |
| 60 | + expect(await metadataCount(runId)).toBe(1); |
| 61 | + |
| 62 | + expect(replay.status).toBe(400); |
| 63 | + expect(replay.body).toEqual({ |
| 64 | + error: "Snapshot ID doesn't match the latest snapshot", |
| 65 | + code: "SNAPSHOT_CONFLICT", |
| 66 | + latestSnapshotId: first.body.result.snapshot.friendlyId, |
| 67 | + }); |
| 68 | + }, 60_000); |
| 69 | +}); |
| 70 | + |
| 71 | +async function startAttempt() { |
| 72 | + const { id: runId } = await api.triggerTask(fixtures.taskIdentifiers[0]!, { |
| 73 | + payload: {}, |
| 74 | + options: { maxAttempts: 3 }, |
| 75 | + }); |
| 76 | + |
| 77 | + const dequeued = await vi.waitFor( |
| 78 | + async () => { |
| 79 | + const result = await supervisor.dequeue({}); |
| 80 | + if (!result.success) throw new Error(result.error); |
| 81 | + const message = result.data.find((m) => m.run.friendlyId === runId); |
| 82 | + if (!message) throw new Error(`${runId} was not dequeued`); |
| 83 | + return message; |
| 84 | + }, |
| 85 | + { timeout: 10_000, interval: 100 } |
| 86 | + ); |
| 87 | + |
| 88 | + const started = await supervisor.startRunAttempt(runId, dequeued.snapshot.friendlyId, {}); |
| 89 | + if (!started.success) throw new Error(started.error); |
| 90 | + |
| 91 | + return { runId, snapshotId: started.data.snapshot.friendlyId }; |
| 92 | +} |
| 93 | + |
| 94 | +async function completeAttempt(runId: string, snapshotId: string, completion: unknown) { |
| 95 | + const response = await server.webapp.fetch( |
| 96 | + `/engine/v1/worker-actions/runs/${runId}/snapshots/${snapshotId}/attempts/complete`, |
| 97 | + { |
| 98 | + method: "POST", |
| 99 | + headers: workerHeaders(fixtures, INSTANCE_NAME), |
| 100 | + body: JSON.stringify({ completion }), |
| 101 | + } |
| 102 | + ); |
| 103 | + |
| 104 | + return { status: response.status, body: await response.json() }; |
| 105 | +} |
| 106 | + |
| 107 | +async function metadataCount(runId: string) { |
| 108 | + const { metadata } = await server.prisma.taskRun.findFirstOrThrow({ |
| 109 | + where: { friendlyId: runId }, |
| 110 | + select: { metadata: true }, |
| 111 | + }); |
| 112 | + return metadata ? JSON.parse(metadata).count : undefined; |
| 113 | +} |
0 commit comments