|
| 1 | +import { json } from "@remix-run/server-runtime"; |
| 2 | +import { z } from "zod"; |
| 3 | +import { |
| 4 | + CreateInputStreamWaitpointRequestBody, |
| 5 | + type CreateInputStreamWaitpointResponseBody, |
| 6 | +} from "@trigger.dev/core/v3"; |
| 7 | +import { WaitpointId } from "@trigger.dev/core/v3/isomorphic"; |
| 8 | +import { $replica } from "~/db.server"; |
| 9 | +import { createWaitpointTag, MAX_TAGS_PER_WAITPOINT } from "~/models/waitpointTag.server"; |
| 10 | +import { |
| 11 | + deleteInputStreamWaitpoint, |
| 12 | + setInputStreamWaitpoint, |
| 13 | +} from "~/services/inputStreamWaitpointCache.server"; |
| 14 | +import { getRealtimeStreamInstance } from "~/services/realtime/v1StreamsGlobal.server"; |
| 15 | +import { createActionApiRoute } from "~/services/routeBuilders/apiBuilder.server"; |
| 16 | +import { parseDelay } from "~/utils/delays"; |
| 17 | +import { resolveIdempotencyKeyTTL } from "~/utils/idempotencyKeys.server"; |
| 18 | +import { engine } from "~/v3/runEngine.server"; |
| 19 | +import { ServiceValidationError } from "~/v3/services/baseService.server"; |
| 20 | + |
| 21 | +const ParamsSchema = z.object({ |
| 22 | + runFriendlyId: z.string(), |
| 23 | +}); |
| 24 | + |
| 25 | +const { action, loader } = createActionApiRoute( |
| 26 | + { |
| 27 | + params: ParamsSchema, |
| 28 | + body: CreateInputStreamWaitpointRequestBody, |
| 29 | + maxContentLength: 1024 * 10, // 10KB |
| 30 | + method: "POST", |
| 31 | + }, |
| 32 | + async ({ authentication, body, params }) => { |
| 33 | + try { |
| 34 | + const run = await $replica.taskRun.findFirst({ |
| 35 | + where: { |
| 36 | + friendlyId: params.runFriendlyId, |
| 37 | + runtimeEnvironmentId: authentication.environment.id, |
| 38 | + }, |
| 39 | + select: { |
| 40 | + id: true, |
| 41 | + friendlyId: true, |
| 42 | + realtimeStreamsVersion: true, |
| 43 | + }, |
| 44 | + }); |
| 45 | + |
| 46 | + if (!run) { |
| 47 | + return json({ error: "Run not found" }, { status: 404 }); |
| 48 | + } |
| 49 | + |
| 50 | + const idempotencyKeyExpiresAt = body.idempotencyKeyTTL |
| 51 | + ? resolveIdempotencyKeyTTL(body.idempotencyKeyTTL) |
| 52 | + : undefined; |
| 53 | + |
| 54 | + const timeout = await parseDelay(body.timeout); |
| 55 | + |
| 56 | + // Process tags (same pattern as api.v1.waitpoints.tokens.ts) |
| 57 | + const bodyTags = typeof body.tags === "string" ? [body.tags] : body.tags; |
| 58 | + |
| 59 | + if (bodyTags && bodyTags.length > MAX_TAGS_PER_WAITPOINT) { |
| 60 | + throw new ServiceValidationError( |
| 61 | + `Waitpoints can only have ${MAX_TAGS_PER_WAITPOINT} tags, you're trying to set ${bodyTags.length}.` |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + if (bodyTags && bodyTags.length > 0) { |
| 66 | + for (const tag of bodyTags) { |
| 67 | + await createWaitpointTag({ |
| 68 | + tag, |
| 69 | + environmentId: authentication.environment.id, |
| 70 | + projectId: authentication.environment.projectId, |
| 71 | + }); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + // Step 1: Create the waitpoint |
| 76 | + const result = await engine.createManualWaitpoint({ |
| 77 | + environmentId: authentication.environment.id, |
| 78 | + projectId: authentication.environment.projectId, |
| 79 | + idempotencyKey: body.idempotencyKey, |
| 80 | + idempotencyKeyExpiresAt, |
| 81 | + timeout, |
| 82 | + tags: bodyTags, |
| 83 | + }); |
| 84 | + |
| 85 | + // Step 2: Cache the mapping in Redis for fast lookup from .send() |
| 86 | + const ttlMs = timeout ? timeout.getTime() - Date.now() : undefined; |
| 87 | + await setInputStreamWaitpoint( |
| 88 | + run.friendlyId, |
| 89 | + body.streamId, |
| 90 | + result.waitpoint.id, |
| 91 | + ttlMs && ttlMs > 0 ? ttlMs : undefined |
| 92 | + ); |
| 93 | + |
| 94 | + // Step 3: Check if data was already sent to this input stream (race condition handling). |
| 95 | + // If .send() landed before .wait(), the data is in the S2 stream but no waitpoint |
| 96 | + // existed to complete. We check from the client's last known position. |
| 97 | + if (!result.isCached) { |
| 98 | + try { |
| 99 | + const realtimeStream = getRealtimeStreamInstance( |
| 100 | + authentication.environment, |
| 101 | + run.realtimeStreamsVersion |
| 102 | + ); |
| 103 | + |
| 104 | + const records = await realtimeStream.readRecords( |
| 105 | + run.friendlyId, |
| 106 | + `$trigger.input:${body.streamId}`, |
| 107 | + body.lastSeqNum |
| 108 | + ); |
| 109 | + |
| 110 | + if (records.length > 0) { |
| 111 | + const record = records[0]!; |
| 112 | + |
| 113 | + // Record data is the raw user payload — no wrapper to unwrap |
| 114 | + await engine.completeWaitpoint({ |
| 115 | + id: result.waitpoint.id, |
| 116 | + output: { |
| 117 | + value: record.data, |
| 118 | + type: "application/json", |
| 119 | + isError: false, |
| 120 | + }, |
| 121 | + }); |
| 122 | + |
| 123 | + // Clean up the Redis cache since we completed it ourselves |
| 124 | + await deleteInputStreamWaitpoint(run.friendlyId, body.streamId); |
| 125 | + } |
| 126 | + } catch { |
| 127 | + // Non-fatal: if the S2 check fails, the waitpoint is still PENDING. |
| 128 | + // The next .send() will complete it via the Redis cache path. |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + return json<CreateInputStreamWaitpointResponseBody>({ |
| 133 | + waitpointId: WaitpointId.toFriendlyId(result.waitpoint.id), |
| 134 | + isCached: result.isCached, |
| 135 | + }); |
| 136 | + } catch (error) { |
| 137 | + if (error instanceof ServiceValidationError) { |
| 138 | + return json({ error: error.message }, { status: 422 }); |
| 139 | + } else if (error instanceof Error) { |
| 140 | + return json({ error: error.message }, { status: 500 }); |
| 141 | + } |
| 142 | + |
| 143 | + return json({ error: "Something went wrong" }, { status: 500 }); |
| 144 | + } |
| 145 | + } |
| 146 | +); |
| 147 | + |
| 148 | +export { action, loader }; |
0 commit comments