Skip to content

Commit afcba35

Browse files
waleedlatif1claude
andcommitted
fix(usage): lock activity rollup marks in key order
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 59ea17b commit afcba35

2 files changed

Lines changed: 4 additions & 0 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ export async function markWorkflowActivityDirty(
7878
SELECT 1 FROM ${activityRollupState}
7979
WHERE maintained_at > now() - ${msInterval(MAINTENANCE_GAP_MS)}
8080
)
81+
-- Writers of several marks lock them in key order, so two can never deadlock.
82+
ORDER BY 1, 2
8183
ON CONFLICT (workspace_id, bucket_start) DO UPDATE SET due_at = excluded.due_at
8284
`)
8385
})
@@ -147,6 +149,7 @@ export async function captureLateEnds(
147149
FROM ${workflowExecutionLogs} l, bound
148150
WHERE l.ended_at >= l.started_at + interval '2 hours'
149151
AND l.ended_at > bound.cursor AND l.ended_at <= bound.boundary
152+
ORDER BY 1, 2
150153
ON CONFLICT (workspace_id, bucket_start) DO NOTHING
151154
RETURNING 1
152155
), moved AS (

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,7 @@ export function markStaleHours(workspaceId: string, range: InstantRange): Promis
472472
SELECT 1 FROM ${activityRollupInflight} q
473473
WHERE q.workspace_id = ${workspaceId} AND q.bucket_start = d.hour
474474
)
475+
ORDER BY d.hour
475476
ON CONFLICT (workspace_id, bucket_start) DO NOTHING
476477
`)
477478
return marked.count

0 commit comments

Comments
 (0)