Skip to content

Commit 0719def

Browse files
committed
Clean up a few excessive ternaries
1 parent 92b6db3 commit 0719def

File tree

2 files changed

+102
-88
lines changed

2 files changed

+102
-88
lines changed

apps/webapp/app/routes/resources.taskruns.$runParam.replay.ts

Lines changed: 77 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { parse } from "@conform-to/zod";
22
import { type ActionFunction, json, type LoaderFunctionArgs } from "@remix-run/node";
3-
import { prettyPrintPacket } from "@trigger.dev/core/v3";
3+
import { type EnvironmentType, prettyPrintPacket } from "@trigger.dev/core/v3";
44
import { typedjson } from "remix-typedjson";
55
import { z } from "zod";
66
import { $replica, prisma } from "~/db.server";
@@ -99,54 +99,10 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
9999
throw new Response("Environment not found", { status: 404 });
100100
}
101101

102-
const task =
103-
environment.type !== "DEVELOPMENT"
104-
? (
105-
await findCurrentWorkerDeployment({
106-
environmentId: environment.id,
107-
})
108-
)?.worker?.tasks.find((t) => t.slug === run.taskIdentifier)
109-
: await $replica.backgroundWorkerTask.findFirst({
110-
select: {
111-
queueId: true,
112-
},
113-
where: {
114-
slug: run.taskIdentifier,
115-
runtimeEnvironmentId: environment.id,
116-
},
117-
orderBy: {
118-
createdAt: "desc",
119-
},
120-
});
121-
122-
const taskQueue = task?.queueId
123-
? await $replica.taskQueue.findFirst({
124-
where: {
125-
runtimeEnvironmentId: environment.id,
126-
id: task.queueId,
127-
},
128-
select: {
129-
friendlyId: true,
130-
name: true,
131-
type: true,
132-
paused: true,
133-
},
134-
})
135-
: undefined;
136-
137-
const backgroundWorkers = await $replica.backgroundWorker.findMany({
138-
where: {
139-
runtimeEnvironmentId: environment.id,
140-
},
141-
select: {
142-
version: true,
143-
engine: true,
144-
},
145-
orderBy: {
146-
createdAt: "desc",
147-
},
148-
take: 20, // last 20 versions should suffice
149-
});
102+
const [taskQueue, backgroundWorkers] = await Promise.all([
103+
findTaskQueue(environment, run.taskIdentifier),
104+
listLatestBackgroundWorkers(environment),
105+
]);
150106

151107
const latestVersions = backgroundWorkers.map((v) => v.version);
152108
const disableVersionSelection = environment.type === "DEVELOPMENT";
@@ -182,16 +138,13 @@ export async function loader({ request, params }: LoaderFunctionArgs) {
182138
branchName: environment.branchName ?? undefined,
183139
},
184140
environments: sortEnvironments(
185-
run.project.environments.map((environment) => {
186-
return {
187-
...displayableEnvironment(environment, userId),
188-
branchName: environment.branchName ?? undefined,
189-
};
190-
})
191-
).filter((env) => {
192-
if (env.type === "PREVIEW" && !env.branchName) return false;
193-
return true;
194-
}),
141+
run.project.environments
142+
.filter((env) => env.type !== "PREVIEW" || env.branchName)
143+
.map((env) => ({
144+
...displayableEnvironment(env, userId),
145+
branchName: env.branchName ?? undefined,
146+
}))
147+
),
195148
});
196149
}
197150

