Skip to content

Commit 0e77747

Browse files
authored
fix: runs.retrieve now uses the output stored on the TaskRun table instead of deprecated attempts table (#1853)
* fix: runs.retrieve now uses the output stored on the TaskRun table instead of deprecated attempts table * Fixed typecheck error
1 parent 8977d25 commit 0e77747

File tree

2 files changed

+115
-33
lines changed

2 files changed

+115
-33
lines changed

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

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ const commonRunSelect = {
5252
friendlyId: true,
5353
},
5454
},
55+
runTags: true,
5556
} satisfies Prisma.TaskRunSelect;
5657

5758
type CommonRelatedRun = Prisma.Result<
@@ -69,26 +70,29 @@ export class ApiRetrieveRunPresenter extends BasePresenter {
6970
friendlyId,
7071
runtimeEnvironmentId: env.id,
7172
},
72-
include: {
73-
attempts: true,
74-
lockedToVersion: true,
75-
tags: true,
76-
batch: {
73+
select: {
74+
...commonRunSelect,
75+
payload: true,
76+
payloadType: true,
77+
output: true,
78+
outputType: true,
79+
error: true,
80+
attempts: {
7781
select: {
7882
id: true,
79-
friendlyId: true,
8083
},
8184
},
85+
attemptNumber: true,
86+
engine: true,
87+
taskEventStore: true,
8288
parentTaskRun: {
8389
select: commonRunSelect,
8490
},
8591
rootTaskRun: {
8692
select: commonRunSelect,
8793
},
8894
childRuns: {
89-
select: {
90-
...commonRunSelect,
91-
},
95+
select: commonRunSelect,
9296
},
9397
},
9498
});
@@ -124,29 +128,23 @@ export class ApiRetrieveRunPresenter extends BasePresenter {
124128
}
125129

126130
if (taskRun.status === "COMPLETED_SUCCESSFULLY") {
127-
const completedAttempt = taskRun.attempts.find(
128-
(a) => a.status === "COMPLETED" && typeof a.output !== null
129-
);
130-
131-
if (completedAttempt && completedAttempt.output) {
132-
const outputPacket = await conditionallyImportPacket({
133-
data: completedAttempt.output,
134-
dataType: completedAttempt.outputType,
135-
});
131+
const outputPacket = await conditionallyImportPacket({
132+
data: taskRun.output ?? undefined,
133+
dataType: taskRun.outputType,
134+
});
136135

137-
if (
138-
outputPacket.dataType === "application/store" &&
139-
typeof outputPacket.data === "string"
140-
) {
141-
$outputPresignedUrl = await generatePresignedUrl(
142-
env.project.externalRef,
143-
env.slug,
144-
outputPacket.data,
145-
"GET"
146-
);
147-
} else {
148-
$output = await parsePacket(outputPacket);
149-
}
136+
if (
137+
outputPacket.dataType === "application/store" &&
138+
typeof outputPacket.data === "string"
139+
) {
140+
$outputPresignedUrl = await generatePresignedUrl(
141+
env.project.externalRef,
142+
env.slug,
143+
outputPacket.data,
144+
"GET"
145+
);
146+
} else {
147+
$output = await parsePacket(outputPacket);
150148
}
151149
}
152150

@@ -159,7 +157,8 @@ export class ApiRetrieveRunPresenter extends BasePresenter {
159157
error: ApiRetrieveRunPresenter.apiErrorFromError(taskRun.error),
160158
schedule: await resolveSchedule(taskRun),
161159
// We're removing attempts from the API
162-
attemptCount: taskRun.attempts.length,
160+
attemptCount:
161+
taskRun.engine === "V1" ? taskRun.attempts.length : taskRun.attemptNumber ?? 0,
163162
attempts: [],
164163
relatedRuns: {
165164
root: taskRun.rootTaskRun

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

Lines changed: 84 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
import { logger, task, wait } from "@trigger.dev/sdk";
1+
import { logger, runs, task, wait } from "@trigger.dev/sdk";
2+
import assert from "node:assert";
3+
import { setTimeout } from "node:timers/promises";
24

35
export const tagsTester = task({
46
id: "tags-tester",
@@ -20,3 +22,84 @@ export const tagsChildTask = task({
2022
logger.log("Hello, world from the child", { payload });
2123
},
2224
});
25+
26+
// Task that will be triggered with tags
27+
export const taggedTask = task({
28+
id: "tagged-task",
29+
run: async (payload: { waitSeconds: number }, { ctx }) => {
30+
logger.info("Running tagged task", { tags: ctx.run.tags });
31+
32+
// Verify initial tags from trigger
33+
const expectedInitialTags = ["test-tag-1", "test-tag-2"];
34+
for (const tag of expectedInitialTags) {
35+
if (!ctx.run.tags.includes(tag)) {
36+
throw new Error(`Expected tag ${tag} to be present initially`);
37+
}
38+
}
39+
40+
// Wait a bit to ensure we can query the running task
41+
await setTimeout(payload.waitSeconds * 1000);
42+
43+
return {
44+
initialTags: ctx.run.tags,
45+
};
46+
},
47+
});
48+
49+
// Test task that verifies tag behavior
50+
export const tagTestTask = task({
51+
id: "tag-test",
52+
run: async (payload, { ctx }) => {
53+
logger.info("Starting tag verification test");
54+
55+
// Trigger a task with initial tags
56+
const handle = await taggedTask.trigger(
57+
{ waitSeconds: 3 },
58+
{
59+
tags: ["test-tag-1", "test-tag-2"],
60+
}
61+
);
62+
63+
// Wait a moment to ensure the task is running
64+
await setTimeout(3_000);
65+
66+
// Query for running tasks with our tags
67+
const runningTasks = await runs.list({
68+
status: "EXECUTING",
69+
tag: ["test-tag-1", "test-tag-2"],
70+
});
71+
72+
let foundRun = false;
73+
for await (const run of runningTasks) {
74+
if (run.id === handle.id) {
75+
foundRun = true;
76+
break;
77+
}
78+
}
79+
80+
if (!foundRun) {
81+
throw new Error("Could not find running task with tags test-tag-1 and test-tag-2");
82+
}
83+
84+
logger.info("Found running task with tags test-tag-1 and test-tag-2");
85+
86+
await wait.for({ seconds: 10 });
87+
88+
const finalRun = await runs.retrieve<typeof taggedTask>(handle.id);
89+
90+
logger.info("Final run", { finalRun });
91+
92+
assert.ok(finalRun.status === "COMPLETED", "Run should be completed");
93+
assert.ok(finalRun.output, "Output should be defined");
94+
95+
// Verify the tags were preserved in the task context
96+
const outputTags = finalRun.output.initialTags;
97+
if (!outputTags.includes("test-tag-1") || !outputTags.includes("test-tag-2")) {
98+
throw new Error(
99+
`Expected tags test-tag-1 and test-tag-2 in output, got ${outputTags.join(", ")}`
100+
);
101+
}
102+
103+
logger.info("✅ Tag verification test passed");
104+
},
105+
});

0 commit comments

Comments
 (0)