Skip to content

Commit ed972ac

Browse files
matt-aitkensamejr
authored andcommitted
Don't create an attempt if the run is final, batchTriggerAndWait bad continue fix (#1698)
* WIP fix for ResumeAttemptService selecting the wrong attempt (which has no error or output) * Don’t create an attempt if the run is already in a final status * Don’t get all the columns for the query. Improved the logging. * Added a log to the batch example * Filter out the undefined values
1 parent 2c02c8b commit ed972ac

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

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

+7
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { BaseService, ServiceValidationError } from "./baseService.server";
1111
import { CrashTaskRunService } from "./crashTaskRun.server";
1212
import { ExpireEnqueuedRunService } from "./expireEnqueuedRun.server";
1313
import { findQueueInEnvironment } from "~/models/taskQueue.server";
14+
import { FINAL_RUN_STATUSES } from "../taskStatus";
1415

1516
export class CreateTaskRunAttemptService extends BaseService {
1617
public async call({
@@ -91,11 +92,17 @@ export class CreateTaskRunAttemptService extends BaseService {
9192

9293
span.setAttribute("taskRunId", taskRun.id);
9394
span.setAttribute("taskRunFriendlyId", taskRun.friendlyId);
95+
span.setAttribute("taskRunStatus", taskRun.status);
9496

9597
if (taskRun.status === "CANCELED") {
9698
throw new ServiceValidationError("Task run is cancelled", 400);
9799
}
98100

101+
// If the run is finalized, it's pointless to create another attempt
102+
if (FINAL_RUN_STATUSES.includes(taskRun.status)) {
103+
throw new ServiceValidationError("Task run is already finished", 400);
104+
}
105+
99106
const lockedBy = taskRun.lockedBy;
100107

101108
if (!lockedBy) {

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

+30-6
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ export class ResumeAttemptService extends BaseService {
2727
take: 1,
2828
select: {
2929
id: true,
30+
number: true,
31+
status: true,
3032
},
3133
} satisfies Prisma.TaskRunInclude["attempts"];
3234

@@ -37,9 +39,9 @@ export class ResumeAttemptService extends BaseService {
3739
include: {
3840
taskRun: true,
3941
dependencies: {
40-
include: {
42+
select: {
4143
taskRun: {
42-
include: {
44+
select: {
4345
attempts: latestAttemptSelect,
4446
},
4547
},
@@ -50,11 +52,11 @@ export class ResumeAttemptService extends BaseService {
5052
take: 1,
5153
},
5254
batchDependencies: {
53-
include: {
55+
select: {
5456
items: {
55-
include: {
57+
select: {
5658
taskRun: {
57-
include: {
59+
select: {
5860
attempts: latestAttemptSelect,
5961
},
6062
},
@@ -130,7 +132,29 @@ export class ResumeAttemptService extends BaseService {
130132
return;
131133
}
132134

133-
completedAttemptIds = dependentBatchItems.map((item) => item.taskRun.attempts[0]?.id);
135+
//find the best attempt for each batch item
136+
//it should be the most recent one in a final state
137+
const finalAttempts = dependentBatchItems
138+
.map((item) => {
139+
return item.taskRun.attempts
140+
.filter((a) => FINAL_ATTEMPT_STATUSES.includes(a.status))
141+
.sort((a, b) => b.number - a.number)
142+
.at(0);
143+
})
144+
.filter(Boolean);
145+
146+
completedAttemptIds = finalAttempts.map((a) => a.id);
147+
148+
if (completedAttemptIds.length !== dependentBatchItems.length) {
149+
this._logger.error("[ResumeAttemptService] not all batch items have attempts", {
150+
runId: attempt.taskRunId,
151+
completedAttemptIds,
152+
finalAttempts,
153+
dependentBatchItems,
154+
});
155+
156+
return;
157+
}
134158
} else {
135159
this._logger.error("No batch dependency");
136160
return;

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

+2
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ export const batchTask = task({
8080

8181
const results = await childTask.batchTriggerAndWait(items);
8282

83+
logger.info("Batch task complete", { results });
84+
8385
return {
8486
batchCount: payload.count,
8587
results,

0 commit comments

Comments
 (0)