Skip to content

Commit 477472b

Browse files
waleedlatif1claude
andcommitted
fix(usage): count a failed pass whose lease overran, log the marked-cell fallback at info
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 3c74e17 commit 477472b

3 files changed

Lines changed: 22 additions & 9 deletions

File tree

‎apps/sim/lib/billing/activity-rollup/pass.ts‎

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import {
5050
resetRollup,
5151
truncateRollupBatch,
5252
updateLeasedState,
53+
WORKFLOW_ROLLUP_ID,
5354
} from '@/lib/billing/activity-rollup/state'
5455
import type { DbTransaction } from '@/lib/db/types'
5556

@@ -664,14 +665,25 @@ export async function runActivityRollupPass(
664665
}
665666
}
666667

667-
/** Counts a pass that failed after taking the lease, and logs the run of failures loudly. */
668+
/**
669+
* Counts a pass that failed after taking the lease, and logs the run of failures loudly. Matched on
670+
* the owner alone, so a pass that overran its lease is still counted; one whose lease another pass
671+
* took over was already counted by that takeover, and matches nothing.
672+
*/
668673
async function recordFailedPass(owner: string, error: unknown): Promise<void> {
669674
try {
670-
const state = await updateLeasedState(owner, {
671-
failedPasses: sql`${activityRollupState.failedPasses} + 1`,
672-
})
675+
const [state] = await db
676+
.update(activityRollupState)
677+
.set({ failedPasses: sql`${activityRollupState.failedPasses} + 1`, updatedAt: sql`now()` })
678+
.where(
679+
and(
680+
eq(activityRollupState.id, WORKFLOW_ROLLUP_ID),
681+
eq(activityRollupState.leaseOwner, owner)
682+
)
683+
)
684+
.returning({ failedPasses: activityRollupState.failedPasses })
673685
logger.error('Activity rollup pass failed', {
674-
consecutiveFailures: state.failedPasses,
686+
consecutiveFailures: state?.failedPasses ?? null,
675687
error: getErrorMessage(error),
676688
})
677689
} catch (countError) {

‎apps/sim/lib/billing/activity-rollup/state.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ export async function readRollupState(
5555
*
5656
* Every pass releases the lease on its way out, so one taken over after it expired belonged to a
5757
* pass that died holding it (killed, or out of memory) and is counted as a failed pass, like one
58-
* that threw.
58+
* that threw. A lease left behind for any other reason (its release failing) counts the same way,
59+
* which at worst backs off a deferred hour one failure early.
5960
*/
6061
export async function acquireLease(owner: string): Promise<ActivityRollupState | null> {
6162
await db

‎apps/sim/lib/billing/core/organization-activity-rollup-queries.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ const MAX_FRESHNESS_LAG_MS = 6 * HOUR_MS
4848
/**
4949
* Reads use the rollup only while the late-end scan is this recent: a run that ends late leaves
5050
* its stored cell stale until the scan marks it. The scan trails now by its margin plus the time
51-
* between passes, minutes in all, so this bound is only crossed when the scan is failing or
52-
* catching up a backlog.
51+
* between passes (longer while a heavy-hour retry holds the lease), a quarter hour at most, so
52+
* this bound is only crossed when the scan is failing or catching up a backlog.
5353
*/
5454
const MAX_LATE_END_SCAN_LAG_MS = HOUR_MS / 2
5555

@@ -118,7 +118,7 @@ function readInSnapshot<T>(
118118
)
119119
if (!plan) return null
120120
if (plan.rollup && (await exceedsLiveMarkedCells(tx, scope, plan.rollup))) {
121-
logger.warn('Organization activity read too many marked cells; using the legacy read', {
121+
logger.info('Organization activity read too many marked cells; using the legacy read', {
122122
organizationId: scope.organizationId,
123123
maxLiveMarkedCells: MAX_LIVE_MARKED_CELLS,
124124
})

0 commit comments

Comments
 (0)