Skip to content

Commit 7f2bd98

Browse files
committed
perf: skip task lookup in locked path when per-trigger ttl is provided
1 parent b06c42d commit 7f2bd98

File tree

1 file changed

+37
-21
lines changed

1 file changed

+37
-21
lines changed

apps/webapp/app/runEngine/concerns/queues.server.ts

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -80,27 +80,6 @@ export class DefaultQueueManager implements QueueManager {
8080
// Task is locked to a specific worker version
8181
const specifiedQueueName = extractQueueName(request.body.options?.queue);
8282

83-
// Always fetch the task to get TTL (and default queue if no override)
84-
const lockedTask = await this.prisma.backgroundWorkerTask.findFirst({
85-
where: {
86-
workerId: lockedBackgroundWorker.id,
87-
runtimeEnvironmentId: request.environment.id,
88-
slug: request.taskId,
89-
},
90-
include: {
91-
queue: true,
92-
},
93-
});
94-
95-
if (!lockedTask) {
96-
throw new ServiceValidationError(
97-
`Task '${request.taskId}' not found on locked version '${lockedBackgroundWorker.version ?? "<unknown>"
98-
}'.`
99-
);
100-
}
101-
102-
taskTtl = lockedTask.ttl;
103-
10483
if (specifiedQueueName) {
10584
// A specific queue name is provided, validate it exists for the locked worker
10685
const specifiedQueue = await this.prisma.taskQueue.findFirst({
@@ -117,10 +96,46 @@ export class DefaultQueueManager implements QueueManager {
11796
}'.`
11897
);
11998
}
99+
120100
// Use the validated queue name directly
121101
queueName = specifiedQueue.name;
122102
lockedQueueId = specifiedQueue.id;
103+
104+
// Only fetch task for TTL if caller didn't provide a per-trigger TTL
105+
if (request.body.options?.ttl === undefined) {
106+
const lockedTask = await this.prisma.backgroundWorkerTask.findFirst({
107+
where: {
108+
workerId: lockedBackgroundWorker.id,
109+
runtimeEnvironmentId: request.environment.id,
110+
slug: request.taskId,
111+
},
112+
select: { ttl: true },
113+
});
114+
115+
taskTtl = lockedTask?.ttl;
116+
}
123117
} else {
118+
// No queue override - fetch task with queue to get both default queue and TTL
119+
const lockedTask = await this.prisma.backgroundWorkerTask.findFirst({
120+
where: {
121+
workerId: lockedBackgroundWorker.id,
122+
runtimeEnvironmentId: request.environment.id,
123+
slug: request.taskId,
124+
},
125+
include: {
126+
queue: true,
127+
},
128+
});
129+
130+
if (!lockedTask) {
131+
throw new ServiceValidationError(
132+
`Task '${request.taskId}' not found on locked version '${lockedBackgroundWorker.version ?? "<unknown>"
133+
}'.`
134+
);
135+
}
136+
137+
taskTtl = lockedTask.ttl;
138+
124139
if (!lockedTask.queue) {
125140
// This case should ideally be prevented by earlier checks or schema constraints,
126141
// but handle it defensively.
@@ -134,6 +149,7 @@ export class DefaultQueueManager implements QueueManager {
134149
}'.`
135150
);
136151
}
152+
137153
// Use the task's default queue name
138154
queueName = lockedTask.queue.name;
139155
lockedQueueId = lockedTask.queue.id;

0 commit comments

Comments
 (0)