|
| 1 | +import { json } from "@remix-run/server-runtime"; |
| 2 | +import { |
| 3 | + CompleteWaitpointTokenRequestBody, |
| 4 | + CompleteWaitpointTokenResponseBody, |
| 5 | + conditionallyExportPacket, |
| 6 | + CreateWaitpointTokenResponseBody, |
| 7 | + stringifyIO, |
| 8 | + WaitForWaitpointTokenRequestBody, |
| 9 | + WaitForWaitpointTokenResponseBody, |
| 10 | +} from "@trigger.dev/core/v3"; |
| 11 | +import { RunId, WaitpointId } from "@trigger.dev/core/v3/apps"; |
| 12 | +import { z } from "zod"; |
| 13 | +import { $replica } from "~/db.server"; |
| 14 | +import { logger } from "~/services/logger.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 | + |
| 20 | +const { action } = createActionApiRoute( |
| 21 | + { |
| 22 | + params: z.object({ |
| 23 | + runFriendlyId: z.string(), |
| 24 | + waitpointFriendlyId: z.string(), |
| 25 | + }), |
| 26 | + body: WaitForWaitpointTokenRequestBody, |
| 27 | + maxContentLength: 1024 * 10, // 10KB |
| 28 | + method: "POST", |
| 29 | + }, |
| 30 | + async ({ authentication, body, params }) => { |
| 31 | + // Resume tokens are actually just waitpoints |
| 32 | + const waitpointId = WaitpointId.toId(params.waitpointFriendlyId); |
| 33 | + const runId = RunId.toId(params.runFriendlyId); |
| 34 | + |
| 35 | + const timeout = await parseDelay(body.timeout); |
| 36 | + |
| 37 | + try { |
| 38 | + //check permissions |
| 39 | + const waitpoint = await $replica.waitpoint.findFirst({ |
| 40 | + where: { |
| 41 | + id: waitpointId, |
| 42 | + environmentId: authentication.environment.id, |
| 43 | + }, |
| 44 | + }); |
| 45 | + |
| 46 | + if (!waitpoint) { |
| 47 | + throw json({ error: "Waitpoint not found" }, { status: 404 }); |
| 48 | + } |
| 49 | + |
| 50 | + const result = await engine.blockRunWithWaitpoint({ |
| 51 | + runId, |
| 52 | + waitpoints: [waitpointId], |
| 53 | + environmentId: authentication.environment.id, |
| 54 | + projectId: authentication.environment.project.id, |
| 55 | + failAfter: timeout, |
| 56 | + }); |
| 57 | + |
| 58 | + return json<WaitForWaitpointTokenResponseBody>( |
| 59 | + { |
| 60 | + success: true, |
| 61 | + }, |
| 62 | + { status: 200 } |
| 63 | + ); |
| 64 | + } catch (error) { |
| 65 | + logger.error("Failed to wait for waitpoint", { runId, waitpointId, error }); |
| 66 | + throw json({ error: "Failed to wait for waitpoint token" }, { status: 500 }); |
| 67 | + } |
| 68 | + } |
| 69 | +); |
| 70 | + |
| 71 | +export { action }; |
0 commit comments