11import { db } from '@sim/db'
2- import { workflowExecutionLogs } from '@sim/db/schema'
2+ import { jobExecutionLogs , workflowExecutionLogs } from '@sim/db/schema'
33import { createLogger } from '@sim/logger'
44import { task } from '@trigger.dev/sdk'
55import { and , inArray , lt } from 'drizzle-orm'
@@ -112,6 +112,63 @@ async function cleanupTier(
112112 return results
113113}
114114
115+ interface JobLogCleanupResults {
116+ deleted : number
117+ deleteFailed : number
118+ }
119+
120+ async function cleanupJobExecutionLogsTier (
121+ workspaceIds : string [ ] ,
122+ retentionDate : Date ,
123+ label : string
124+ ) : Promise < JobLogCleanupResults > {
125+ const results : JobLogCleanupResults = { deleted : 0 , deleteFailed : 0 }
126+ if ( workspaceIds . length === 0 ) return results
127+
128+ let batchesProcessed = 0
129+ let hasMore = true
130+
131+ while ( hasMore && batchesProcessed < MAX_BATCHES_PER_TIER ) {
132+ const batch = await db
133+ . select ( { id : jobExecutionLogs . id } )
134+ . from ( jobExecutionLogs )
135+ . where (
136+ and (
137+ inArray ( jobExecutionLogs . workspaceId , workspaceIds ) ,
138+ lt ( jobExecutionLogs . startedAt , retentionDate )
139+ )
140+ )
141+ . limit ( BATCH_SIZE )
142+
143+ if ( batch . length === 0 ) {
144+ hasMore = false
145+ break
146+ }
147+
148+ const logIds = batch . map ( ( log ) => log . id )
149+ try {
150+ const deleted = await db
151+ . delete ( jobExecutionLogs )
152+ . where ( inArray ( jobExecutionLogs . id , logIds ) )
153+ . returning ( { id : jobExecutionLogs . id } )
154+
155+ results . deleted += deleted . length
156+ } catch ( deleteError ) {
157+ results . deleteFailed += logIds . length
158+ logger . error ( `Batch delete failed for ${ label } (job_execution_logs):` , { deleteError } )
159+ }
160+
161+ batchesProcessed ++
162+ hasMore = batch . length === BATCH_SIZE
163+
164+ logger . info (
165+ `[${ label } ] job_execution_logs batch ${ batchesProcessed } : ${ batch . length } rows processed`
166+ )
167+ }
168+
169+ return results
170+ }
171+
115172export async function runCleanupLogs ( payload : CleanupJobPayload ) : Promise < void > {
116173 const startTime = Date . now ( )
117174
@@ -135,7 +192,12 @@ export async function runCleanupLogs(payload: CleanupJobPayload): Promise<void>
135192
136193 const results = await cleanupTier ( workspaceIds , retentionDate , label )
137194 logger . info (
138- `[${ label } ] Result: ${ results . deleted } deleted, ${ results . deleteFailed } failed out of ${ results . total } candidates`
195+ `[${ label } ] workflow_execution_logs: ${ results . deleted } deleted, ${ results . deleteFailed } failed out of ${ results . total } candidates`
196+ )
197+
198+ const jobLogResults = await cleanupJobExecutionLogsTier ( workspaceIds , retentionDate , label )
199+ logger . info (
200+ `[${ label } ] job_execution_logs: ${ jobLogResults . deleted } deleted, ${ jobLogResults . deleteFailed } failed`
139201 )
140202
141203 // Snapshot cleanup runs only on the free job to avoid running it N times for N enterprise workspaces.
0 commit comments