@@ -293,3 +246,68 @@ export const action: ActionFunction = async ({ request, params }) => {
293246
);
294247
}
295248
};
249+
250+
async function findTask(
251+
environment: { type: EnvironmentType; id: string },
252+
taskIdentifier: string
253+
) {
254+
if (environment.type === "DEVELOPMENT") {
255+
return $replica.backgroundWorkerTask.findFirst({
256+
select: {
257+
queueId: true,
258+
},
259+
where: {
260+
slug: taskIdentifier,
261+
runtimeEnvironmentId: environment.id,
262+
},
263+
orderBy: {
264+
createdAt: "desc",
265+
},
266+
});
267+
}
268+
269+
const currentDeployment = await findCurrentWorkerDeployment({
270+
environmentId: environment.id,
271+
});
272+
return currentDeployment?.worker?.tasks.find((t) => t.slug === taskIdentifier);
273+
}
274+
275+
async function findTaskQueue(
276+
environment: { type: EnvironmentType; id: string },
277+
taskIdentifier: string
278+
) {
279+
const task = await findTask(environment, taskIdentifier);
280+
281+
if (!task?.queueId) {
282+
return undefined;
283+
}
284+
285+
return $replica.taskQueue.findFirst({
286+
where: {
287+
runtimeEnvironmentId: environment.id,
288+
id: task.queueId,
289+
},
290+
select: {
291+
friendlyId: true,
292+
name: true,
293+
type: true,
294+
paused: true,
295+
},
296+
});
297+
}
298+
299+
function listLatestBackgroundWorkers(environment: { id: string }, limit = 20) {
300+
return $replica.backgroundWorker.findMany({
301+
where: {
302+
runtimeEnvironmentId: environment.id,
303+
},
304+
select: {
305+
version: true,
306+
engine: true,
307+
},
308+
orderBy: {
309+
createdAt: "desc",
310+
},
311+
take: limit,
312+
});
313+
}

apps/webapp/app/v3/services/replayTaskRun.server.ts

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,37 +35,11 @@ export class ReplayTaskRunService extends BaseService {
3535
taskRunFriendlyId: existingTaskRun.friendlyId,
3636
});
3737

38-
const getExistingPayload = async () => {
39-
const existingPayloadPacket = await conditionallyImportPacket({
40-
data: existingTaskRun.payload,
41-
dataType: existingTaskRun.payloadType,
42-
});
43-
44-
return existingPayloadPacket.dataType === "application/json"
45-
? await parsePacket(existingPayloadPacket)
46-
: existingPayloadPacket.data;
47-
};
48-
49-
const payload = overrideOptions.payload ?? (await getExistingPayload());
50-
const metadata =
51-
overrideOptions.metadata ??
52-
(existingTaskRun.seedMetadata
53-
? await parsePacket({
54-
data: existingTaskRun.seedMetadata,
55-
dataType: existingTaskRun.seedMetadataType,
56-
})
57-
: undefined);
38+
const payload = overrideOptions.payload ?? (await this.getExistingPayload(existingTaskRun));
39+
const metadata = overrideOptions.metadata ?? (await this.getExistingMetadata(existingTaskRun));
40+
const tags = overrideOptions.tags ?? existingTaskRun.runTags;
5841

5942
try {
60-
const tags =
61-
overrideOptions.tags ??
62-
(
63-
await getTagsForRunId({
64-
friendlyId: existingTaskRun.friendlyId,
65-
environmentId: authenticatedEnvironment.id,
66-
})
67-
)?.map((t) => t.name);
68-
6943
const taskQueue = await this._prisma.taskQueue.findFirst({
7044
where: {
7145
runtimeEnvironmentId: authenticatedEnvironment.id,
@@ -130,4 +104,26 @@ export class ReplayTaskRunService extends BaseService {
130104
return;
131105
}
132106
}
107+
108+
private async getExistingPayload(existingTaskRun: TaskRun) {
109+
const existingPayloadPacket = await conditionallyImportPacket({
110+
data: existingTaskRun.payload,
111+
dataType: existingTaskRun.payloadType,
112+
});
113+
114+
return existingPayloadPacket.dataType === "application/json"
115+
? await parsePacket(existingPayloadPacket)
116+
: existingPayloadPacket.data;
117+
}
118+
119+
private async getExistingMetadata(existingTaskRun: TaskRun) {
120+
if (!existingTaskRun.seedMetadata) {
121+
return undefined;
122+
}
123+
124+
return parsePacket({
125+
data: existingTaskRun.seedMetadata,
126+
dataType: existingTaskRun.seedMetadataType,
127+
});
128+
}
133129
}

0 commit comments

Comments
 (0)