|
| 1 | +import { type BatchTaskRunStatus } from "@trigger.dev/database"; |
| 2 | +import { displayableEnvironment } from "~/models/runtimeEnvironment.server"; |
| 3 | +import { engine } from "~/v3/runEngine.server"; |
| 4 | +import { BasePresenter } from "./basePresenter.server"; |
| 5 | + |
| 6 | +type BatchPresenterOptions = { |
| 7 | + environmentId: string; |
| 8 | + batchId: string; |
| 9 | + userId?: string; |
| 10 | +}; |
| 11 | + |
| 12 | +export type BatchPresenterData = Awaited<ReturnType<BatchPresenter["call"]>>; |
| 13 | + |
| 14 | +export class BatchPresenter extends BasePresenter { |
| 15 | + public async call({ environmentId, batchId, userId }: BatchPresenterOptions) { |
| 16 | + const batch = await this._replica.batchTaskRun.findFirst({ |
| 17 | + select: { |
| 18 | + id: true, |
| 19 | + friendlyId: true, |
| 20 | + status: true, |
| 21 | + runCount: true, |
| 22 | + batchVersion: true, |
| 23 | + createdAt: true, |
| 24 | + updatedAt: true, |
| 25 | + completedAt: true, |
| 26 | + processingStartedAt: true, |
| 27 | + successfulRunCount: true, |
| 28 | + failedRunCount: true, |
| 29 | + idempotencyKey: true, |
| 30 | + runtimeEnvironment: { |
| 31 | + select: { |
| 32 | + id: true, |
| 33 | + type: true, |
| 34 | + slug: true, |
| 35 | + orgMember: { |
| 36 | + select: { |
| 37 | + user: { |
| 38 | + select: { |
| 39 | + id: true, |
| 40 | + name: true, |
| 41 | + displayName: true, |
| 42 | + }, |
| 43 | + }, |
| 44 | + }, |
| 45 | + }, |
| 46 | + }, |
| 47 | + }, |
| 48 | + errors: { |
| 49 | + select: { |
| 50 | + id: true, |
| 51 | + index: true, |
| 52 | + taskIdentifier: true, |
| 53 | + error: true, |
| 54 | + errorCode: true, |
| 55 | + createdAt: true, |
| 56 | + }, |
| 57 | + orderBy: { |
| 58 | + index: "asc", |
| 59 | + }, |
| 60 | + }, |
| 61 | + }, |
| 62 | + where: { |
| 63 | + runtimeEnvironmentId: environmentId, |
| 64 | + friendlyId: batchId, |
| 65 | + }, |
| 66 | + }); |
| 67 | + |
| 68 | + if (!batch) { |
| 69 | + throw new Error("Batch not found"); |
| 70 | + } |
| 71 | + |
| 72 | + const hasFinished = batch.status !== "PENDING" && batch.status !== "PROCESSING"; |
| 73 | + const isV2 = batch.batchVersion === "runengine:v2"; |
| 74 | + |
| 75 | + // For v2 batches in PROCESSING state, get live progress from Redis |
| 76 | + // This provides real-time updates without waiting for the batch to complete |
| 77 | + let liveSuccessCount = batch.successfulRunCount ?? 0; |
| 78 | + let liveFailureCount = batch.failedRunCount ?? 0; |
| 79 | + |
| 80 | + if (isV2 && batch.status === "PROCESSING") { |
| 81 | + const liveProgress = await engine.getBatchQueueProgress(batch.id); |
| 82 | + if (liveProgress) { |
| 83 | + liveSuccessCount = liveProgress.successCount; |
| 84 | + liveFailureCount = liveProgress.failureCount; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + return { |
| 89 | + id: batch.id, |
| 90 | + friendlyId: batch.friendlyId, |
| 91 | + status: batch.status as BatchTaskRunStatus, |
| 92 | + runCount: batch.runCount, |
| 93 | + batchVersion: batch.batchVersion, |
| 94 | + isV2, |
| 95 | + createdAt: batch.createdAt.toISOString(), |
| 96 | + updatedAt: batch.updatedAt.toISOString(), |
| 97 | + completedAt: batch.completedAt?.toISOString(), |
| 98 | + processingStartedAt: batch.processingStartedAt?.toISOString(), |
| 99 | + finishedAt: batch.completedAt |
| 100 | + ? batch.completedAt.toISOString() |
| 101 | + : hasFinished |
| 102 | + ? batch.updatedAt.toISOString() |
| 103 | + : undefined, |
| 104 | + hasFinished, |
| 105 | + successfulRunCount: liveSuccessCount, |
| 106 | + failedRunCount: liveFailureCount, |
| 107 | + idempotencyKey: batch.idempotencyKey, |
| 108 | + environment: displayableEnvironment(batch.runtimeEnvironment, userId), |
| 109 | + errors: batch.errors.map((error) => ({ |
| 110 | + id: error.id, |
| 111 | + index: error.index, |
| 112 | + taskIdentifier: error.taskIdentifier, |
| 113 | + error: error.error, |
| 114 | + errorCode: error.errorCode, |
| 115 | + createdAt: error.createdAt.toISOString(), |
| 116 | + })), |
| 117 | + }; |
| 118 | + } |
| 119 | +} |
| 120 | + |
0 commit comments