-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathdequeuing.test.ts
More file actions
215 lines (194 loc) · 5.66 KB
/
dequeuing.test.ts
File metadata and controls
215 lines (194 loc) · 5.66 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
import { containerTest } from "@internal/testcontainers";
import { trace } from "@internal/tracing";
import { generateFriendlyId } from "@trigger.dev/core/v3/isomorphic";
import { PrismaClientOrTransaction } from "@trigger.dev/database";
import { expect } from "vitest";
import { MinimalAuthenticatedEnvironment } from "../../shared/index.js";
import { RunEngine } from "../index.js";
import { setupAuthenticatedEnvironment, setupBackgroundWorker } from "./setup.js";
import { DequeuedMessage } from "@trigger.dev/core/v3";
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,
},
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);
//check the queue length
const queueLength = await engine.runQueue.lengthOfEnvQueue(authenticatedEnvironment);
expect(queueLength).toBe(10);
//dequeue
const dequeued: DequeuedMessage[] = [];
for (let i = 0; i < 5; i++) {
dequeued.push(
...(await engine.dequeueFromMasterQueue({
consumerId: "test_12345",
masterQueue: "main",
maxRunCount: 1,
}))
);
}
expect(dequeued.length).toBe(5);
} finally {
await engine.quit();
}
});
//This will fail until we support dequeuing multiple runs from a single environment
containerTest.fails(
"Dequeues runs within machine constraints",
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,
},
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, {
preset: "small-1x",
});
//trigger the runs
const runs = await triggerRuns({
engine,
environment: authenticatedEnvironment,
taskIdentifier,
prisma,
count: 20,
});
expect(runs.length).toBe(20);
//check the queue length
const queueLength = await engine.runQueue.lengthOfEnvQueue(authenticatedEnvironment);
expect(queueLength).toBe(20);
//dequeue
const dequeued = await engine.dequeueFromMasterQueue({
consumerId: "test_12345",
masterQueue: "main",
maxRunCount: 5,
maxResources: {
cpu: 1.1,
memory: 3.8,
},
});
expect(dequeued.length).toBe(2);
//check the queue length
const queueLength2 = await engine.runQueue.lengthOfEnvQueue(authenticatedEnvironment);
expect(queueLength2).toBe(18);
const dequeued2 = await engine.dequeueFromMasterQueue({
consumerId: "test_12345",
masterQueue: "main",
maxRunCount: 10,
maxResources: {
cpu: 4.7,
memory: 3.0,
},
});
expect(dequeued2.length).toBe(6);
//check the queue length
const queueLength3 = await engine.runQueue.lengthOfEnvQueue(authenticatedEnvironment);
expect(queueLength3).toBe(12);
} 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",
masterQueue: "main",
queue: `task/${taskIdentifier}`,
isTest: false,
tags: [],
},
prisma
);
}
return runs;
}