(jobType: CleanupJobType)
| 295 | * Falls back to `JobQueueBackend` enqueue when Trigger.dev isn't available. |
| 296 | */ |
| 297 | export 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, |
no test coverage detected