|
1 |
| -import { logger, queue, queues, task } from "@trigger.dev/sdk/v3"; |
| 1 | +import { batch, logger, queue, queues, task } from "@trigger.dev/sdk/v3"; |
2 | 2 |
|
3 | 3 | export const queuesTester = task({
|
4 | 4 | id: "queues-tester",
|
@@ -52,3 +52,186 @@ export const otherQueueTask = task({
|
52 | 52 | logger.log("Other queue task", { payload });
|
53 | 53 | },
|
54 | 54 | });
|
| 55 | + |
| 56 | +import { setTimeout } from "node:timers/promises"; |
| 57 | + |
| 58 | +type Payload = { |
| 59 | + id: string; |
| 60 | + waitSeconds: number; |
| 61 | +}; |
| 62 | + |
| 63 | +export const myQueue = queue({ |
| 64 | + name: "shared-queue", |
| 65 | + concurrencyLimit: 2, |
| 66 | +}); |
| 67 | + |
| 68 | +// First task type that uses shared queue |
| 69 | +export const sharedQueueTask1 = task({ |
| 70 | + id: "shared-queue-task-1", |
| 71 | + queue: myQueue, |
| 72 | + run: async (payload: Payload) => { |
| 73 | + const startedAt = Date.now(); |
| 74 | + logger.info(`Task1 ${payload.id} started at ${startedAt}`); |
| 75 | + |
| 76 | + await setTimeout(payload.waitSeconds * 1000); |
| 77 | + |
| 78 | + const completedAt = Date.now(); |
| 79 | + logger.info(`Task1 ${payload.id} completed at ${completedAt}`); |
| 80 | + |
| 81 | + return { |
| 82 | + id: payload.id, |
| 83 | + startedAt, |
| 84 | + completedAt, |
| 85 | + }; |
| 86 | + }, |
| 87 | +}); |
| 88 | + |
| 89 | +// Second task type that uses the same queue |
| 90 | +export const sharedQueueTask2 = task({ |
| 91 | + id: "shared-queue-task-2", |
| 92 | + queue: myQueue, |
| 93 | + run: async (payload: Payload) => { |
| 94 | + const startedAt = Date.now(); |
| 95 | + logger.info(`Task2 ${payload.id} started at ${startedAt}`); |
| 96 | + |
| 97 | + await setTimeout(payload.waitSeconds * 1000); |
| 98 | + |
| 99 | + const completedAt = Date.now(); |
| 100 | + logger.info(`Task2 ${payload.id} completed at ${completedAt}`); |
| 101 | + |
| 102 | + return { |
| 103 | + id: payload.id, |
| 104 | + startedAt, |
| 105 | + completedAt, |
| 106 | + }; |
| 107 | + }, |
| 108 | +}); |
| 109 | + |
| 110 | +export const sharedQueueTask3 = task({ |
| 111 | + id: "shared-queue-task-3", |
| 112 | + queue: myQueue, |
| 113 | + run: async (payload: Payload) => { |
| 114 | + const startedAt = Date.now(); |
| 115 | + logger.info(`Task2 ${payload.id} started at ${startedAt}`); |
| 116 | + |
| 117 | + await setTimeout(payload.waitSeconds * 1000); |
| 118 | + |
| 119 | + const completedAt = Date.now(); |
| 120 | + logger.info(`Task2 ${payload.id} completed at ${completedAt}`); |
| 121 | + |
| 122 | + return { |
| 123 | + id: payload.id, |
| 124 | + startedAt, |
| 125 | + completedAt, |
| 126 | + }; |
| 127 | + }, |
| 128 | +}); |
| 129 | + |
| 130 | +export const sharedQueueTask4 = task({ |
| 131 | + id: "shared-queue-task-4", |
| 132 | + queue: myQueue, |
| 133 | + run: async (payload: Payload) => { |
| 134 | + const startedAt = Date.now(); |
| 135 | + logger.info(`Task2 ${payload.id} started at ${startedAt}`); |
| 136 | + |
| 137 | + await setTimeout(payload.waitSeconds * 1000); |
| 138 | + |
| 139 | + const completedAt = Date.now(); |
| 140 | + logger.info(`Task2 ${payload.id} completed at ${completedAt}`); |
| 141 | + |
| 142 | + return { |
| 143 | + id: payload.id, |
| 144 | + startedAt, |
| 145 | + completedAt, |
| 146 | + }; |
| 147 | + }, |
| 148 | +}); |
| 149 | + |
| 150 | +export const sharedQueueTask5 = task({ |
| 151 | + id: "shared-queue-task-5", |
| 152 | + queue: myQueue, |
| 153 | + run: async (payload: Payload) => { |
| 154 | + const startedAt = Date.now(); |
| 155 | + logger.info(`Task2 ${payload.id} started at ${startedAt}`); |
| 156 | + |
| 157 | + await setTimeout(payload.waitSeconds * 1000); |
| 158 | + |
| 159 | + const completedAt = Date.now(); |
| 160 | + logger.info(`Task2 ${payload.id} completed at ${completedAt}`); |
| 161 | + |
| 162 | + return { |
| 163 | + id: payload.id, |
| 164 | + startedAt, |
| 165 | + completedAt, |
| 166 | + }; |
| 167 | + }, |
| 168 | +}); |
| 169 | + |
| 170 | +// Test task that verifies shared queue concurrency |
| 171 | +export const sharedQueueTestTask = task({ |
| 172 | + id: "shared-queue-test", |
| 173 | + retry: { |
| 174 | + maxAttempts: 1, |
| 175 | + }, |
| 176 | + // 4 minutes |
| 177 | + maxDuration: 240, |
| 178 | + run: async (payload, { ctx }) => { |
| 179 | + logger.info("Starting shared queue concurrency test"); |
| 180 | + |
| 181 | + // Trigger mix of both task types (5 total tasks) |
| 182 | + // With concurrencyLimit: 2, we expect only 2 running at once |
| 183 | + // regardless of task type |
| 184 | + const results = await batch.triggerAndWait([ |
| 185 | + { id: sharedQueueTask1.id, payload: { id: "t1-1", waitSeconds: 4 } }, |
| 186 | + { id: sharedQueueTask2.id, payload: { id: "t2-1", waitSeconds: 4 } }, |
| 187 | + { id: sharedQueueTask1.id, payload: { id: "t1-2", waitSeconds: 4 } }, |
| 188 | + { id: sharedQueueTask2.id, payload: { id: "t2-2", waitSeconds: 4 } }, |
| 189 | + { id: sharedQueueTask1.id, payload: { id: "t1-3", waitSeconds: 4 } }, |
| 190 | + ]); |
| 191 | + |
| 192 | + // Verify all tasks completed successfully |
| 193 | + if (!results.runs.every((r) => r.ok)) { |
| 194 | + throw new Error("One or more tasks failed"); |
| 195 | + } |
| 196 | + |
| 197 | + // Get all executions sorted by start time |
| 198 | + const executions = results.runs.map((r) => r.output).sort((a, b) => a.startedAt - b.startedAt); |
| 199 | + |
| 200 | + // For each point in time, count how many tasks were running |
| 201 | + let maxConcurrent = 0; |
| 202 | + for (let i = 0; i < executions.length; i++) { |
| 203 | + const current = executions[i]; |
| 204 | + const concurrent = |
| 205 | + executions.filter( |
| 206 | + (task) => |
| 207 | + task.id !== current.id && // not the same task |
| 208 | + task.startedAt <= current.startedAt && // started before or at same time |
| 209 | + task.completedAt >= current.startedAt // hadn't completed yet |
| 210 | + ).length + 1; // +1 for current task |
| 211 | + |
| 212 | + maxConcurrent = Math.max(maxConcurrent, concurrent); |
| 213 | + } |
| 214 | + |
| 215 | + // Verify we never exceeded the concurrency limit |
| 216 | + if (maxConcurrent > 2) { |
| 217 | + throw new Error(`Expected maximum of 2 concurrent tasks, but found ${maxConcurrent}`); |
| 218 | + } |
| 219 | + |
| 220 | + // Verify tasks from both types were able to run |
| 221 | + const task1Runs = executions.filter((e) => e.id.startsWith("t1-")).length; |
| 222 | + const task2Runs = executions.filter((e) => e.id.startsWith("t2-")).length; |
| 223 | + |
| 224 | + if (task1Runs === 0 || task2Runs === 0) { |
| 225 | + throw new Error( |
| 226 | + `Expected both task types to run, but got ${task1Runs} task1 runs and ${task2Runs} task2 runs` |
| 227 | + ); |
| 228 | + } |
| 229 | + |
| 230 | + return { |
| 231 | + executions, |
| 232 | + maxConcurrent, |
| 233 | + task1Count: task1Runs, |
| 234 | + task2Count: task2Runs, |
| 235 | + }; |
| 236 | + }, |
| 237 | +}); |
0 commit comments