Skip to content

Commit 3c67415

Browse files
perf: eliminate getLatestExecutionSnapshot read in dequeue flow
Pre-generate the snapshot ID with SnapshotId.generate() and construct the event/return data from values already available in scope. This removes the extra DB read that was added in the initial merge commit. Co-Authored-By: Eric Allam <eallam@icloud.com>
1 parent 1ef59a7 commit 3c67415

File tree

1 file changed

+32
-14
lines changed

1 file changed

+32
-14
lines changed

internal-packages/run-engine/src/engine/systems/dequeueSystem.ts

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { startSpan } from "@internal/tracing";
33
import { assertExhaustive, tryCatch } from "@trigger.dev/core";
44
import { DequeuedMessage, RetryOptions, RunAnnotations } from "@trigger.dev/core/v3";
55
import { placementTag } from "@trigger.dev/core/v3/serverOnly";
6-
import { getMaxDuration } from "@trigger.dev/core/v3/isomorphic";
6+
import { getMaxDuration, SnapshotId } from "@trigger.dev/core/v3/isomorphic";
77
import {
88
BackgroundWorker,
99
BackgroundWorkerTask,
@@ -416,6 +416,9 @@ export class DequeueSystem {
416416
? undefined
417417
: result.task.retryConfig;
418418

419+
// Pre-generate snapshot ID so we can construct the result without an extra read
420+
const snapshotIds = SnapshotId.generate();
421+
419422
const lockedTaskRun = await prisma.taskRun.update({
420423
where: {
421424
id: runId,
@@ -437,6 +440,7 @@ export class DequeueSystem {
437440
maxAttempts: maxAttempts ?? undefined,
438441
executionSnapshots: {
439442
create: {
443+
id: snapshotIds.id,
440444
engine: "V2",
441445
executionStatus: "PENDING_EXECUTING",
442446
description: "Run was dequeued for execution",
@@ -543,35 +547,49 @@ export class DequeueSystem {
543547
}
544548

545549
// Snapshot was created as part of the taskRun.update above (single transaction).
546-
// Fetch the enhanced snapshot and handle side effects (heartbeat + event) manually.
547-
const newSnapshot = await getLatestExecutionSnapshot(prisma, runId);
550+
// Construct the snapshot info from data we already have and handle side effects
551+
// (heartbeat + event) manually — no extra DB read needed.
552+
const snapshotCreatedAt = new Date();
548553

549554
this.$.eventBus.emit("executionSnapshotCreated", {
550-
time: newSnapshot.createdAt,
555+
time: snapshotCreatedAt,
551556
run: {
552-
id: newSnapshot.runId,
557+
id: runId,
553558
},
554559
snapshot: {
555-
...newSnapshot,
556-
completedWaitpointIds: newSnapshot.completedWaitpoints.map((wp) => wp.id),
560+
id: snapshotIds.id,
561+
executionStatus: "PENDING_EXECUTING",
562+
description: "Run was dequeued for execution",
563+
runStatus: "PENDING",
564+
attemptNumber: result.run.attemptNumber ?? null,
565+
checkpointId: snapshot.checkpointId ?? null,
566+
workerId: workerId ?? null,
567+
runnerId: runnerId ?? null,
568+
isValid: true,
569+
error: null,
570+
completedWaitpointIds: snapshot.completedWaitpoints.map((wp) => wp.id),
557571
},
558572
});
559573

560-
await this.executionSnapshotSystem.enqueueHeartbeatIfNeeded(newSnapshot);
574+
await this.executionSnapshotSystem.enqueueHeartbeatIfNeeded({
575+
id: snapshotIds.id,
576+
runId,
577+
executionStatus: "PENDING_EXECUTING",
578+
});
561579

562580
return {
563581
version: "1" as const,
564582
dequeuedAt: new Date(),
565583
workerQueueLength: message.workerQueueLength,
566584
snapshot: {
567-
id: newSnapshot.id,
568-
friendlyId: newSnapshot.friendlyId,
569-
executionStatus: newSnapshot.executionStatus,
570-
description: newSnapshot.description,
571-
createdAt: newSnapshot.createdAt,
585+
id: snapshotIds.id,
586+
friendlyId: snapshotIds.friendlyId,
587+
executionStatus: "PENDING_EXECUTING" as const,
588+
description: "Run was dequeued for execution",
589+
createdAt: snapshotCreatedAt,
572590
},
573591
image: result.deployment?.imageReference ?? undefined,
574-
checkpoint: newSnapshot.checkpoint ?? undefined,
592+
checkpoint: snapshot.checkpoint ?? undefined,
575593
completedWaitpoints: snapshot.completedWaitpoints,
576594
backgroundWorker: {
577595
id: result.worker.id,

0 commit comments

Comments
 (0)