|
1 | | -import { MachineResources } from "@trigger.dev/core/v3"; |
2 | 1 | import { SupervisorHttpClient } from "./http.js"; |
3 | 2 | import { WorkerApiDequeueResponseBody } from "./schemas.js"; |
| 3 | +import { PreDequeueFn } from "./types.js"; |
4 | 4 |
|
5 | 5 | type RunQueueConsumerOptions = { |
6 | 6 | client: SupervisorHttpClient; |
7 | 7 | intervalMs?: number; |
8 | | - preDequeue?: () => Promise<{ |
9 | | - maxResources?: MachineResources; |
10 | | - }>; |
| 8 | + preDequeue?: PreDequeueFn; |
11 | 9 | onDequeue: (messages: WorkerApiDequeueResponseBody) => Promise<void>; |
12 | 10 | }; |
13 | 11 |
|
14 | 12 | export class RunQueueConsumer { |
15 | 13 | private readonly client: SupervisorHttpClient; |
16 | | - private readonly preDequeue?: () => Promise<{ |
17 | | - maxResources?: MachineResources; |
18 | | - }>; |
| 14 | + private readonly preDequeue?: PreDequeueFn; |
19 | 15 | private readonly onDequeue: (messages: WorkerApiDequeueResponseBody) => Promise<void>; |
20 | 16 |
|
21 | 17 | private intervalMs: number; |
@@ -54,18 +50,27 @@ export class RunQueueConsumer { |
54 | 50 | return; |
55 | 51 | } |
56 | 52 |
|
57 | | - let maxResources: MachineResources | undefined; |
| 53 | + let preDequeueResult: Awaited<ReturnType<PreDequeueFn>> | undefined; |
58 | 54 | if (this.preDequeue) { |
59 | 55 | try { |
60 | | - const preDequeueResult = await this.preDequeue(); |
61 | | - maxResources = preDequeueResult.maxResources; |
| 56 | + preDequeueResult = await this.preDequeue(); |
62 | 57 | } catch (preDequeueError) { |
63 | 58 | console.error("[RunQueueConsumer] preDequeue error", { error: preDequeueError }); |
64 | 59 | } |
65 | 60 | } |
66 | 61 |
|
| 62 | + if ( |
| 63 | + preDequeueResult?.skipDequeue || |
| 64 | + preDequeueResult?.maxResources?.cpu === 0 || |
| 65 | + preDequeueResult?.maxResources?.memory === 0 |
| 66 | + ) { |
| 67 | + return this.scheduleNextDequeue(); |
| 68 | + } |
| 69 | + |
67 | 70 | try { |
68 | | - const response = await this.client.dequeue({ maxResources }); |
| 71 | + const response = await this.client.dequeue({ |
| 72 | + maxResources: preDequeueResult?.maxResources, |
| 73 | + }); |
69 | 74 |
|
70 | 75 | if (!response.success) { |
71 | 76 | console.error("[RunQueueConsumer] Failed to dequeue", { error: response.error }); |
|
0 commit comments