-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathdequeuing.test.ts
More file actions
253 lines (223 loc) · 7.8 KB
/
dequeuing.test.ts
File metadata and controls
253 lines (223 loc) · 7.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import { assertNonNullable, containerTest } from "@internal/testcontainers";
import { trace } from "@internal/tracing";
import { DequeuedMessage } from "@trigger.dev/core/v3";
import { generateFriendlyId } from "@trigger.dev/core/v3/isomorphic";
import { PrismaClientOrTransaction } from "@trigger.dev/database";
import { expect } from "vitest";
import { setTimeout } from "node:timers/promises";
import { MinimalAuthenticatedEnvironment } from "../../shared/index.js";
import { RunEngine } from "../index.js";
import { setupAuthenticatedEnvironment, setupBackgroundWorker } from "./setup.js";
vi.setConfig({ testTimeout: 60_000 });
describe("RunEngine dequeuing", () => {
containerTest("Dequeues 5 runs", async ({ prisma, redisOptions }) => {
const authenticatedEnvironment = await setupAuthenticatedEnvironment(prisma, "PRODUCTION");
const engine = new RunEngine({
prisma,
worker: {
redis: redisOptions,
workers: 1,
tasksPerWorker: 10,
pollIntervalMs: 100,
},
queue: {
redis: redisOptions,
masterQueueConsumersDisabled: true,
processWorkerQueueDebounceMs: 50,
},
runLock: {
redis: redisOptions,
},
machines: {
defaultMachine: "small-1x",
machines: {
"small-1x": {
name: "small-1x" as const,
cpu: 0.5,
memory: 0.5,
centsPerMs: 0.0001,
},
},
baseCostInCents: 0.0005,
},
tracer: trace.getTracer("test", "0.0.0"),
});
try {
const taskIdentifier = "test-task";
//create background worker
await setupBackgroundWorker(engine, authenticatedEnvironment, taskIdentifier);
//trigger the runs
const runs = await triggerRuns({
engine,
environment: authenticatedEnvironment,
taskIdentifier,
prisma,
count: 10,
});
expect(runs.length).toBe(10);
//dequeue
await engine.runQueue.processMasterQueueForEnvironment(authenticatedEnvironment.id, 5);
const dequeued: DequeuedMessage[] = [];
for (let i = 0; i < 5; i++) {
dequeued.push(
...(await engine.dequeueFromWorkerQueue({
consumerId: "test_12345",
workerQueue: "main",
}))
);
}
expect(dequeued.length).toBe(5);
} finally {
await engine.quit();
}
});
containerTest(
"Direct nack after dequeue clears concurrency and allows recovery",
async ({ prisma, redisOptions }) => {
const authenticatedEnvironment = await setupAuthenticatedEnvironment(prisma, "PRODUCTION");
// Use a short heartbeat timeout so the stalled system recovers the run quickly
const pendingExecutingTimeout = 1000;
const engine = new RunEngine({
prisma,
worker: {
redis: redisOptions,
workers: 1,
tasksPerWorker: 10,
pollIntervalMs: 100,
},
queue: {
redis: redisOptions,
masterQueueConsumersDisabled: true,
processWorkerQueueDebounceMs: 50,
},
runLock: {
redis: redisOptions,
},
machines: {
defaultMachine: "small-1x",
machines: {
"small-1x": {
name: "small-1x" as const,
cpu: 0.5,
memory: 0.5,
centsPerMs: 0.0001,
},
},
baseCostInCents: 0.0005,
},
heartbeatTimeoutsMs: {
PENDING_EXECUTING: pendingExecutingTimeout,
},
tracer: trace.getTracer("test", "0.0.0"),
});
try {
const taskIdentifier = "test-task";
// Setup background worker
await setupBackgroundWorker(engine, authenticatedEnvironment, taskIdentifier);
// Trigger a single run
const runs = await triggerRuns({
engine,
environment: authenticatedEnvironment,
taskIdentifier,
prisma,
count: 1,
});
expect(runs.length).toBe(1);
const run = runs[0];
// Process master queue to move run to worker queue
await engine.runQueue.processMasterQueueForEnvironment(authenticatedEnvironment.id, 1);
// Wait for processing
await setTimeout(500);
// Dequeue from worker queue - this puts run in concurrency sets and creates PENDING_EXECUTING snapshot
const dequeued = await engine.dequeueFromWorkerQueue({
consumerId: "test_12345",
workerQueue: "main",
});
expect(dequeued.length).toBe(1);
assertNonNullable(dequeued[0]);
// Verify run is in PENDING_EXECUTING state
const executionDataBefore = await engine.getRunExecutionData({ runId: run.id });
assertNonNullable(executionDataBefore);
expect(executionDataBefore.snapshot.executionStatus).toBe("PENDING_EXECUTING");
// Verify run is in concurrency
const envConcurrencyBefore = await engine.runQueue.currentConcurrencyOfEnvironment(
authenticatedEnvironment
);
expect(envConcurrencyBefore).toBe(1);
// Simulate DB failure fallback: call nackMessage directly via Redis
// This is what happens when the catch block can't read from Postgres
await engine.runQueue.nackMessage({
orgId: authenticatedEnvironment.organization.id,
messageId: run.id,
});
// Verify concurrency is cleared - this is the key fix!
// Without this fix, the run would stay in concurrency sets forever
const envConcurrencyAfter = await engine.runQueue.currentConcurrencyOfEnvironment(
authenticatedEnvironment
);
expect(envConcurrencyAfter).toBe(0);
// Verify the message is back in the queue
const envQueueLength = await engine.runQueue.lengthOfEnvQueue(authenticatedEnvironment);
expect(envQueueLength).toBe(1);
// Wait for the stalled system to detect and recover the PENDING_EXECUTING run
// The stalled system will call tryNackAndRequeue which updates Postgres state to QUEUED
await setTimeout(pendingExecutingTimeout * 5);
// Verify the stalled system recovered the run to QUEUED state
const executionDataAfterStall = await engine.getRunExecutionData({ runId: run.id });
assertNonNullable(executionDataAfterStall);
expect(executionDataAfterStall.snapshot.executionStatus).toBe("QUEUED");
// Process master queue to move the run from env queue to worker queue
await engine.runQueue.processMasterQueueForEnvironment(authenticatedEnvironment.id, 1);
// Wait for processing
await setTimeout(500);
// Dequeue from worker queue - the run should now be available
const dequeuedAgain = await engine.dequeueFromWorkerQueue({
consumerId: "test_12345",
workerQueue: "main",
});
expect(dequeuedAgain.length).toBe(1);
assertNonNullable(dequeuedAgain[0]);
expect(dequeuedAgain[0].run.id).toBe(run.id);
} finally {
await engine.quit();
}
}
);
});
async function triggerRuns({
engine,
environment,
taskIdentifier,
prisma,
count,
}: {
engine: RunEngine;
environment: MinimalAuthenticatedEnvironment;
taskIdentifier: string;
prisma: PrismaClientOrTransaction;
count: number;
}) {
const runs = [];
for (let i = 0; i < count; i++) {
runs[i] = await engine.trigger(
{
number: i,
friendlyId: generateFriendlyId("run"),
environment,
taskIdentifier,
payload: "{}",
payloadType: "application/json",
context: {},
traceContext: {},
traceId: "t12345",
spanId: "s12345",
workerQueue: "main",
queue: `task/${taskIdentifier}`,
isTest: false,
tags: [],
},
prisma
);
}
return runs;
}