|
| 1 | +import { type ActionFunctionArgs, json } from "@remix-run/server-runtime"; |
| 2 | +import { type TaskRun } from "@trigger.dev/database"; |
| 3 | +import { z } from "zod"; |
| 4 | +import { prisma } from "~/db.server"; |
| 5 | +import { logger } from "~/services/logger.server"; |
| 6 | +import { authenticateApiRequestWithPersonalAccessToken } from "~/services/personalAccessToken.server"; |
| 7 | +import { runsReplicationInstance } from "~/services/runsReplicationInstance.server"; |
| 8 | +import { FINAL_RUN_STATUSES } from "~/v3/taskStatus"; |
| 9 | + |
| 10 | +const Body = z.object({ |
| 11 | + runIds: z.array(z.string()), |
| 12 | +}); |
| 13 | + |
| 14 | +const MAX_BATCH_SIZE = 50; |
| 15 | + |
| 16 | +export async function action({ request }: ActionFunctionArgs) { |
| 17 | + // Next authenticate the request |
| 18 | + const authenticationResult = await authenticateApiRequestWithPersonalAccessToken(request); |
| 19 | + |
| 20 | + if (!authenticationResult) { |
| 21 | + return json({ error: "Invalid or Missing API key" }, { status: 401 }); |
| 22 | + } |
| 23 | + |
| 24 | + const user = await prisma.user.findUnique({ |
| 25 | + where: { |
| 26 | + id: authenticationResult.userId, |
| 27 | + }, |
| 28 | + }); |
| 29 | + |
| 30 | + if (!user) { |
| 31 | + return json({ error: "Invalid or Missing API key" }, { status: 401 }); |
| 32 | + } |
| 33 | + |
| 34 | + if (!user.admin) { |
| 35 | + return json({ error: "You must be an admin to perform this action" }, { status: 403 }); |
| 36 | + } |
| 37 | + |
| 38 | + try { |
| 39 | + const body = await request.json(); |
| 40 | + const { runIds } = Body.parse(body); |
| 41 | + |
| 42 | + logger.info("Backfilling runs", { runIds }); |
| 43 | + |
| 44 | + const runs: TaskRun[] = []; |
| 45 | + for (let i = 0; i < runIds.length; i += MAX_BATCH_SIZE) { |
| 46 | + const batch = runIds.slice(i, i + MAX_BATCH_SIZE); |
| 47 | + const batchRuns = await prisma.taskRun.findMany({ |
| 48 | + where: { |
| 49 | + id: { in: batch }, |
| 50 | + status: { |
| 51 | + in: FINAL_RUN_STATUSES, |
| 52 | + }, |
| 53 | + }, |
| 54 | + }); |
| 55 | + runs.push(...batchRuns); |
| 56 | + } |
| 57 | + |
| 58 | + if (!runsReplicationInstance) { |
| 59 | + throw new Error("Runs replication instance not found"); |
| 60 | + } |
| 61 | + |
| 62 | + await runsReplicationInstance.backfill(runs); |
| 63 | + |
| 64 | + logger.info("Backfilled runs", { runs }); |
| 65 | + |
| 66 | + return json({ |
| 67 | + success: true, |
| 68 | + runCount: runs.length, |
| 69 | + }); |
| 70 | + } catch (error) { |
| 71 | + return json({ error: error instanceof Error ? error.message : error }, { status: 400 }); |
| 72 | + } |
| 73 | +} |
0 commit comments