import { Prisma } from '../generated/prisma/client'; export const OBJECT_TRANSACTION = { maxWait: 5000, timeout: 20000 }; /** All writers and cleaners use the same transaction-scoped lock for an immutable key. */ export async function tryAttachmentObjectLock( tx: Prisma.TransactionClient, objectKey: string, ): Promise { // A suspended/crashed writer must not keep an idle transaction open indefinitely. await tx.$executeRaw`SET LOCAL idle_in_transaction_session_timeout = '15s'`; const [result] = await tx.$queryRaw<{ locked: boolean }[]>(Prisma.sql` SELECT pg_try_advisory_xact_lock(hashtextextended(${`attachment:${objectKey}`}, 0)) AS locked `); return result.locked; }