-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathbatchSystem.ts
More file actions
234 lines (206 loc) · 7.21 KB
/
Copy pathbatchSystem.ts
File metadata and controls
234 lines (206 loc) · 7.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import { startSpan } from "@internal/tracing";
import { TaskRunError } from "@trigger.dev/core/v3/schemas";
import { isFinalRunStatus } from "../statuses.js";
import { SystemResources } from "./systems.js";
import { WaitpointSystem } from "./waitpointSystem.js";
export type BatchSystemOptions = {
resources: SystemResources;
waitpointSystem: WaitpointSystem;
};
export class BatchSystem {
private readonly $: SystemResources;
private readonly waitpointSystem: WaitpointSystem;
constructor(private readonly options: BatchSystemOptions) {
this.$ = options.resources;
this.waitpointSystem = options.waitpointSystem;
}
public async scheduleCompleteBatch({ batchId }: { batchId: string }): Promise<void> {
await this.$.worker.enqueue({
//this will debounce the call
id: `tryCompleteBatch:${batchId}`,
job: "tryCompleteBatch",
payload: { batchId: batchId },
//200ms in the future
availableAt: new Date(Date.now() + 200),
});
}
public async performCompleteBatch({ batchId }: { batchId: string }): Promise<void> {
await this.#tryCompleteBatch({ batchId });
}
public async scheduleExpireBatch({
batchId,
availableAt,
}: {
batchId: string;
availableAt: Date;
}): Promise<void> {
await this.$.worker.enqueue({
// Stable id dedupes repeated schedules for the same batch.
id: `expireBatch:${batchId}`,
job: "expireBatch",
payload: { batchId },
availableAt,
});
}
/**
* Terminally fail a batch whose Phase 2 item stream never sealed it, and resolve
* the parent's batchTriggerAndWait waitpoint with an error so the parent resumes
* with a failure instead of hanging forever.
*
* Idempotent and race-safe: if the stream sealed the batch (or it otherwise
* progressed past an unsealed PENDING state) in the meantime, this is a no-op.
*/
public async expireBatch({ batchId }: { batchId: string }): Promise<void> {
return startSpan(this.$.tracer, "expireBatch", async (span) => {
span.setAttribute("batchId", batchId);
const batch = await this.$.prisma.batchTaskRun.findFirst({
select: { status: true, sealed: true },
where: { id: batchId },
});
if (!batch) {
this.$.logger.debug("expireBatch: batch doesn't exist", { batchId });
return;
}
// The stream sealed the batch, or it already progressed — nothing to fail.
if (batch.sealed || batch.status !== "PENDING") {
this.$.logger.debug("expireBatch: batch already sealed or no longer PENDING", {
batchId,
status: batch.status,
sealed: batch.sealed,
});
return;
}
// Conditional update guards against racing a late seal — whichever loses no-ops.
const aborted = await this.$.prisma.batchTaskRun.updateMany({
where: { id: batchId, sealed: false, status: "PENDING" },
data: {
status: "ABORTED",
completedAt: new Date(),
processingCompletedAt: new Date(),
},
});
if (aborted.count === 0) {
this.$.logger.debug("expireBatch: lost race to seal, no-op", { batchId });
return;
}
// Only batchTriggerAndWait blocks a parent, so only it has a waitpoint to resolve.
const waitpoint = await this.$.prisma.waitpoint.findFirst({
where: { completedByBatchId: batchId },
});
if (!waitpoint) {
this.$.logger.debug("expireBatch: no waitpoint to resolve (fire-and-forget batch)", {
batchId,
});
return;
}
const error: TaskRunError = {
type: "STRING_ERROR",
raw: "Batch items could not be streamed before the batch timed out",
};
await this.waitpointSystem.completeWaitpoint({
id: waitpoint.id,
output: { value: JSON.stringify(error), isError: true },
});
this.$.logger.warn("expireBatch: aborted unsealed batch and resumed parent with error", {
batchId,
waitpointId: waitpoint.id,
});
});
}
/**
* Checks to see if all runs for a BatchTaskRun are completed, if they are then update the status.
* This isn't used operationally, but it's used for the Batches dashboard page.
*/
async #tryCompleteBatch({ batchId }: { batchId: string }) {
return startSpan(this.$.tracer, "#tryCompleteBatch", async (span) => {
const batch = await this.$.prisma.batchTaskRun.findFirst({
select: {
status: true,
runtimeEnvironmentId: true,
processingJobsCount: true,
runCount: true,
batchVersion: true,
successfulRunCount: true,
failedRunCount: true,
},
where: {
id: batchId,
},
});
if (!batch) {
this.$.logger.error("#tryCompleteBatch batch doesn't exist", { batchId });
return;
}
if (batch.status === "COMPLETED") {
this.$.logger.debug("#tryCompleteBatch: Batch already completed", { batchId });
return;
}
// Check if all runs are created (or accounted for with failures)
// v2 batches use successfulRunCount + failedRunCount, v1 uses processingJobsCount
const isNewBatch = batch.batchVersion === "runengine:v2";
let processedRunCount: number;
if (isNewBatch) {
// For v2/v3 batches, we need to count both successful and failed runs
const successfulCount = batch.successfulRunCount ?? 0;
const failedCount = batch.failedRunCount ?? 0;
processedRunCount = successfulCount + failedCount;
} else {
processedRunCount = batch.processingJobsCount;
}
if (processedRunCount < batch.runCount) {
this.$.logger.debug("#tryCompleteBatch: Not all runs are processed yet", {
batchId,
processedRunCount,
runCount: batch.runCount,
isNewBatch,
});
return;
}
const runs = await this.$.runStore.findRuns(
{
select: {
id: true,
status: true,
},
where: {
batchId,
runtimeEnvironmentId: batch.runtimeEnvironmentId,
},
},
this.$.prisma
);
if (runs.every((r) => isFinalRunStatus(r.status))) {
this.$.logger.debug("#tryCompleteBatch: All runs are completed", { batchId });
await this.$.prisma.batchTaskRun.update({
where: {
id: batchId,
},
data: {
status: "COMPLETED",
},
});
//get waitpoint (if there is one)
const waitpoint = await this.$.prisma.waitpoint.findFirst({
where: {
completedByBatchId: batchId,
},
});
if (!waitpoint) {
this.$.logger.debug(
"RunEngine.unblockRunForBatch(): Waitpoint not found. This is ok, because only batchTriggerAndWait has waitpoints",
{
batchId,
}
);
return;
}
await this.waitpointSystem.completeWaitpoint({
id: waitpoint.id,
output: { value: "Batch waitpoint completed", isError: false },
});
} else {
this.$.logger.debug("#tryCompleteBatch: Not all runs are completed", { batchId });
}
});
}
}