MCPcopy Index your code
hub / github.com/simstudioai/sim / dispatchCleanupJobs

Function dispatchCleanupJobs

apps/sim/lib/billing/cleanup-dispatcher.ts:297–380  ·  view source on GitHub ↗
(jobType: CleanupJobType)

Source from the content-addressed store, hash-verified

295 * Falls back to `JobQueueBackend` enqueue when Trigger.dev isn't available.
296 */
297export async function dispatchCleanupJobs(jobType: CleanupJobType): Promise<{
298 jobIds: string[]
299 jobCount: number
300 chunkCount: number
301 workspaceCount: number
302}> {
303 const jobIds: string[] = []
304 let succeeded = 0
305 let failed = 0
306
307 if (isTriggerAvailable()) {
308 let batch: CleanupJobPayload[] = []
309 const flushBatch = async () => {
310 if (batch.length === 0) return
311 const currentBatch = batch
312 batch = []
313 const region = await resolveTriggerRegion()
314 const batchResult = await tasks.batchTrigger(
315 jobType,
316 currentBatch.map((payload) => ({
317 payload,
318 options: {
319 tags: [`plan:${payload.plan}`, `jobType:${jobType}`],
320 concurrencyKey: getCleanupConcurrencyKey(jobType),
321 region,
322 },
323 }))
324 )
325 jobIds.push(batchResult.batchId)
326 succeeded += currentBatch.length
327 }
328
329 const { chunkCount, workspaceCount } = await forEachCleanupChunk(jobType, async (payload) => {
330 batch.push(payload)
331 if (batch.length >= BATCH_TRIGGER_CHUNK_SIZE) {
332 await flushBatch()
333 }
334 })
335 await flushBatch()
336
337 logger.info(
338 `[${jobType}] Trigger cleanup chunks: ${succeeded} dispatched in ${jobIds.length} batch(es)`
339 )
340 return { jobIds, jobCount: jobIds.length, chunkCount, workspaceCount }
341 }
342
343 const inlineRunner = shouldExecuteInline() ? await buildCleanupRunner(jobType) : undefined
344 if (inlineRunner) {
345 const { chunkCount, workspaceCount } = await forEachCleanupChunk(jobType, async (payload) => {
346 try {
347 await inlineRunner(payload, new AbortController().signal)
348 jobIds.push(`inline:${jobType}:${payload.label}`)
349 succeeded++
350 } catch (error) {
351 failed++
352 logger.error(`[${jobType}] Inline cleanup chunk failed:`, {
353 plan: payload.plan,
354 label: payload.label,

Callers 3

route.tsFile · 0.90
route.tsFile · 0.90
route.tsFile · 0.90

Calls 11

isTriggerAvailableFunction · 0.90
shouldExecuteInlineFunction · 0.90
getJobQueueFunction · 0.90
forEachCleanupChunkFunction · 0.85
buildCleanupRunnerFunction · 0.85
getCleanupConcurrencyKeyFunction · 0.85
infoMethod · 0.80
errorMethod · 0.80
flushBatchFunction · 0.70
enqueueMethod · 0.65
pushMethod · 0.45

Tested by

no test coverage detected