|
| 1 | +import { RunId } from "@trigger.dev/core/v3/isomorphic"; |
| 2 | +import type { PrismaClientOrTransaction, TaskRun } from "@trigger.dev/database"; |
| 3 | +import { logger } from "~/services/logger.server"; |
| 4 | +import { resolveIdempotencyKeyTTL } from "~/utils/idempotencyKeys.server"; |
| 5 | +import type { RunEngine } from "~/v3/runEngine.server"; |
| 6 | +import type { TraceEventConcern, TriggerTaskRequest } from "../types"; |
| 7 | + |
| 8 | +export type IdempotencyKeyConcernResult = |
| 9 | + | { isCached: true; run: TaskRun } |
| 10 | + | { isCached: false; idempotencyKey?: string; idempotencyKeyExpiresAt?: Date }; |
| 11 | + |
| 12 | +export class IdempotencyKeyConcern { |
| 13 | + constructor( |
| 14 | + private readonly prisma: PrismaClientOrTransaction, |
| 15 | + private readonly engine: RunEngine, |
| 16 | + private readonly traceEventConcern: TraceEventConcern |
| 17 | + ) {} |
| 18 | + |
| 19 | + async handleTriggerRequest(request: TriggerTaskRequest): Promise<IdempotencyKeyConcernResult> { |
| 20 | + const idempotencyKey = request.options?.idempotencyKey ?? request.body.options?.idempotencyKey; |
| 21 | + const idempotencyKeyExpiresAt = |
| 22 | + request.options?.idempotencyKeyExpiresAt ?? |
| 23 | + resolveIdempotencyKeyTTL(request.body.options?.idempotencyKeyTTL) ?? |
| 24 | + new Date(Date.now() + 24 * 60 * 60 * 1000 * 30); // 30 days |
| 25 | + |
| 26 | + if (!idempotencyKey) { |
| 27 | + return { isCached: false, idempotencyKey, idempotencyKeyExpiresAt }; |
| 28 | + } |
| 29 | + |
| 30 | + const existingRun = idempotencyKey |
| 31 | + ? await this.prisma.taskRun.findFirst({ |
| 32 | + where: { |
| 33 | + runtimeEnvironmentId: request.environment.id, |
| 34 | + idempotencyKey, |
| 35 | + taskIdentifier: request.taskId, |
| 36 | + }, |
| 37 | + include: { |
| 38 | + associatedWaitpoint: true, |
| 39 | + }, |
| 40 | + }) |
| 41 | + : undefined; |
| 42 | + |
| 43 | + if (existingRun) { |
| 44 | + if (existingRun.idempotencyKeyExpiresAt && existingRun.idempotencyKeyExpiresAt < new Date()) { |
| 45 | + logger.debug("[TriggerTaskService][call] Idempotency key has expired", { |
| 46 | + idempotencyKey: request.options?.idempotencyKey, |
| 47 | + run: existingRun, |
| 48 | + }); |
| 49 | + |
| 50 | + // Update the existing run to remove the idempotency key |
| 51 | + await this.prisma.taskRun.updateMany({ |
| 52 | + where: { id: existingRun.id, idempotencyKey }, |
| 53 | + data: { idempotencyKey: null, idempotencyKeyExpiresAt: null }, |
| 54 | + }); |
| 55 | + } else { |
| 56 | + const associatedWaitpoint = existingRun.associatedWaitpoint; |
| 57 | + const parentRunId = request.body.options?.parentRunId; |
| 58 | + const resumeParentOnCompletion = request.body.options?.resumeParentOnCompletion; |
| 59 | + //We're using `andWait` so we need to block the parent run with a waitpoint |
| 60 | + if (associatedWaitpoint && resumeParentOnCompletion && parentRunId) { |
| 61 | + await this.traceEventConcern.traceIdempotentRun( |
| 62 | + request, |
| 63 | + { |
| 64 | + existingRun, |
| 65 | + idempotencyKey, |
| 66 | + incomplete: associatedWaitpoint.status === "PENDING", |
| 67 | + isError: associatedWaitpoint.outputIsError, |
| 68 | + }, |
| 69 | + async (event) => { |
| 70 | + //block run with waitpoint |
| 71 | + await this.engine.blockRunWithWaitpoint({ |
| 72 | + runId: RunId.fromFriendlyId(parentRunId), |
| 73 | + waitpoints: associatedWaitpoint.id, |
| 74 | + spanIdToComplete: event.spanId, |
| 75 | + batch: request.options?.batchId |
| 76 | + ? { |
| 77 | + id: request.options.batchId, |
| 78 | + index: request.options.batchIndex ?? 0, |
| 79 | + } |
| 80 | + : undefined, |
| 81 | + projectId: request.environment.projectId, |
| 82 | + organizationId: request.environment.organizationId, |
| 83 | + tx: this.prisma, |
| 84 | + releaseConcurrency: request.body.options?.releaseConcurrency, |
| 85 | + }); |
| 86 | + } |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + return { isCached: true, run: existingRun }; |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return { isCached: false, idempotencyKey, idempotencyKeyExpiresAt }; |
| 95 | + } |
| 96 | +} |
0 commit comments