Skip to content

Commit 493a30c

Browse files
committed
Mirror task run attempt errors and output
1 parent 2cfea9c commit 493a30c

File tree

6 files changed

+49
-29
lines changed

6 files changed

+49
-29
lines changed

apps/webapp/app/v3/services/completeAttempt.server.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ export class CompleteAttemptService extends BaseService {
128128
output: completion.output,
129129
outputType: completion.outputType,
130130
usageDurationMs: completion.usage?.durationMs,
131+
taskRun: {
132+
update: {
133+
output: completion.output,
134+
outputType: completion.outputType,
135+
},
136+
},
131137
},
132138
});
133139

@@ -348,6 +354,15 @@ export class CompleteAttemptService extends BaseService {
348354
})
349355
);
350356
} else {
357+
await this._prisma.taskRun.update({
358+
where: {
359+
id: taskRunAttempt.taskRunId,
360+
},
361+
data: {
362+
error: sanitizedError,
363+
},
364+
});
365+
351366
const finalizeService = new FinalizeTaskRunService();
352367
await finalizeService.call({
353368
id: taskRunAttempt.taskRunId,

apps/webapp/app/v3/services/finalizeTaskRun.server.ts

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,11 @@ import { sanitizeError, TaskRunError } from "@trigger.dev/core/v3";
22
import { type Prisma, type TaskRun } from "@trigger.dev/database";
33
import { logger } from "~/services/logger.server";
44
import { marqs, sanitizeQueueName } from "~/v3/marqs/index.server";
5-
import {
6-
isFailedRunStatus,
7-
type FINAL_ATTEMPT_STATUSES,
8-
type FINAL_RUN_STATUSES,
9-
} from "../taskStatus";
5+
import { generateFriendlyId } from "../friendlyIdentifiers";
6+
import { FINAL_ATTEMPT_STATUSES, isFailedRunStatus, type FINAL_RUN_STATUSES } from "../taskStatus";
107
import { PerformTaskRunAlertsService } from "./alerts/performTaskRunAlerts.server";
118
import { BaseService } from "./baseService.server";
129
import { ResumeDependentParentsService } from "./resumeDependentParents.server";
13-
import { generateFriendlyId } from "../friendlyIdentifiers";
1410

1511
type BaseInput = {
1612
id: string;
@@ -60,36 +56,20 @@ export class FinalizeTaskRunService extends BaseService {
6056
completedAt,
6157
});
6258

63-
let output: string | undefined = undefined;
64-
let outputType: string | undefined = undefined;
65-
66-
if (status === "COMPLETED_SUCCESSFULLY") {
67-
// We need to get the output from the attempt
68-
const attempt = await this._prisma.taskRunAttempt.findFirst({
69-
where: { taskRunId: id, status: "COMPLETED", output: { not: null } },
70-
orderBy: { id: "desc" },
71-
select: {
72-
output: true,
73-
outputType: true,
74-
},
75-
});
76-
77-
if (attempt) {
78-
output = attempt.output ?? undefined;
79-
outputType = attempt.outputType;
80-
}
81-
}
82-
8359
const run = await this._prisma.taskRun.update({
8460
where: { id },
85-
data: { status, expiredAt, completedAt, output, outputType },
61+
data: { status, expiredAt, completedAt },
8662
...(include ? { include } : {}),
8763
});
8864

8965
if (attemptStatus || error) {
9066
await this.finalizeAttempt({ attemptStatus, error, run });
9167
}
9268

69+
if (isFailedRunStatus(run.status)) {
70+
await this.finalizeRunError(run, error);
71+
}
72+
9373
//resume any dependencies
9474
const resumeService = new ResumeDependentParentsService(this._prisma);
9575
const result = await resumeService.call({ id: run.id });
@@ -108,6 +88,19 @@ export class FinalizeTaskRunService extends BaseService {
10888
return run as Output<T>;
10989
}
11090

91+
async finalizeRunError(run: TaskRun, error?: TaskRunError) {
92+
if (!error) {
93+
return;
94+
}
95+
96+
await this._prisma.taskRun.update({
97+
where: { id: run.id },
98+
data: {
99+
error: sanitizeError(error),
100+
},
101+
});
102+
}
103+
111104
async finalizeAttempt({
112105
attemptStatus,
113106
error,

apps/webapp/app/v3/taskStatus.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,13 @@ export const FINAL_ATTEMPT_STATUSES = [
5050
"COMPLETED",
5151
"FAILED",
5252
] satisfies TaskRunAttemptStatus[];
53+
5354
export type FINAL_ATTEMPT_STATUSES = (typeof FINAL_ATTEMPT_STATUSES)[number];
5455

56+
export const FAILED_ATTEMPT_STATUSES = ["FAILED", "CANCELED"] satisfies TaskRunAttemptStatus[];
57+
58+
export type FAILED_ATTEMPT_STATUSES = (typeof FAILED_ATTEMPT_STATUSES)[number];
59+
5560
export const FREEZABLE_RUN_STATUSES: TaskRunStatus[] = ["EXECUTING", "RETRYING_AFTER_FAILURE"];
5661
export const FREEZABLE_ATTEMPT_STATUSES: TaskRunAttemptStatus[] = ["EXECUTING", "FAILED"];
5762

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- AlterTable
2+
ALTER TABLE "TaskRun" ADD COLUMN "error" JSONB;

packages/database/prisma/schema.prisma

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1759,6 +1759,9 @@ model TaskRun {
17591759
output String?
17601760
outputType String @default("application/json")
17611761
1762+
/// Run error
1763+
error Json?
1764+
17621765
@@unique([runtimeEnvironmentId, taskIdentifier, idempotencyKey])
17631766
// Finding child runs
17641767
@@index([parentTaskRunId])

references/v3-catalog/src/trigger/runMetadata.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { logger, task, metadata } from "@trigger.dev/sdk/v3";
1+
import { logger, task, metadata, AbortTaskRunError } from "@trigger.dev/sdk/v3";
22

33
export const runMetadataTask = task({
44
id: "run-metadata-task",
@@ -55,7 +55,9 @@ export const runMetadataChildTask = task({
5555

5656
export const runMetadataChildTask2 = task({
5757
id: "run-metadata-child-task-2",
58-
run: async (payload: any, { ctx }) => {},
58+
run: async (payload: any, { ctx }) => {
59+
throw new AbortTaskRunError("aborting");
60+
},
5961
});
6062

6163
export const myTask = task({

0 commit comments

Comments
 (0)