|
| 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 { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; |
| 5 | +import { MANAGED_WORKER_SECRET, seedEngineFixtures } from "../../test/bench/lib/engineFixtures"; |
| 6 | + |
| 7 | +const RUNNER_ID = "runner-release-e2e"; |
| 8 | + |
| 9 | +let server: TestServer; |
| 10 | + |
| 11 | +beforeAll(async () => { |
| 12 | + server = await startTestServer({ |
| 13 | + // Only a release can requeue the run within these tests. |
| 14 | + extraEnv: { RUN_ENGINE_TIMEOUT_PENDING_EXECUTING: "600000" }, |
| 15 | + // The engine worker moves runs from the master queue to the worker queue. |
| 16 | + overrideEnv: { RUN_ENGINE_WORKER_ENABLED: "1" }, |
| 17 | + }); |
| 18 | +}, 180_000); |
| 19 | + |
| 20 | +afterAll(async () => { |
| 21 | + await server?.stop(); |
| 22 | +}, 120_000); |
| 23 | + |
| 24 | +describe("release a dequeued run that never started", () => { |
| 25 | + it("requeues the run without waiting for the stall timeout and gives back its slot", async () => { |
| 26 | + const { api, supervisor, environmentId, taskIdentifier } = await setup("release-requeue"); |
| 27 | + const { id: runId } = await api.triggerTask(taskIdentifier, { payload: {} }); |
| 28 | + const dequeued = await dequeue(supervisor); |
| 29 | + |
| 30 | + const released = await supervisor.releaseRun( |
| 31 | + runId, |
| 32 | + dequeued.snapshot.friendlyId, |
| 33 | + { reason: "PodFailed" }, |
| 34 | + RUNNER_ID, |
| 35 | + environmentId |
| 36 | + ); |
| 37 | + expect(released).toEqual({ success: true, data: { ok: true, requeued: true } }); |
| 38 | + |
| 39 | + const { execution } = ok(await supervisor.getLatestSnapshot(runId)); |
| 40 | + expect(execution.snapshot.executionStatus).toBe("QUEUED"); |
| 41 | + expect(execution.snapshot.description).toBe("Released before the run started: PodFailed"); |
| 42 | + |
| 43 | + // The queue's limit is 1, so the run is dequeued again only if its slot was given back. |
| 44 | + const redelivered = await dequeue(supervisor); |
| 45 | + expect(redelivered.run.friendlyId).toBe(runId); |
| 46 | + expect(redelivered.snapshot.executionStatus).toBe("PENDING_EXECUTING"); |
| 47 | + ok(await supervisor.startRunAttempt(runId, redelivered.snapshot.friendlyId, {})); |
| 48 | + }, 60_000); |
| 49 | + |
| 50 | + it("refuses a run that has started and says not to retry", async () => { |
| 51 | + const { api, supervisor, environmentId, taskIdentifier } = await setup("release-started"); |
| 52 | + const { id: runId } = await api.triggerTask(taskIdentifier, { payload: {} }); |
| 53 | + const dequeued = await dequeue(supervisor); |
| 54 | + ok(await supervisor.startRunAttempt(runId, dequeued.snapshot.friendlyId, {})); |
| 55 | + |
| 56 | + const released = await supervisor.releaseRun( |
| 57 | + runId, |
| 58 | + dequeued.snapshot.friendlyId, |
| 59 | + { reason: "PodFailed" }, |
| 60 | + RUNNER_ID, |
| 61 | + environmentId |
| 62 | + ); |
| 63 | + expect(released).toMatchObject({ |
| 64 | + success: false, |
| 65 | + statusCode: 409, |
| 66 | + shouldRetry: false, |
| 67 | + error: "The run is not at this dequeued snapshot, or this worker group does not serve it", |
| 68 | + }); |
| 69 | + |
| 70 | + const { execution } = ok(await supervisor.getLatestSnapshot(runId)); |
| 71 | + expect(execution.snapshot.executionStatus).toBe("EXECUTING"); |
| 72 | + }, 60_000); |
| 73 | + |
| 74 | + it("answers a run outside the caller's environment with a 409, never a 404", async () => { |
| 75 | + const { api, supervisor, taskIdentifier } = await setup("release-other-env"); |
| 76 | + const { id: runId } = await api.triggerTask(taskIdentifier, { payload: {} }); |
| 77 | + const dequeued = await dequeue(supervisor); |
| 78 | + |
| 79 | + const released = await supervisor.releaseRun( |
| 80 | + runId, |
| 81 | + dequeued.snapshot.friendlyId, |
| 82 | + { reason: "PodFailed" }, |
| 83 | + RUNNER_ID, |
| 84 | + "env_not_the_runs" |
| 85 | + ); |
| 86 | + expect(released).toMatchObject({ success: false, statusCode: 409, shouldRetry: false }); |
| 87 | + |
| 88 | + const { execution } = ok(await supervisor.getLatestSnapshot(runId)); |
| 89 | + expect(execution.snapshot.friendlyId).toBe(dequeued.snapshot.friendlyId); |
| 90 | + }, 60_000); |
| 91 | + |
| 92 | + it("accepts a release from another worker instance of the same group that never dequeued", async () => { |
| 93 | + const { api, supervisor, environmentId, taskIdentifier, workerGroupToken } = |
| 94 | + await setup("release-dequeuer"); |
| 95 | + const { id: runId } = await api.triggerTask(taskIdentifier, { payload: {} }); |
| 96 | + const dequeued = await dequeue(supervisor); |
| 97 | + // A process that only watches failed pods: same group token, its own instance name, no dequeue. |
| 98 | + const watcher = new SupervisorHttpClient({ |
| 99 | + apiUrl: server.webapp.baseUrl, |
| 100 | + workerToken: workerGroupToken, |
| 101 | + instanceName: "release-watcher", |
| 102 | + managedWorkerSecret: MANAGED_WORKER_SECRET, |
| 103 | + }); |
| 104 | + |
| 105 | + const released = await watcher.releaseRun( |
| 106 | + runId, |
| 107 | + dequeued.snapshot.friendlyId, |
| 108 | + { reason: "PodFailed" }, |
| 109 | + "runner-from-the-watcher", |
| 110 | + environmentId |
| 111 | + ); |
| 112 | + expect(released).toEqual({ success: true, data: { ok: true, requeued: true } }); |
| 113 | + |
| 114 | + const { execution } = ok(await supervisor.getLatestSnapshot(runId)); |
| 115 | + expect(execution.snapshot.executionStatus).toBe("QUEUED"); |
| 116 | + }, 60_000); |
| 117 | + |
| 118 | + it("answers a run that does not exist with a 409, even without the environment header", async () => { |
| 119 | + const { supervisor } = await setup("release-missing-run"); |
| 120 | + |
| 121 | + const released = await supervisor.releaseRun( |
| 122 | + "run_doesnotexist0000000000", |
| 123 | + "snapshot_doesnotexist00000", |
| 124 | + { reason: "PodFailed" }, |
| 125 | + RUNNER_ID |
| 126 | + ); |
| 127 | + expect(released).toMatchObject({ success: false, statusCode: 409, shouldRetry: false }); |
| 128 | + }, 60_000); |
| 129 | +}); |
| 130 | + |
| 131 | +async function setup(instanceName: string) { |
| 132 | + const fixtures = await seedEngineFixtures(server.prisma, { taskCount: 1, concurrencyLimit: 1 }); |
| 133 | + return { |
| 134 | + api: new ApiClient(server.webapp.baseUrl, fixtures.environmentApiKey), |
| 135 | + supervisor: new SupervisorHttpClient({ |
| 136 | + apiUrl: server.webapp.baseUrl, |
| 137 | + workerToken: fixtures.workerGroupToken, |
| 138 | + instanceName, |
| 139 | + managedWorkerSecret: MANAGED_WORKER_SECRET, |
| 140 | + }), |
| 141 | + environmentId: fixtures.environmentId, |
| 142 | + taskIdentifier: fixtures.taskIdentifiers[0]!, |
| 143 | + workerGroupToken: fixtures.workerGroupToken, |
| 144 | + }; |
| 145 | +} |
| 146 | + |
| 147 | +function dequeue(supervisor: SupervisorHttpClient) { |
| 148 | + return vi.waitFor( |
| 149 | + async () => { |
| 150 | + const [message] = ok(await supervisor.dequeue({})); |
| 151 | + if (!message) throw new Error("Nothing dequeued yet"); |
| 152 | + return message; |
| 153 | + }, |
| 154 | + { timeout: 10_000, interval: 100 } |
| 155 | + ); |
| 156 | +} |
| 157 | + |
| 158 | +function ok<T>(result: { success: true; data: T } | { success: false; error: string }): T { |
| 159 | + if (!result.success) throw new Error(result.error); |
| 160 | + return result.data; |
| 161 | +} |
0 commit comments