Skip to content

Commit 5683952

Browse files
committed
refactor(webapp): route presenter TaskRun reads through the run store
Relocate the dashboard presenter TaskRun reads to the RunStore read methods, preserving the exact client per site. Behavior-preserving.
1 parent 5b74b48 commit 5683952

9 files changed

Lines changed: 213 additions & 170 deletions

apps/webapp/app/presenters/v3/ApiRetrieveRunPresenter.server.ts

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import {
2222
type SyntheticRun,
2323
} from "~/v3/mollifier/readFallback.server";
2424
import { generatePresignedUrl } from "~/v3/objectStore.server";
25+
import { runStore } from "~/v3/runStore.server";
2526
import { tracer } from "~/v3/tracer.server";
2627
import { startSpanWithEnv } from "~/v3/tracing.server";
2728

@@ -110,38 +111,41 @@ export class ApiRetrieveRunPresenter {
110111
friendlyId: string,
111112
env: AuthenticatedEnvironment,
112113
): Promise<FoundRun | null> {
113-
const pgRow = await $replica.taskRun.findFirst({
114-
where: {
114+
const pgRow = await runStore.findRun(
115+
{
115116
friendlyId,
116117
runtimeEnvironmentId: env.id,
117118
},
118-
select: {
119-
...commonRunSelect,
120-
traceId: true,
121-
payload: true,
122-
payloadType: true,
123-
output: true,
124-
outputType: true,
125-
error: true,
126-
attempts: {
127-
select: {
128-
id: true,
119+
{
120+
select: {
121+
...commonRunSelect,
122+
traceId: true,
123+
payload: true,
124+
payloadType: true,
125+
output: true,
126+
outputType: true,
127+
error: true,
128+
attempts: {
129+
select: {
130+
id: true,
131+
},
132+
},
133+
attemptNumber: true,
134+
engine: true,
135+
taskEventStore: true,
136+
parentTaskRun: {
137+
select: commonRunSelect,
138+
},
139+
rootTaskRun: {
140+
select: commonRunSelect,
141+
},
142+
childRuns: {
143+
select: commonRunSelect,
129144
},
130-
},
131-
attemptNumber: true,
132-
engine: true,
133-
taskEventStore: true,
134-
parentTaskRun: {
135-
select: commonRunSelect,
136-
},
137-
rootTaskRun: {
138-
select: commonRunSelect,
139-
},
140-
childRuns: {
141-
select: commonRunSelect,
142145
},
143146
},
144-
});
147+
$replica
148+
);
145149

146150
if (pgRow) return { ...pgRow, isBuffered: false };
147151

apps/webapp/app/presenters/v3/ApiRunResultPresenter.server.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { TaskRunExecutionResult } from "@trigger.dev/core/v3";
22
import { executionResultForTaskRun } from "~/models/taskRun.server";
33
import { AuthenticatedEnvironment } from "~/services/apiAuth.server";
4+
import { runStore } from "~/v3/runStore.server";
45
import { BasePresenter } from "./basePresenter.server";
56

67
export class ApiRunResultPresenter extends BasePresenter {
@@ -9,19 +10,22 @@ export class ApiRunResultPresenter extends BasePresenter {
910
env: AuthenticatedEnvironment
1011
): Promise<TaskRunExecutionResult | undefined> {
1112
return this.traceWithEnv("call", env, async (span) => {
12-
const taskRun = await this._prisma.taskRun.findFirst({
13-
where: {
13+
const taskRun = await runStore.findRun(
14+
{
1415
friendlyId,
1516
runtimeEnvironmentId: env.id,
1617
},
17-
include: {
18-
attempts: {
19-
orderBy: {
20-
createdAt: "desc",
18+
{
19+
include: {
20+
attempts: {
21+
orderBy: {
22+
createdAt: "desc",
23+
},
2124
},
2225
},
2326
},
24-
});
27+
this._prisma
28+
);
2529

2630
if (!taskRun) {
2731
return undefined;

apps/webapp/app/presenters/v3/NextRunListPresenter.server.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { getTaskIdentifiers } from "~/models/task.server";
1313
import { RunsRepository } from "~/services/runsRepository/runsRepository.server";
1414
import { regionForDisplay } from "~/runEngine/concerns/workerQueueSplit.server";
1515
import { machinePresetFromRun } from "~/v3/machinePresets.server";
16+
import { runStore } from "~/v3/runStore.server";
1617
import { ServiceValidationError } from "~/v3/services/baseService.server";
1718
import { isCancellableRunStatus, isFinalRunStatus, isPendingRunStatus } from "~/v3/taskStatus";
1819

@@ -206,11 +207,12 @@ export class NextRunListPresenter {
206207
let hasAnyRuns = runs.length > 0;
207208

208209
if (!hasAnyRuns) {
209-
const firstRun = await this.replica.taskRun.findFirst({
210-
where: {
210+
const firstRun = await runStore.findRun(
211+
{
211212
runtimeEnvironmentId: environmentId,
212213
},
213-
});
214+
this.replica
215+
);
214216

215217
if (firstRun) {
216218
hasAnyRuns = true;

apps/webapp/app/presenters/v3/RunPresenter.server.ts

Lines changed: 56 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { getTaskEventStoreTableForRun } from "~/v3/taskEventStore.server";
88
import { isFinalRunStatus } from "~/v3/taskStatus";
99
import { env } from "~/env.server";
1010
import { getEventRepositoryForStore } from "~/v3/eventRepository/index.server";
11+
import { runStore } from "~/v3/runStore.server";
1112

1213
type Result = Awaited<ReturnType<RunPresenter["call"]>>;
1314
export type Run = Result["run"];
@@ -62,57 +63,8 @@ export class RunPresenter {
6263
// buffer view. `findFirstOrThrow` would log a `PrismaClient error`
6364
// every tick of the page poll, masking real DB issues with synthetic
6465
// not-found noise.
65-
const run = await this.#prismaClient.taskRun.findFirst({
66-
select: {
67-
id: true,
68-
createdAt: true,
69-
taskEventStore: true,
70-
taskIdentifier: true,
71-
number: true,
72-
traceId: true,
73-
spanId: true,
74-
parentSpanId: true,
75-
friendlyId: true,
76-
status: true,
77-
startedAt: true,
78-
completedAt: true,
79-
logsDeletedAt: true,
80-
annotations: true,
81-
rootTaskRun: {
82-
select: {
83-
friendlyId: true,
84-
spanId: true,
85-
createdAt: true,
86-
},
87-
},
88-
parentTaskRun: {
89-
select: {
90-
friendlyId: true,
91-
spanId: true,
92-
createdAt: true,
93-
},
94-
},
95-
runtimeEnvironment: {
96-
select: {
97-
id: true,
98-
type: true,
99-
slug: true,
100-
organizationId: true,
101-
orgMember: {
102-
select: {
103-
user: {
104-
select: {
105-
id: true,
106-
name: true,
107-
displayName: true,
108-
},
109-
},
110-
},
111-
},
112-
},
113-
},
114-
},
115-
where: {
66+
const run = await runStore.findRun(
67+
{
11668
friendlyId: runFriendlyId,
11769
project: {
11870
slug: projectSlug,
@@ -125,7 +77,59 @@ export class RunPresenter {
12577
},
12678
},
12779
},
128-
});
80+
{
81+
select: {
82+
id: true,
83+
createdAt: true,
84+
taskEventStore: true,
85+
taskIdentifier: true,
86+
number: true,
87+
traceId: true,
88+
spanId: true,
89+
parentSpanId: true,
90+
friendlyId: true,
91+
status: true,
92+
startedAt: true,
93+
completedAt: true,
94+
logsDeletedAt: true,
95+
annotations: true,
96+
rootTaskRun: {
97+
select: {
98+
friendlyId: true,
99+
spanId: true,
100+
createdAt: true,
101+
},
102+
},
103+
parentTaskRun: {
104+
select: {
105+
friendlyId: true,
106+
spanId: true,
107+
createdAt: true,
108+
},
109+
},
110+
runtimeEnvironment: {
111+
select: {
112+
id: true,
113+
type: true,
114+
slug: true,
115+
organizationId: true,
116+
orgMember: {
117+
select: {
118+
user: {
119+
select: {
120+
id: true,
121+
name: true,
122+
displayName: true,
123+
},
124+
},
125+
},
126+
},
127+
},
128+
},
129+
},
130+
},
131+
this.#prismaClient
132+
);
129133

130134
if (!run) {
131135
throw new RunNotInPgError(runFriendlyId);

apps/webapp/app/presenters/v3/RunStreamPresenter.server.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { ABORT_REASON_SEND_ERROR, createSSELoader, SendFunction } from "~/utils/
66
import { throttle } from "~/utils/throttle";
77
import { getMollifierBuffer } from "~/v3/mollifier/mollifierBuffer.server";
88
import { deserialiseMollifierSnapshot } from "~/v3/mollifier/mollifierSnapshot.server";
9+
import { runStore } from "~/v3/runStore.server";
910
import { tracePubSub } from "~/v3/services/tracePubSub.server";
1011

1112
const PING_INTERVAL = 5_000;
@@ -36,8 +37,8 @@ export class RunStreamPresenter {
3637
// Scope the lookup to organizations the requesting user is a member
3738
// of, matching RunPresenter's run lookup. Unauthorized and missing
3839
// runs are indistinguishable (both 404).
39-
const run = await prismaClient.taskRun.findFirst({
40-
where: {
40+
const run = await runStore.findRun(
41+
{
4142
friendlyId: runFriendlyId,
4243
project: {
4344
organization: {
@@ -49,10 +50,13 @@ export class RunStreamPresenter {
4950
},
5051
},
5152
},
52-
select: {
53-
traceId: true,
53+
{
54+
select: {
55+
traceId: true,
56+
},
5457
},
55-
});
58+
prismaClient
59+
);
5660

5761
// Fall back to the mollifier buffer when the run isn't in PG yet.
5862
// The buffered run has no execution events to stream, but we still

apps/webapp/app/presenters/v3/SessionListPresenter.server.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import {
1010
} from "~/services/sessionsRepository/sessionsRepository.server";
1111
import { ServiceValidationError } from "~/v3/services/baseService.server";
1212
import { findCurrentWorkerFromEnvironment } from "~/v3/models/workerDeployment.server";
13+
import { runStore } from "~/v3/runStore.server";
1314
import { startActiveSpan } from "~/v3/tracer.server";
1415

1516
export type SessionListOptions = {
@@ -189,14 +190,17 @@ export class SessionListPresenter {
189190
// pointer could surface another tenant's run. The list query above
190191
// is already env-scoped; the run lookup needs the same fence.
191192
return currentRunIds.length > 0
192-
? this.replica.taskRun.findMany({
193-
where: {
194-
id: { in: currentRunIds },
195-
projectId,
196-
runtimeEnvironmentId: environmentId,
193+
? runStore.findRuns(
194+
{
195+
where: {
196+
id: { in: currentRunIds },
197+
projectId,
198+
runtimeEnvironmentId: environmentId,
199+
},
200+
select: { id: true, friendlyId: true },
197201
},
198-
select: { id: true, friendlyId: true },
199-
})
202+
this.replica
203+
)
200204
: [];
201205
}
202206
);

apps/webapp/app/presenters/v3/SessionPresenter.server.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { chatSnapshotStorageKey } from "~/services/realtime/chatSnapshot.server"
66
import { resolveSessionByIdOrExternalId } from "~/services/realtime/sessions.server";
77
import { logger } from "~/services/logger.server";
88
import { generatePresignedUrl } from "~/v3/objectStore.server";
9+
import { runStore } from "~/v3/runStore.server";
910
import { ServiceValidationError } from "~/v3/services/baseService.server";
1011
import { startActiveSpan } from "~/v3/tracer.server";
1112

@@ -96,10 +97,13 @@ export class SessionPresenter {
9697
async (span) => {
9798
span.setAttribute("runIds.count", runIds.length);
9899
return runIds.length > 0
99-
? this.replica.taskRun.findMany({
100-
where: { id: { in: runIds } },
101-
select: { id: true, friendlyId: true, status: true },
102-
})
100+
? runStore.findRuns(
101+
{
102+
where: { id: { in: runIds } },
103+
select: { id: true, friendlyId: true, status: true },
104+
},
105+
this.replica
106+
)
103107
: [];
104108
}
105109
);
@@ -110,10 +114,13 @@ export class SessionPresenter {
110114
(await startActiveSpan(
111115
"SessionPresenter.findCurrentRunFallback",
112116
() =>
113-
this.replica.taskRun.findFirst({
114-
where: { id: session.currentRunId! },
115-
select: { id: true, friendlyId: true, status: true },
116-
})
117+
runStore.findRun(
118+
{ id: session.currentRunId! },
119+
{
120+
select: { id: true, friendlyId: true, status: true },
121+
},
122+
this.replica
123+
)
117124
))
118125
: null;
119126

0 commit comments

Comments
 (0)