|
| 1 | +import { startTestServer, type TestServer } from "@internal/testcontainers/webapp"; |
| 2 | +import { ApiClient } from "@trigger.dev/core/v3"; |
| 3 | +import { SupervisorHttpClient, WORKER_HEADERS } from "@trigger.dev/core/v3/runEngineWorker"; |
| 4 | +import { setTimeout } from "node:timers/promises"; |
| 5 | +import { Client } from "pg"; |
| 6 | +import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; |
| 7 | +import { |
| 8 | + MANAGED_WORKER_SECRET, |
| 9 | + seedEngineFixtures, |
| 10 | + workerHeaders, |
| 11 | + type EngineFixtures, |
| 12 | +} from "../../test/bench/lib/engineFixtures"; |
| 13 | + |
| 14 | +const INSTANCE_NAME = "continue-e2e"; |
| 15 | +const RUNNER_ID = "runner-continue-e2e"; |
| 16 | + |
| 17 | +let server: TestServer; |
| 18 | +let fixtures: EngineFixtures; |
| 19 | +let api: ApiClient; |
| 20 | +let supervisor: SupervisorHttpClient; |
| 21 | + |
| 22 | +beforeAll(async () => { |
| 23 | + server = await startTestServer({ |
| 24 | + extraEnv: { RUN_ENGINE_CONTINUE_RETRYABLE_ERRORS_ENABLED: "true" }, |
| 25 | + // The engine worker is what requeues a run once its waitpoint completes. |
| 26 | + overrideEnv: { RUN_ENGINE_WORKER_ENABLED: "1" }, |
| 27 | + }); |
| 28 | + fixtures = await seedEngineFixtures(server.prisma, { taskCount: 1 }); |
| 29 | + api = new ApiClient(server.webapp.baseUrl, fixtures.environmentApiKey); |
| 30 | + supervisor = new SupervisorHttpClient({ |
| 31 | + apiUrl: server.webapp.baseUrl, |
| 32 | + workerToken: fixtures.workerGroupToken, |
| 33 | + instanceName: INSTANCE_NAME, |
| 34 | + managedWorkerSecret: MANAGED_WORKER_SECRET, |
| 35 | + }); |
| 36 | +}, 180_000); |
| 37 | + |
| 38 | +afterAll(async () => { |
| 39 | + await server?.stop(); |
| 40 | +}, 120_000); |
| 41 | + |
| 42 | +describe("continue after a restore from a checkpoint", () => { |
| 43 | + it("answers a retry of a continue that already succeeded with the same result", async () => { |
| 44 | + const { runId, snapshotId } = await restoreFromCheckpoint(); |
| 45 | + |
| 46 | + const first = await continueRun(runId, snapshotId); |
| 47 | + const retry = await continueRun(runId, snapshotId); |
| 48 | + |
| 49 | + expect(first.success).toBe(true); |
| 50 | + expect(retry).toEqual(first); |
| 51 | + }, 60_000); |
| 52 | + |
| 53 | + it("keeps retrying through a database outage and succeeds once it ends", async () => { |
| 54 | + const { runId, snapshotId } = await restoreFromCheckpoint(); |
| 55 | + |
| 56 | + const outage = await startDatabaseOutage(); |
| 57 | + let continued: ReturnType<typeof continueRun>; |
| 58 | + try { |
| 59 | + // A single request, because the client retries. It gets past worker auth because that is |
| 60 | + // served from the in-process cache the earlier calls warmed; a cold cache would answer 401. |
| 61 | + const duringOutage = await server.webapp.fetch( |
| 62 | + `/engine/v1/worker-actions/runs/${runId}/snapshots/${snapshotId}/continue`, |
| 63 | + { |
| 64 | + headers: { |
| 65 | + ...workerHeaders(fixtures, INSTANCE_NAME), |
| 66 | + [WORKER_HEADERS.ENVIRONMENT_ID]: fixtures.environmentId, |
| 67 | + }, |
| 68 | + } |
| 69 | + ); |
| 70 | + expect(duringOutage.status).toBe(503); |
| 71 | + |
| 72 | + continued = continueRun(runId, snapshotId, { budgetMs: 30_000 }); |
| 73 | + await setTimeout(3_000); |
| 74 | + } finally { |
| 75 | + await outage.end(); |
| 76 | + } |
| 77 | + |
| 78 | + const { success, retry } = await continued; |
| 79 | + expect(success).toBe(true); |
| 80 | + expect(retry?.outcome).toBe("succeeded"); |
| 81 | + expect(retry?.attempts).toBeGreaterThan(1); |
| 82 | + }, 60_000); |
| 83 | +}); |
| 84 | + |
| 85 | +function continueRun(runId: string, snapshotId: string, retryBudget?: { budgetMs: number }) { |
| 86 | + return supervisor.continueRunExecution(runId, snapshotId, RUNNER_ID, fixtures.environmentId, { |
| 87 | + retryBudget, |
| 88 | + }); |
| 89 | +} |
| 90 | + |
| 91 | +/** |
| 92 | + * Takes a run to the point where its runner, restored from a checkpoint, calls `continue`: the |
| 93 | + * run starts, blocks on a waitpoint and is checkpointed, then the waitpoint completes and the run |
| 94 | + * is dequeued again. Returns the snapshot `continue` is called with. |
| 95 | + */ |
| 96 | +async function restoreFromCheckpoint() { |
| 97 | + const { id: runId } = await api.triggerTask(fixtures.taskIdentifiers[0]!, { payload: {} }); |
| 98 | + const dequeued = await dequeue(); |
| 99 | + ok(await supervisor.startRunAttempt(runId, dequeued.snapshot.friendlyId, {})); |
| 100 | + |
| 101 | + const token = await api.createWaitpointToken({}); |
| 102 | + await api.waitForWaitpointToken({ runFriendlyId: runId, waitpointFriendlyId: token.id }); |
| 103 | + |
| 104 | + const { execution } = ok(await supervisor.getLatestSnapshot(runId)); |
| 105 | + ok( |
| 106 | + await supervisor.submitSuspendCompletion({ |
| 107 | + runId, |
| 108 | + snapshotId: execution.snapshot.friendlyId, |
| 109 | + body: { success: true, checkpoint: { type: "DOCKER", location: "e2e-checkpoint" } }, |
| 110 | + }) |
| 111 | + ); |
| 112 | + |
| 113 | + await api.completeWaitpointToken(token.id, {}); |
| 114 | + const restored = await dequeue(); |
| 115 | + expect(restored.snapshot.executionStatus).toBe("PENDING_EXECUTING"); |
| 116 | + |
| 117 | + return { runId, snapshotId: restored.snapshot.friendlyId }; |
| 118 | +} |
| 119 | + |
| 120 | +function dequeue() { |
| 121 | + return vi.waitFor( |
| 122 | + async () => { |
| 123 | + const [message] = ok(await supervisor.dequeue({})); |
| 124 | + if (!message) throw new Error("Nothing dequeued yet"); |
| 125 | + return message; |
| 126 | + }, |
| 127 | + { timeout: 10_000, interval: 100 } |
| 128 | + ); |
| 129 | +} |
| 130 | + |
| 131 | +/** Closes the webapp's database to new connections and drops the open ones. */ |
| 132 | +async function startDatabaseOutage() { |
| 133 | + const url = new URL(server.databaseUrl); |
| 134 | + const database = url.pathname.slice(1); |
| 135 | + url.pathname = "/postgres"; |
| 136 | + |
| 137 | + const admin = new Client({ connectionString: url.toString() }); |
| 138 | + await admin.connect(); |
| 139 | + await admin.query(`ALTER DATABASE "${database}" WITH ALLOW_CONNECTIONS false`); |
| 140 | + await admin.query(`SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = $1`, [ |
| 141 | + database, |
| 142 | + ]); |
| 143 | + |
| 144 | + return { |
| 145 | + async end() { |
| 146 | + await admin.query(`ALTER DATABASE "${database}" WITH ALLOW_CONNECTIONS true`); |
| 147 | + await admin.end(); |
| 148 | + }, |
| 149 | + }; |
| 150 | +} |
| 151 | + |
| 152 | +function ok<T>(result: { success: true; data: T } | { success: false; error: string }): T { |
| 153 | + if (!result.success) throw new Error(result.error); |
| 154 | + return result.data; |
| 155 | +} |
0 commit comments