Skip to content

Commit 162d864

Browse files
committed
Fixed prisma dataLoader slow query when getting an attempt payload
1 parent 8ba9987 commit 162d864

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

apps/webapp/app/v3/marqs/sharedQueueConsumer.server.ts

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,7 +1080,7 @@ class SharedQueueTasks {
10801080

10811081
const variables = await this.#buildEnvironmentVariables(
10821082
attempt.runtimeEnvironment,
1083-
taskRun,
1083+
taskRun.id,
10841084
machinePreset
10851085
);
10861086

@@ -1136,16 +1136,18 @@ class SharedQueueTasks {
11361136
return;
11371137
}
11381138

1139-
const run = await prisma.taskRun.findUnique({
1139+
const run = await prisma.taskRun.findFirst({
11401140
where: {
11411141
id: runId,
1142-
runtimeEnvironmentId: environment.id,
11431142
},
1144-
include: {
1145-
lockedBy: true,
1146-
_count: {
1143+
select: {
1144+
id: true,
1145+
traceContext: true,
1146+
friendlyId: true,
1147+
isTest: true,
1148+
lockedBy: {
11471149
select: {
1148-
attempts: true,
1150+
machineConfig: true,
11491151
},
11501152
},
11511153
},
@@ -1156,9 +1158,20 @@ class SharedQueueTasks {
11561158
return;
11571159
}
11581160

1161+
const attemptCount = await prisma.taskRunAttempt.count({
1162+
where: {
1163+
taskRunId: run.id,
1164+
},
1165+
});
1166+
1167+
logger.debug("Getting lazy attempt payload for run", {
1168+
run,
1169+
attemptCount,
1170+
});
1171+
11591172
const machinePreset = machinePresetFromConfig(run.lockedBy?.machineConfig ?? {});
11601173

1161-
const variables = await this.#buildEnvironmentVariables(environment, run, machinePreset);
1174+
const variables = await this.#buildEnvironmentVariables(environment, run.id, machinePreset);
11621175

11631176
return {
11641177
traceContext: run.traceContext as Record<string, unknown>,
@@ -1169,7 +1182,7 @@ class SharedQueueTasks {
11691182
runId: run.friendlyId,
11701183
messageId: run.id,
11711184
isTest: run.isTest,
1172-
attemptCount: run._count.attempts,
1185+
attemptCount,
11731186
} satisfies TaskRunExecutionLazyAttemptPayload;
11741187
}
11751188

@@ -1203,21 +1216,21 @@ class SharedQueueTasks {
12031216

12041217
async #buildEnvironmentVariables(
12051218
environment: RuntimeEnvironment,
1206-
run: TaskRun,
1219+
runId: string,
12071220
machinePreset: MachinePreset
12081221
): Promise<Array<EnvironmentVariable>> {
12091222
const variables = await resolveVariablesForEnvironment(environment);
12101223

12111224
const jwt = await generateJWTTokenForEnvironment(environment, {
1212-
run_id: run.id,
1225+
run_id: runId,
12131226
machine_preset: machinePreset.name,
12141227
});
12151228

12161229
return [
12171230
...variables,
12181231
...[
12191232
{ key: "TRIGGER_JWT", value: jwt },
1220-
{ key: "TRIGGER_RUN_ID", value: run.id },
1233+
{ key: "TRIGGER_RUN_ID", value: runId },
12211234
{
12221235
key: "TRIGGER_MACHINE_PRESET",
12231236
value: machinePreset.name,

0 commit comments

Comments
 (0)