Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 44 additions & 17 deletions internal-packages/run-engine/src/engine/systems/waitpointSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,25 +226,52 @@ export class WaitpointSystem {
}
}

const waitpoint = await prisma.waitpoint.upsert({
where: {
environmentId_idempotencyKey: {
environmentId,
let waitpoint: Waitpoint;
try {
waitpoint = await prisma.waitpoint.upsert({
where: {
environmentId_idempotencyKey: {
environmentId,
idempotencyKey: idempotencyKey ?? nanoid(24),
},
},
create: {
...WaitpointId.generate(),
type: "DATETIME",
idempotencyKey: idempotencyKey ?? nanoid(24),
idempotencyKeyExpiresAt,
userProvidedIdempotencyKey: !!idempotencyKey,
environmentId,
projectId,
completedAfter,
},
},
create: {
...WaitpointId.generate(),
type: "DATETIME",
idempotencyKey: idempotencyKey ?? nanoid(24),
idempotencyKeyExpiresAt,
userProvidedIdempotencyKey: !!idempotencyKey,
environmentId,
projectId,
completedAfter,
},
update: {},
});
update: {},
});
} catch (error) {
// A concurrent request with the same user-provided idempotencyKey can win the
// race between our findFirst above and this upsert, causing a P2002 on
// @@unique([environmentId, idempotencyKey]). The winner already created the row
// and enqueued the finishWaitpoint job, so return the existing row as cached
// instead of retrying (a plain retry would re-collide on the user-provided key).
if (
idempotencyKey &&
error instanceof Prisma.PrismaClientKnownRequestError &&
error.code === "P2002"
) {
const existing = await prisma.waitpoint.findFirst({
where: {
environmentId,
idempotencyKey,
},
});

if (existing) {
return { waitpoint: existing, isCached: true };
}
}

throw error;
}

await this.$.worker.enqueue({
id: `finishWaitpoint.${waitpoint.id}`,
Expand Down