|
| 1 | +import { |
| 2 | + assertNonNullable, |
| 3 | + containerTest, |
| 4 | + setupAuthenticatedEnvironment, |
| 5 | + setupBackgroundWorker, |
| 6 | +} from "@internal/testcontainers"; |
| 7 | +import { trace } from "@opentelemetry/api"; |
| 8 | +import { expect } from "vitest"; |
| 9 | +import { EventBusEventArgs } from "../eventBus.js"; |
| 10 | +import { RunEngine } from "../index.js"; |
| 11 | + |
| 12 | +describe("RunEngine attempt failures", () => { |
| 13 | + containerTest( |
| 14 | + "Single run (retry attempt, then succeed)", |
| 15 | + { timeout: 15_000 }, |
| 16 | + async ({ prisma, redisOptions }) => { |
| 17 | + //create environment |
| 18 | + const authenticatedEnvironment = await setupAuthenticatedEnvironment(prisma, "PRODUCTION"); |
| 19 | + |
| 20 | + const engine = new RunEngine({ |
| 21 | + prisma, |
| 22 | + worker: { |
| 23 | + redis: redisOptions, |
| 24 | + workers: 1, |
| 25 | + tasksPerWorker: 10, |
| 26 | + pollIntervalMs: 100, |
| 27 | + }, |
| 28 | + queue: { |
| 29 | + redis: redisOptions, |
| 30 | + }, |
| 31 | + runLock: { |
| 32 | + redis: redisOptions, |
| 33 | + }, |
| 34 | + machines: { |
| 35 | + defaultMachine: "small-1x", |
| 36 | + machines: { |
| 37 | + "small-1x": { |
| 38 | + name: "small-1x" as const, |
| 39 | + cpu: 0.5, |
| 40 | + memory: 0.5, |
| 41 | + centsPerMs: 0.0001, |
| 42 | + }, |
| 43 | + }, |
| 44 | + baseCostInCents: 0.0001, |
| 45 | + }, |
| 46 | + tracer: trace.getTracer("test", "0.0.0"), |
| 47 | + }); |
| 48 | + |
| 49 | + try { |
| 50 | + const taskIdentifier = "test-task"; |
| 51 | + |
| 52 | + //create background worker |
| 53 | + const backgroundWorker = await setupBackgroundWorker( |
| 54 | + prisma, |
| 55 | + authenticatedEnvironment, |
| 56 | + taskIdentifier |
| 57 | + ); |
| 58 | + |
| 59 | + //trigger the run |
| 60 | + const run = await engine.trigger( |
| 61 | + { |
| 62 | + number: 1, |
| 63 | + friendlyId: "run_1234", |
| 64 | + environment: authenticatedEnvironment, |
| 65 | + taskIdentifier, |
| 66 | + payload: "{}", |
| 67 | + payloadType: "application/json", |
| 68 | + context: {}, |
| 69 | + traceContext: {}, |
| 70 | + traceId: "t12345", |
| 71 | + spanId: "s12345", |
| 72 | + masterQueue: "main", |
| 73 | + queueName: "task/test-task", |
| 74 | + isTest: false, |
| 75 | + tags: [], |
| 76 | + }, |
| 77 | + prisma |
| 78 | + ); |
| 79 | + |
| 80 | + //dequeue the run |
| 81 | + const dequeued = await engine.dequeueFromMasterQueue({ |
| 82 | + consumerId: "test_12345", |
| 83 | + masterQueue: run.masterQueue, |
| 84 | + maxRunCount: 10, |
| 85 | + }); |
| 86 | + |
| 87 | + //create an attempt |
| 88 | + const attemptResult = await engine.startRunAttempt({ |
| 89 | + runId: dequeued[0].run.id, |
| 90 | + snapshotId: dequeued[0].snapshot.id, |
| 91 | + }); |
| 92 | + |
| 93 | + //fail the attempt |
| 94 | + const error = { |
| 95 | + type: "BUILT_IN_ERROR" as const, |
| 96 | + name: "UserError", |
| 97 | + message: "This is a user error", |
| 98 | + stackTrace: "Error: This is a user error\n at <anonymous>:1:1", |
| 99 | + }; |
| 100 | + const result = await engine.completeRunAttempt({ |
| 101 | + runId: dequeued[0].run.id, |
| 102 | + snapshotId: attemptResult.snapshot.id, |
| 103 | + completion: { |
| 104 | + ok: false, |
| 105 | + id: dequeued[0].run.id, |
| 106 | + error, |
| 107 | + retry: { |
| 108 | + timestamp: Date.now(), |
| 109 | + delay: 0, |
| 110 | + }, |
| 111 | + }, |
| 112 | + }); |
| 113 | + expect(result.attemptStatus).toBe("RETRY_IMMEDIATELY"); |
| 114 | + expect(result.snapshot.executionStatus).toBe("PENDING_EXECUTING"); |
| 115 | + expect(result.run.status).toBe("RETRYING_AFTER_FAILURE"); |
| 116 | + |
| 117 | + //state should be completed |
| 118 | + const executionData3 = await engine.getRunExecutionData({ runId: run.id }); |
| 119 | + assertNonNullable(executionData3); |
| 120 | + expect(executionData3.snapshot.executionStatus).toBe("PENDING_EXECUTING"); |
| 121 | + //only when the new attempt is created, should the attempt be increased |
| 122 | + expect(executionData3.run.attemptNumber).toBe(1); |
| 123 | + expect(executionData3.run.status).toBe("RETRYING_AFTER_FAILURE"); |
| 124 | + |
| 125 | + //create a second attempt |
| 126 | + const attemptResult2 = await engine.startRunAttempt({ |
| 127 | + runId: dequeued[0].run.id, |
| 128 | + snapshotId: executionData3.snapshot.id, |
| 129 | + }); |
| 130 | + expect(attemptResult2.run.attemptNumber).toBe(2); |
| 131 | + |
| 132 | + //now complete it successfully |
| 133 | + const result2 = await engine.completeRunAttempt({ |
| 134 | + runId: dequeued[0].run.id, |
| 135 | + snapshotId: attemptResult2.snapshot.id, |
| 136 | + completion: { |
| 137 | + ok: true, |
| 138 | + id: dequeued[0].run.id, |
| 139 | + output: `{"foo":"bar"}`, |
| 140 | + outputType: "application/json", |
| 141 | + }, |
| 142 | + }); |
| 143 | + expect(result2.snapshot.executionStatus).toBe("FINISHED"); |
| 144 | + expect(result2.run.attemptNumber).toBe(2); |
| 145 | + expect(result2.run.status).toBe("COMPLETED_SUCCESSFULLY"); |
| 146 | + |
| 147 | + //waitpoint should have been completed, with the output |
| 148 | + const runWaitpointAfter = await prisma.waitpoint.findMany({ |
| 149 | + where: { |
| 150 | + completedByTaskRunId: run.id, |
| 151 | + }, |
| 152 | + }); |
| 153 | + expect(runWaitpointAfter.length).toBe(1); |
| 154 | + expect(runWaitpointAfter[0].type).toBe("RUN"); |
| 155 | + expect(runWaitpointAfter[0].output).toBe(`{"foo":"bar"}`); |
| 156 | + expect(runWaitpointAfter[0].outputIsError).toBe(false); |
| 157 | + |
| 158 | + //state should be completed |
| 159 | + const executionData4 = await engine.getRunExecutionData({ runId: run.id }); |
| 160 | + assertNonNullable(executionData4); |
| 161 | + expect(executionData4.snapshot.executionStatus).toBe("FINISHED"); |
| 162 | + expect(executionData4.run.attemptNumber).toBe(2); |
| 163 | + expect(executionData4.run.status).toBe("COMPLETED_SUCCESSFULLY"); |
| 164 | + } finally { |
| 165 | + engine.quit(); |
| 166 | + } |
| 167 | + } |
| 168 | + ); |
| 169 | +}); |
0 commit comments