( workspaceIds: string[], retentionDate: Date, label: string )
| 39 | ] as const |
| 40 | |
| 41 | async function cleanupRunChildren( |
| 42 | workspaceIds: string[], |
| 43 | retentionDate: Date, |
| 44 | label: string |
| 45 | ): Promise<TableCleanupResult[]> { |
| 46 | if (workspaceIds.length === 0) return [] |
| 47 | |
| 48 | const runIds = await selectRowsByIdChunks(workspaceIds, (chunkIds, chunkLimit) => |
| 49 | db |
| 50 | .select({ id: copilotRuns.id }) |
| 51 | .from(copilotRuns) |
| 52 | .where( |
| 53 | and(inArray(copilotRuns.workspaceId, chunkIds), lt(copilotRuns.updatedAt, retentionDate)) |
| 54 | ) |
| 55 | .limit(chunkLimit) |
| 56 | ) |
| 57 | |
| 58 | if (runIds.length === 0) { |
| 59 | return RUN_CHILD_TABLES.map((t) => ({ table: `${label}/${t.name}`, deleted: 0, failed: 0 })) |
| 60 | } |
| 61 | |
| 62 | const ids = runIds.map((r) => r.id) |
| 63 | |
| 64 | return Promise.all( |
| 65 | RUN_CHILD_TABLES.map((t) => deleteRowsById(t.table, t.runIdCol, ids, `${label}/${t.name}`)) |
| 66 | ) |
| 67 | } |
| 68 | |
| 69 | export async function runCleanupTasks(payload: CleanupJobPayload): Promise<void> { |
| 70 | const startTime = Date.now() |
no test coverage detected