Skip to content

Commit c4e45f4

Browse files
committed
runs.list filtering for queue and machine
1 parent d35f32d commit c4e45f4

File tree

6 files changed

+120
-12
lines changed

6 files changed

+120
-12
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: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
type ListRunResponse,
33
type ListRunResponseItem,
4+
MachinePresetName,
45
parsePacket,
56
RunStatus,
67
} from "@trigger.dev/core/v3";
@@ -106,6 +107,34 @@ export const ApiRunListSearchParams = z.object({
106107
"filter[createdAt][to]": CoercedDate,
107108
"filter[createdAt][period]": z.string().optional(),
108109
"filter[batch]": z.string().optional(),
110+
"filter[queue]": z
111+
.string()
112+
.optional()
113+
.transform((value) => {
114+
return value ? value.split(",") : undefined;
115+
}),
116+
"filter[machine]": z
117+
.string()
118+
.optional()
119+
.transform((value, ctx) => {
120+
const values = value ? value.split(",") : undefined;
121+
if (!values) {
122+
return undefined;
123+
}
124+
125+
const parsedValues = values.map((v) => MachinePresetName.safeParse(v));
126+
const invalidValues = parsedValues.filter((result) => !result.success);
127+
if (invalidValues.length > 0) {
128+
ctx.addIssue({
129+
code: z.ZodIssueCode.custom,
130+
message: `Invalid machine values: ${invalidValues.join(", ")}`,
131+
});
132+
133+
return z.NEVER;
134+
}
135+
136+
return parsedValues.map((result) => result.data).filter(Boolean);
137+
}),
109138
});
110139

111140
type ApiRunListSearchParams = z.infer<typeof ApiRunListSearchParams>;
@@ -213,6 +242,14 @@ export class ApiRunListPresenter extends BasePresenter {
213242
options.batchId = searchParams["filter[batch]"];
214243
}
215244

245+
if (searchParams["filter[queue]"]) {
246+
options.queues = searchParams["filter[queue]"];
247+
}
248+
249+
if (searchParams["filter[machine]"]) {
250+
options.machines = searchParams["filter[machine]"];
251+
}
252+
216253
const presenter = new NextRunListPresenter(this._prisma, clickhouseClient);
217254

218255
logger.debug("Calling RunListPresenter", { options });

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,
@@ -1149,11 +1150,35 @@ function createSearchQueryForListRuns(query?: ListRunsQueryParams): URLSearchPar
11491150
if (query.batch) {
11501151
searchParams.append("filter[batch]", query.batch);
11511152
}
1153+
1154+
if (query.queue) {
1155+
searchParams.append(
1156+
"filter[queue]",
1157+
Array.isArray(query.queue)
1158+
? query.queue.map((q) => queueNameFromQueueTypeName(q)).join(",")
1159+
: queueNameFromQueueTypeName(query.queue)
1160+
);
1161+
}
1162+
1163+
if (query.machine) {
1164+
searchParams.append(
1165+
"filter[machine]",
1166+
Array.isArray(query.machine) ? query.machine.join(",") : query.machine
1167+
);
1168+
}
11521169
}
11531170

11541171
return searchParams;
11551172
}
11561173

1174+
function queueNameFromQueueTypeName(queue: QueueTypeName): string {
1175+
if (queue.type === "task") {
1176+
return `task/${queue.name}`;
1177+
}
1178+
1179+
return queue.name;
1180+
}
1181+
11571182
function createSearchQueryForListWaitpointTokens(
11581183
query?: ListWaitpointTokensQueryParams
11591184
): 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
@@ -47,6 +47,17 @@ export const ListQueueOptions = z.object({
4747

4848
export type ListQueueOptions = z.infer<typeof ListQueueOptions>;
4949

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

8081
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)