Skip to content

Commit 4fe0d0e

Browse files
waleedlatif1claude
andcommitted
fix(data-drains): guard dispatcher rollback so DB errors don't abort batch
If the rollback update threw (e.g. transient DB error), the exception bubbled out of the for loop and silently skipped the rest of the candidate drains for the cycle. Wrap it so the batch continues. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
1 parent 13f7eff commit 4fe0d0e

1 file changed

Lines changed: 17 additions & 5 deletions

File tree

apps/sim/lib/data-drains/dispatcher.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { db } from '@sim/db'
22
import { dataDrainRuns, dataDrains } from '@sim/db/schema'
33
import { createLogger } from '@sim/logger'
4+
import { toError } from '@sim/utils/errors'
45
import { and, eq, isNull, lt, or } from 'drizzle-orm'
56
import { isOrganizationOnEnterprisePlan } from '@/lib/billing/core/subscription'
67
import { getJobQueue } from '@/lib/core/async-jobs'
@@ -148,11 +149,22 @@ export async function dispatchDueDrains(now: Date = new Date()): Promise<{
148149
} catch (error) {
149150
// Roll back the claim — otherwise a transient queue outage silently
150151
// delays this drain by a full cadence. We only revert if no other
151-
// process has advanced lastRunAt past our claim timestamp.
152-
await db
153-
.update(dataDrains)
154-
.set({ lastRunAt: candidate.lastRunAt, updatedAt: now })
155-
.where(and(eq(dataDrains.id, candidate.id), eq(dataDrains.lastRunAt, now)))
152+
// process has advanced lastRunAt past our claim timestamp. Guard the
153+
// rollback itself so a transient DB error here doesn't abort the
154+
// remaining candidates in the batch.
155+
try {
156+
await db
157+
.update(dataDrains)
158+
.set({ lastRunAt: candidate.lastRunAt, updatedAt: now })
159+
.where(and(eq(dataDrains.id, candidate.id), eq(dataDrains.lastRunAt, now)))
160+
} catch (rollbackError) {
161+
logger.error('Failed to roll back data-drain claim after enqueue failure', {
162+
drainId: candidate.id,
163+
enqueueError: toError(error).message,
164+
rollbackError: toError(rollbackError).message,
165+
})
166+
continue
167+
}
156168
logger.error('Failed to enqueue data-drain job; rolled back claim', {
157169
drainId: candidate.id,
158170
error,

0 commit comments

Comments
 (0)