@@ -220,6 +220,7 @@ interface BuildCleanupChunksResult {
220220async function buildCleanupChunks ( jobType : CleanupJobType ) : Promise < BuildCleanupChunksResult > {
221221 const config = CLEANUP_CONFIG [ jobType ]
222222 const chunks : CleanupJobPayload [ ] = [ ]
223+ const chunkCountByPlan : Partial < Record < NonEnterprisePlan , number > > = { }
223224 let workspaceCount = 0
224225 let afterId : string | null = null
225226
@@ -241,12 +242,14 @@ async function buildCleanupChunks(jobType: CleanupJobType): Promise<BuildCleanup
241242
242243 workspaceCount += workspaceIds . length
243244 const planChunks = chunkArray ( workspaceIds , WORKSPACES_PER_CLEANUP_CHUNK )
244- for ( const [ idx , ws ] of planChunks . entries ( ) ) {
245+ for ( const ws of planChunks ) {
246+ const chunkNumber = ( chunkCountByPlan [ plan ] ?? 0 ) + 1
247+ chunkCountByPlan [ plan ] = chunkNumber
245248 chunks . push ( {
246249 plan,
247250 workspaceIds : ws ,
248251 retentionHours,
249- label : planChunks . length > 1 ? `${ plan } /${ idx + 1 } ` : plan ,
252+ label : `${ plan } /${ chunkNumber } ` ,
250253 } )
251254 }
252255 }
@@ -267,15 +270,20 @@ async function buildCleanupChunks(jobType: CleanupJobType): Promise<BuildCleanup
267270
268271 const housekeepingPlan = GLOBAL_HOUSEKEEPING_PLAN [ jobType ]
269272 if ( housekeepingPlan && housekeepingPlan !== 'enterprise' ) {
270- const retentionHours = config . defaults [ housekeepingPlan ]
271- if ( retentionHours != null ) {
272- chunks . push ( {
273- plan : housekeepingPlan ,
274- workspaceIds : [ ] ,
275- retentionHours,
276- label : `${ housekeepingPlan } /housekeeping` ,
277- runGlobalHousekeeping : true ,
278- } )
273+ const target = chunks . find ( ( chunk ) => chunk . plan === housekeepingPlan )
274+ if ( target ) {
275+ target . runGlobalHousekeeping = true
276+ } else {
277+ const retentionHours = config . defaults [ housekeepingPlan ]
278+ if ( retentionHours != null ) {
279+ chunks . push ( {
280+ plan : housekeepingPlan ,
281+ workspaceIds : [ ] ,
282+ retentionHours,
283+ label : `${ housekeepingPlan } /housekeeping` ,
284+ runGlobalHousekeeping : true ,
285+ } )
286+ }
279287 }
280288 }
281289
@@ -305,6 +313,24 @@ export async function dispatchCleanupJobs(jobType: CleanupJobType): Promise<{
305313
306314 const jobIds : string [ ] = [ ]
307315
316+ if ( isTriggerAvailable ( ) ) {
317+ for ( let i = 0 ; i < chunks . length ; i += BATCH_TRIGGER_CHUNK_SIZE ) {
318+ const batch = chunks . slice ( i , i + BATCH_TRIGGER_CHUNK_SIZE )
319+ const batchResult = await tasks . batchTrigger (
320+ jobType ,
321+ batch . map ( ( payload ) => ( {
322+ payload,
323+ options : {
324+ tags : [ `plan:${ payload . plan } ` , `jobType:${ jobType } ` ] ,
325+ concurrencyKey : getCleanupConcurrencyKey ( jobType ) ,
326+ } ,
327+ } ) )
328+ )
329+ jobIds . push ( batchResult . batchId )
330+ }
331+ return { jobIds, jobCount : jobIds . length , chunkCount : chunks . length , workspaceCount }
332+ }
333+
308334 const inlineRunner = shouldExecuteInline ( ) ? await buildCleanupRunner ( jobType ) : undefined
309335 if ( inlineRunner ) {
310336 let succeeded = 0
@@ -329,24 +355,6 @@ export async function dispatchCleanupJobs(jobType: CleanupJobType): Promise<{
329355 return { jobIds, jobCount : jobIds . length , chunkCount : chunks . length , workspaceCount }
330356 }
331357
332- if ( isTriggerAvailable ( ) ) {
333- for ( let i = 0 ; i < chunks . length ; i += BATCH_TRIGGER_CHUNK_SIZE ) {
334- const batch = chunks . slice ( i , i + BATCH_TRIGGER_CHUNK_SIZE )
335- const batchResult = await tasks . batchTrigger (
336- jobType ,
337- batch . map ( ( payload ) => ( {
338- payload,
339- options : {
340- tags : [ `plan:${ payload . plan } ` , `jobType:${ jobType } ` ] ,
341- concurrencyKey : getCleanupConcurrencyKey ( jobType ) ,
342- } ,
343- } ) )
344- )
345- jobIds . push ( batchResult . batchId )
346- }
347- return { jobIds, jobCount : jobIds . length , chunkCount : chunks . length , workspaceCount }
348- }
349-
350358 const jobQueue = await getJobQueue ( )
351359 let succeeded = 0
352360 let failed = 0
0 commit comments