( workspaceIds: string[], retentionDate: Date, label: string )
| 130 | } |
| 131 | |
| 132 | async function cleanupLargeExecutionValues( |
| 133 | workspaceIds: string[], |
| 134 | retentionDate: Date, |
| 135 | label: string |
| 136 | ): Promise<LargeValueCleanupStats> { |
| 137 | const stats: LargeValueCleanupStats = { |
| 138 | largeValuesTotal: 0, |
| 139 | largeValuesDeleted: 0, |
| 140 | largeValuesDeleteFailed: 0, |
| 141 | } |
| 142 | if (workspaceIds.length === 0) return stats |
| 143 | |
| 144 | const largeValueRetentionDate = new Date( |
| 145 | retentionDate.getTime() - LARGE_VALUE_CLEANUP_GRACE_HOURS * 60 * 60 * 1000 |
| 146 | ) |
| 147 | const workspaceChunks = chunkArray(workspaceIds, 50) |
| 148 | let attempted = 0 |
| 149 | |
| 150 | for (const chunkIds of workspaceChunks) { |
| 151 | while (attempted < LARGE_VALUE_CLEANUP_TOTAL_KEY_LIMIT) { |
| 152 | const limit = Math.min( |
| 153 | LARGE_VALUE_CLEANUP_BATCH_SIZE, |
| 154 | LARGE_VALUE_CLEANUP_TOTAL_KEY_LIMIT - attempted |
| 155 | ) |
| 156 | const rows = await db |
| 157 | .select({ key: executionLargeValues.key }) |
| 158 | .from(executionLargeValues) |
| 159 | .where( |
| 160 | and( |
| 161 | inArray(executionLargeValues.workspaceId, chunkIds), |
| 162 | isNull(executionLargeValues.deletedAt), |
| 163 | lt(executionLargeValues.createdAt, largeValueRetentionDate), |
| 164 | unreferencedLargeValuePredicate() |
| 165 | ) |
| 166 | ) |
| 167 | .orderBy( |
| 168 | asc(executionLargeValues.workspaceId), |
| 169 | asc(executionLargeValues.createdAt), |
| 170 | asc(executionLargeValues.key) |
| 171 | ) |
| 172 | .limit(limit) |
| 173 | |
| 174 | if (rows.length === 0) break |
| 175 | |
| 176 | const keys = rows.map((row) => row.key) |
| 177 | stats.largeValuesTotal += keys.length |
| 178 | attempted += keys.length |
| 179 | const result = await deleteLargeValueKeys(keys) |
| 180 | stats.largeValuesDeleted += result.deleted |
| 181 | stats.largeValuesDeleteFailed += result.failed |
| 182 | |
| 183 | if (result.deleted === 0) { |
| 184 | break |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | if (attempted >= LARGE_VALUE_CLEANUP_TOTAL_KEY_LIMIT) break |
| 189 | } |
no test coverage detected