|
| 1 | +import type { ActionArgs } from "@remix-run/server-runtime"; |
| 2 | +import { json } from "@remix-run/server-runtime"; |
| 3 | +import { TaskStatus } from "@trigger.dev/database"; |
| 4 | +import { |
| 5 | + RunTaskBodyOutput, |
| 6 | + RunTaskBodyOutputSchema, |
| 7 | + ServerTask, |
| 8 | + StatusHistory, |
| 9 | + StatusHistorySchema, |
| 10 | + StatusUpdate, |
| 11 | + StatusUpdateData, |
| 12 | + StatusUpdateSchema, |
| 13 | + StatusUpdateState, |
| 14 | +} from "@trigger.dev/core"; |
| 15 | +import { z } from "zod"; |
| 16 | +import { $transaction, PrismaClient, prisma } from "~/db.server"; |
| 17 | +import { taskWithAttemptsToServerTask } from "~/models/task.server"; |
| 18 | +import { authenticateApiRequest } from "~/services/apiAuth.server"; |
| 19 | +import { logger } from "~/services/logger.server"; |
| 20 | +import { ulid } from "~/services/ulid.server"; |
| 21 | +import { workerQueue } from "~/services/worker.server"; |
| 22 | +import { JobRunStatusRecordSchema } from "@trigger.dev/core"; |
| 23 | + |
| 24 | +const ParamsSchema = z.object({ |
| 25 | + runId: z.string(), |
| 26 | + id: z.string(), |
| 27 | +}); |
| 28 | + |
| 29 | +export async function action({ request, params }: ActionArgs) { |
| 30 | + // Ensure this is a POST request |
| 31 | + if (request.method.toUpperCase() !== "PUT") { |
| 32 | + return { status: 405, body: "Method Not Allowed" }; |
| 33 | + } |
| 34 | + |
| 35 | + // Next authenticate the request |
| 36 | + const authenticationResult = await authenticateApiRequest(request); |
| 37 | + |
| 38 | + if (!authenticationResult) { |
| 39 | + return json({ error: "Invalid or Missing API key" }, { status: 401 }); |
| 40 | + } |
| 41 | + |
| 42 | + const { runId, id } = ParamsSchema.parse(params); |
| 43 | + |
| 44 | + // Now parse the request body |
| 45 | + const anyBody = await request.json(); |
| 46 | + |
| 47 | + logger.debug("SetStatusService.call() request body", { |
| 48 | + body: anyBody, |
| 49 | + runId, |
| 50 | + id, |
| 51 | + }); |
| 52 | + |
| 53 | + const body = StatusUpdateSchema.safeParse(anyBody); |
| 54 | + |
| 55 | + if (!body.success) { |
| 56 | + return json({ error: "Invalid request body" }, { status: 400 }); |
| 57 | + } |
| 58 | + |
| 59 | + const service = new SetStatusService(); |
| 60 | + |
| 61 | + try { |
| 62 | + const statusRecord = await service.call(runId, id, body.data); |
| 63 | + |
| 64 | + logger.debug("SetStatusService.call() response body", { |
| 65 | + runId, |
| 66 | + id, |
| 67 | + statusRecord, |
| 68 | + }); |
| 69 | + |
| 70 | + if (!statusRecord) { |
| 71 | + return json({ error: "Something went wrong" }, { status: 500 }); |
| 72 | + } |
| 73 | + |
| 74 | + const status = JobRunStatusRecordSchema.parse({ |
| 75 | + ...statusRecord, |
| 76 | + state: statusRecord.state ?? undefined, |
| 77 | + history: statusRecord.history ?? undefined, |
| 78 | + data: statusRecord.data ?? undefined, |
| 79 | + }); |
| 80 | + |
| 81 | + return json(status); |
| 82 | + } catch (error) { |
| 83 | + if (error instanceof Error) { |
| 84 | + return json({ error: error.message }, { status: 400 }); |
| 85 | + } |
| 86 | + |
| 87 | + return json({ error: "Something went wrong" }, { status: 500 }); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +export class SetStatusService { |
| 92 | + #prismaClient: PrismaClient; |
| 93 | + |
| 94 | + constructor(prismaClient: PrismaClient = prisma) { |
| 95 | + this.#prismaClient = prismaClient; |
| 96 | + } |
| 97 | + |
| 98 | + public async call(runId: string, id: string, status: StatusUpdate) { |
| 99 | + const statusRecord = await $transaction(this.#prismaClient, async (tx) => { |
| 100 | + const existingStatus = await tx.jobRunStatusRecord.findUnique({ |
| 101 | + where: { |
| 102 | + runId_key: { |
| 103 | + runId, |
| 104 | + key: id, |
| 105 | + }, |
| 106 | + }, |
| 107 | + }); |
| 108 | + |
| 109 | + const history: StatusHistory = []; |
| 110 | + const historyResult = StatusHistorySchema.safeParse(existingStatus?.history); |
| 111 | + if (historyResult.success) { |
| 112 | + history.push(...historyResult.data); |
| 113 | + } |
| 114 | + if (existingStatus) { |
| 115 | + history.push({ |
| 116 | + label: existingStatus.label, |
| 117 | + state: (existingStatus.state ?? undefined) as StatusUpdateState, |
| 118 | + data: (existingStatus.data ?? undefined) as StatusUpdateData, |
| 119 | + }); |
| 120 | + } |
| 121 | + |
| 122 | + const updatedStatus = await tx.jobRunStatusRecord.upsert({ |
| 123 | + where: { |
| 124 | + runId_key: { |
| 125 | + runId, |
| 126 | + key: id, |
| 127 | + }, |
| 128 | + }, |
| 129 | + create: { |
| 130 | + key: id, |
| 131 | + runId, |
| 132 | + //this shouldn't ever use the id in reality, as the SDK makess it compulsory on the first call |
| 133 | + label: status.label ?? id, |
| 134 | + state: status.state, |
| 135 | + data: status.data as any, |
| 136 | + history: [], |
| 137 | + }, |
| 138 | + update: { |
| 139 | + label: status.label, |
| 140 | + state: status.state, |
| 141 | + data: status.data as any, |
| 142 | + history: history as any[], |
| 143 | + }, |
| 144 | + }); |
| 145 | + |
| 146 | + return updatedStatus; |
| 147 | + }); |
| 148 | + |
| 149 | + return statusRecord; |
| 150 | + } |
| 151 | +} |
0 commit comments