Skip to content

Commit 3a356a9

Browse files
Use group-by to ensure no duplicate jobs
1 parent 990d56a commit 3a356a9

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

apps/sim/lib/billing/cleanup-dispatcher.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ export async function resolveWorkspaceIdsForPlan(plan: NonEnterprisePlan): Promi
9191
)
9292
)
9393
.where(isNull(workspace.archivedAt))
94+
.groupBy(workspace.id)
9495

9596
return rows.map((r) => r.id)
9697
}
@@ -210,7 +211,10 @@ export async function dispatchCleanupJobs(
210211
await runInlineIfNeeded(jobQueue, jobType, jobId, payload)
211212
}
212213

213-
// Enterprise: query workspaces with non-NULL retention column
214+
// Enterprise: query workspaces with non-NULL retention column. The JOIN can
215+
// match multiple subscription rows per workspace (e.g. active + past_due both
216+
// in ENTITLED_SUBSCRIPTION_STATUSES) — groupBy dedupes to one row per workspace
217+
// so we don't dispatch the same cleanup job twice.
214218
const retentionCol = workspace[config.column]
215219
const enterpriseRows = await db
216220
.select({ id: workspace.id })
@@ -224,6 +228,7 @@ export async function dispatchCleanupJobs(
224228
)
225229
)
226230
.where(and(isNull(workspace.archivedAt), isNotNull(retentionCol)))
231+
.groupBy(workspace.id)
227232

228233
const enterpriseCount = enterpriseRows.length
229234

0 commit comments

Comments
 (0)