Skip to content

Commit fc28c74

Browse files
committed
runs.list filtering for queue and machine
1 parent a8bed20 commit fc28c74

File tree

6 files changed

+118
-13
lines changed

6 files changed

+118
-13
lines changed

.changeset/two-eagles-report.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@trigger.dev/sdk": patch
3+
---
4+
5+
Added runs.list filtering for queue and machine

apps/webapp/app/presenters/v3/ApiRunListPresenter.server.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { parsePacket, RunStatus } from "@trigger.dev/core/v3";
1+
import {
2+
type ListRunResponse,
3+
type ListRunResponseItem,
4+
MachinePresetName,
5+
parsePacket,
6+
RunStatus,
7+
} from "@trigger.dev/core/v3";
28
import { type Project, type RuntimeEnvironment, type TaskRunStatus } from "@trigger.dev/database";
39
import assertNever from "assert-never";
410
import { z } from "zod";
@@ -104,6 +110,34 @@ export const ApiRunListSearchParams = z.object({
104110
"filter[createdAt][to]": CoercedDate,
105111
"filter[createdAt][period]": z.string().optional(),
106112
"filter[batch]": z.string().optional(),
113+
"filter[queue]": z
114+
.string()
115+
.optional()
116+
.transform((value) => {
117+
return value ? value.split(",") : undefined;
118+
}),
119+
"filter[machine]": z
120+
.string()
121+
.optional()
122+
.transform((value, ctx) => {
123+
const values = value ? value.split(",") : undefined;
124+
if (!values) {
125+
return undefined;
126+
}
127+
128+
const parsedValues = values.map((v) => MachinePresetName.safeParse(v));
129+
const invalidValues = parsedValues.filter((result) => !result.success);
130+
if (invalidValues.length > 0) {
131+
ctx.addIssue({
132+
code: z.ZodIssueCode.custom,
133+
message: `Invalid machine values: ${invalidValues.join(", ")}`,
134+
});
135+
136+
return z.NEVER;
137+
}
138+
139+
return parsedValues.map((result) => result.data).filter(Boolean);
140+
}),
107141
});
108142

109143
type ApiRunListSearchParams = z.infer<typeof ApiRunListSearchParams>;

packages/core/src/v3/apiClient/index.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
ListRunResponseItem,
2222
ListScheduleOptions,
2323
QueueItem,
24+
QueueTypeName,
2425
ReplayRunResponse,
2526
RescheduleRunRequestBody,
2627
RetrieveBatchV2Response,
@@ -1147,11 +1148,35 @@ function createSearchQueryForListRuns(query?: ListRunsQueryParams): URLSearchPar
11471148
if (query.batch) {
11481149
searchParams.append("filter[batch]", query.batch);
11491150
}
1151+
1152+
if (query.queue) {
1153+
searchParams.append(
1154+
"filter[queue]",
1155+
Array.isArray(query.queue)
1156+
? query.queue.map((q) => queueNameFromQueueTypeName(q)).join(",")
1157+
: queueNameFromQueueTypeName(query.queue)
1158+
);
1159+
}
1160+
1161+
if (query.machine) {
1162+
searchParams.append(
1163+
"filter[machine]",
1164+
Array.isArray(query.machine) ? query.machine.join(",") : query.machine
1165+
);
1166+
}
11501167
}
11511168

11521169
return searchParams;
11531170
}
11541171

1172+
function queueNameFromQueueTypeName(queue: QueueTypeName): string {
1173+
if (queue.type === "task") {
1174+
return `task/${queue.name}`;
1175+
}
1176+
1177+
return queue.name;
1178+
}
1179+
11551180
function createSearchQueryForListWaitpointTokens(
11561181
query?: ListWaitpointTokensQueryParams
11571182
): URLSearchParams {

packages/core/src/v3/apiClient/types.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
import { RunStatus, WaitpointTokenStatus } from "../schemas/index.js";
1+
import {
2+
MachinePresetName,
3+
QueueTypeName,
4+
RunStatus,
5+
WaitpointTokenStatus,
6+
} from "../schemas/index.js";
27
import { CursorPageParams } from "./pagination.js";
38

49
export interface ImportEnvironmentVariablesParams {
@@ -32,6 +37,27 @@ export interface ListRunsQueryParams extends CursorPageParams {
3237
schedule?: string;
3338
isTest?: boolean;
3439
batch?: string;
40+
/**
41+
* The queue type and name, or multiple of them.
42+
*
43+
* @example
44+
* ```ts
45+
* const runs = await runs.list({
46+
* queue: { type: "task", name: "my-task-id" },
47+
* });
48+
*
49+
* // Or multiple queues
50+
* const runs = await runs.list({
51+
* queue: [
52+
* { type: "custom", name: "my-custom-queue" },
53+
* { type: "task", name: "my-task-id" },
54+
* ],
55+
* });
56+
* ```
57+
* */
58+
queue?: Array<QueueTypeName> | QueueTypeName;
59+
/** The machine name, or multiple of them. */
60+
machine?: Array<MachinePresetName> | MachinePresetName;
3561
}
3662

3763
export interface ListProjectRunsQueryParams extends CursorPageParams, ListRunsQueryParams {

packages/core/src/v3/schemas/queues.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@ export const ListQueueOptions = z.object({
4545

4646
export type ListQueueOptions = z.infer<typeof ListQueueOptions>;
4747

48+
export const QueueTypeName = z.object({
49+
/** "task" or "custom" */
50+
type: QueueType,
51+
/** The name of your queue.
52+
* For "task" type it will be the task id, for "custom" it will be the name you specified.
53+
* */
54+
name: z.string(),
55+
});
56+
57+
export type QueueTypeName = z.infer<typeof QueueTypeName>;
58+
4859
/**
4960
* When retrieving a queue you can either use the queue id,
5061
* or the type and name.
@@ -63,16 +74,6 @@ export type ListQueueOptions = z.infer<typeof ListQueueOptions>;
6374
* const q3 = await queues.retrieve({ type: "custom", name: "my-custom-queue" });
6475
* ```
6576
*/
66-
export const RetrieveQueueParam = z.union([
67-
z.string(),
68-
z.object({
69-
/** "task" or "custom" */
70-
type: QueueType,
71-
/** The name of your queue.
72-
* For "task" type it will be the task id, for "custom" it will be the name you specified.
73-
* */
74-
name: z.string(),
75-
}),
76-
]);
77+
export const RetrieveQueueParam = z.union([z.string(), QueueTypeName]);
7778

7879
export type RetrieveQueueParam = z.infer<typeof RetrieveQueueParam>;

references/hello-world/src/trigger/sdk.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,20 @@ export const sdkMethods = task({
2020
logger.info("failed run", { run });
2121
}
2222

23+
for await (const run of runs.list({
24+
queue: { type: "task", name: "sdk-methods" },
25+
limit: 5,
26+
})) {
27+
logger.info("sdk-methods run", { run });
28+
}
29+
30+
for await (const run of runs.list({
31+
machine: ["small-1x", "small-2x"],
32+
limit: 5,
33+
})) {
34+
logger.info("small machine run", { run });
35+
}
36+
2337
return runs;
2438
},
2539
});

0 commit comments

Comments
 (0)