Files
triggerdotdev--trigger.dev/apps/webapp/app/routes/@.runs.$runParam.ts
Daniel Sutton 65c545da4e refactor(run-store,webapp,run-engine): route Postgres TaskRun reads through the run store (#3990)
## Summary

Adds read methods to `RunStore` (`findRun`, `findRunOrThrow`,
`findRuns`) and routes every Postgres read of `TaskRun` through them,
mirroring how writes already go through the store. Behavior-preserving:
each relocated read keeps its exact query, field selection, and database
client (writer, replica, or transaction). This lets `TaskRun` reads be
retargeted to a different backing store later without touching call
sites.

Stacked on #3981 (the write adapter); that PR is the base of this one.

## Scope

In scope: the run engine, webapp services, presenters, and route
loaders. Three reads that pulled `TaskRun` in through a parent model's
relation `include` (alert delivery, batch results, attempt-dependency
cancellation) are decomposed to fetch the run(s) through the store and
stitch them back, since a relation include would not follow `TaskRun` to
a new table.

Left reading the existing table (out of scope): the legacy MarQS paths,
the legacy trigger idempotency read, and one raw-SQL recovery script
(commented for revisiting at cutover).

## Notes

Reads default to the read replica; callers pass the writer or a
transaction client wherever the original read did, so writer-vs-replica
behavior is unchanged.
2026-06-22 10:02:57 +01:00

103 lines
2.9 KiB
TypeScript

import { redirect, type LoaderFunctionArgs } from "@remix-run/server-runtime";
import { z } from "zod";
import { prisma } from "~/db.server";
import { runStore } from "~/v3/runStore.server";
import { redirectWithErrorMessage } from "~/models/message.server";
import { requireUser } from "~/services/session.server";
import { impersonate, rootPath, v3RunPath, v3RunSpanPath } from "~/utils/pathBuilder";
import { findBufferedRunRedirectInfo } from "~/v3/mollifier/syntheticRedirectInfo.server";
const ParamsSchema = z.object({
runParam: z.string(),
});
export async function loader({ params, request }: LoaderFunctionArgs) {
const user = await requireUser(request);
const { runParam } = ParamsSchema.parse(params);
const isAdmin = user.admin || user.isImpersonating;
if (!isAdmin) {
return redirectWithErrorMessage(
rootPath(),
request,
"You're not an admin and cannot impersonate",
{
ephemeral: false,
}
);
}
const run = await runStore.findRun(
{
friendlyId: runParam,
},
{
select: {
spanId: true,
runtimeEnvironment: {
select: {
slug: true,
},
},
project: {
select: {
slug: true,
organization: {
select: {
slug: true,
},
},
},
},
},
},
prisma
);
if (!run) {
// Admin impersonation route — bypass org membership so admins can
// open any buffered run by friendlyId, mirroring the existing PG
// behaviour above (no membership filter on the find).
const buffered = await findBufferedRunRedirectInfo({
runFriendlyId: runParam,
userId: user.id,
skipOrgMembershipCheck: true,
});
if (buffered) {
// Preselect the root span so the run-detail trace tree opens with
// the buffered run's span highlighted, matching the sibling
// redirect routes (runs.$runParam.ts, projects.v3.$projectRef…).
const path = buffered.spanId
? v3RunSpanPath(
{ slug: buffered.organizationSlug },
{ slug: buffered.projectSlug },
{ slug: buffered.environmentSlug },
{ friendlyId: runParam },
{ spanId: buffered.spanId }
)
: v3RunPath(
{ slug: buffered.organizationSlug },
{ slug: buffered.projectSlug },
{ slug: buffered.environmentSlug },
{ friendlyId: runParam }
);
return redirect(impersonate(path));
}
return redirectWithErrorMessage(rootPath(), request, "Run doesn't exist", {
ephemeral: false,
});
}
const path = v3RunSpanPath(
{ slug: run.project.organization.slug },
{ slug: run.project.slug },
{ slug: run.runtimeEnvironment.slug },
{ friendlyId: runParam },
{ spanId: run.spanId }
);
return redirect(impersonate(path));
}