|
| 1 | +import type { ActionFunctionArgs } from "@remix-run/server-runtime"; |
| 2 | +import { json } from "@remix-run/server-runtime"; |
| 3 | +import { InvokeJobRequestBodySchema } from "@trigger.dev/core"; |
| 4 | +import { z } from "zod"; |
| 5 | +import { PrismaErrorSchema } from "~/db.server"; |
| 6 | +import { authenticateApiRequest } from "~/services/apiAuth.server"; |
| 7 | +import { InvokeJobService } from "~/services/jobs/invokeJob.server"; |
| 8 | +import { logger } from "~/services/logger.server"; |
| 9 | + |
| 10 | +const ParamsSchema = z.object({ |
| 11 | + jobSlug: z.string(), |
| 12 | +}); |
| 13 | + |
| 14 | +const HeadersSchema = z.object({ |
| 15 | + "idempotency-key": z.string().optional().nullable(), |
| 16 | + "trigger-version": z.string().optional().nullable(), |
| 17 | +}); |
| 18 | + |
| 19 | +export async function action({ request, params }: ActionFunctionArgs) { |
| 20 | + // Ensure this is a POST request |
| 21 | + if (request.method.toUpperCase() !== "POST") { |
| 22 | + return { status: 405, body: "Method Not Allowed" }; |
| 23 | + } |
| 24 | + |
| 25 | + // Authenticate the request |
| 26 | + const authenticationResult = await authenticateApiRequest(request); |
| 27 | + |
| 28 | + if (!authenticationResult) { |
| 29 | + return json({ error: "Invalid or Missing API Key" }, { status: 401 }); |
| 30 | + } |
| 31 | + |
| 32 | + const parsed = ParamsSchema.safeParse(params); |
| 33 | + |
| 34 | + if (!parsed.success) { |
| 35 | + return json({ error: "Invalid or Missing jobSlug" }, { status: 400 }); |
| 36 | + } |
| 37 | + |
| 38 | + const { jobSlug } = parsed.data; |
| 39 | + |
| 40 | + const headers = HeadersSchema.safeParse(Object.fromEntries(request.headers)); |
| 41 | + |
| 42 | + if (!headers.success) { |
| 43 | + return json({ error: "Invalid headers" }, { status: 400 }); |
| 44 | + } |
| 45 | + |
| 46 | + const { "idempotency-key": idempotencyKey, "trigger-version": triggerVersion } = headers.data; |
| 47 | + |
| 48 | + // Now parse the request body |
| 49 | + const anyBody = await request.json(); |
| 50 | + |
| 51 | + logger.debug("InvokeJobService.call() request body", { |
| 52 | + body: anyBody, |
| 53 | + jobSlug, |
| 54 | + idempotencyKey, |
| 55 | + triggerVersion, |
| 56 | + }); |
| 57 | + |
| 58 | + const body = InvokeJobRequestBodySchema.safeParse(anyBody); |
| 59 | + |
| 60 | + if (!body.success) { |
| 61 | + return json({ error: "Invalid request body" }, { status: 400 }); |
| 62 | + } |
| 63 | + |
| 64 | + const service = new InvokeJobService(); |
| 65 | + |
| 66 | + try { |
| 67 | + const run = await service.call( |
| 68 | + authenticationResult.environment, |
| 69 | + jobSlug, |
| 70 | + body.data, |
| 71 | + idempotencyKey ?? undefined |
| 72 | + ); |
| 73 | + |
| 74 | + if (!run) { |
| 75 | + return json({ error: "Job count not be invoked" }, { status: 500 }); |
| 76 | + } |
| 77 | + |
| 78 | + return json({ id: run.id }); |
| 79 | + } catch (error) { |
| 80 | + const prismaError = PrismaErrorSchema.safeParse(error); |
| 81 | + // Record not found in the database |
| 82 | + if (prismaError.success && prismaError.data.code === "P2005") { |
| 83 | + return json({ error: "Job not found" }, { status: 404 }); |
| 84 | + } else { |
| 85 | + return json({ error: "Internal Server Error" }, { status: 500 }); |
| 86 | + } |
| 87 | + } |
| 88 | +} |
0 commit comments