* Conditional terminal update scoped to the lease acquired at claim * time. Returns true if the UPDATE affected a row, false if the row's * lease was revoked (reaped, reclaimed by another worker). Callers * treat `false` as a "lease lost" signal and skip without retrying — * the newer owner is r
(
event: typeof outboxEvent.$inferSelect,
patch: {
status: 'completed' | 'pending' | 'dead_letter'
attempts?: number
lastError?: string | null
availableAt?: Date
lockedAt: Date | null
processedAt?: Date | null
}
)
| 555 | * the newer owner is responsible for the row now. |
| 556 | */ |
| 557 | async function updateIfLeaseHeld( |
| 558 | event: typeof outboxEvent.$inferSelect, |
| 559 | patch: { |
| 560 | status: 'completed' | 'pending' | 'dead_letter' |
| 561 | attempts?: number |
| 562 | lastError?: string | null |
| 563 | availableAt?: Date |
| 564 | lockedAt: Date | null |
| 565 | processedAt?: Date | null |
| 566 | } |
| 567 | ): Promise<boolean> { |
| 568 | const whereClauses = [eq(outboxEvent.id, event.id), eq(outboxEvent.status, 'processing')] |
| 569 | if (event.lockedAt) { |
| 570 | whereClauses.push(eq(outboxEvent.lockedAt, event.lockedAt)) |
| 571 | } |
| 572 | |
| 573 | const result = await db |
| 574 | .update(outboxEvent) |
| 575 | .set(patch) |
| 576 | .where(and(...whereClauses)) |
| 577 | .returning({ id: outboxEvent.id }) |
| 578 | |
| 579 | return result.length > 0 |
| 580 | } |
no test coverage detected