Remove completed/failed tasks older than max_age seconds. Returns: Number of tasks removed
(self, max_age: float = 3600)
| 205 | return list(self._tasks.values()) |
| 206 | |
| 207 | def cleanup_completed(self, max_age: float = 3600) -> int: |
| 208 | """ |
| 209 | Remove completed/failed tasks older than max_age seconds. |
| 210 | |
| 211 | Returns: |
| 212 | Number of tasks removed |
| 213 | """ |
| 214 | now = time.time() |
| 215 | to_remove = [] |
| 216 | |
| 217 | for task_id, task in self._tasks.items(): |
| 218 | if task.status in (BackgroundTaskStatus.COMPLETED, |
| 219 | BackgroundTaskStatus.FAILED, |
| 220 | BackgroundTaskStatus.STOPPED): |
| 221 | if task.stopped_at and (now - task.stopped_at) > max_age: |
| 222 | to_remove.append(task_id) |
| 223 | |
| 224 | for task_id in to_remove: |
| 225 | del self._tasks[task_id] |
| 226 | |
| 227 | if to_remove: |
| 228 | info(f"Background: cleaned up {len(to_remove)} completed tasks") |
| 229 | |
| 230 | return len(to_remove) |
| 231 | |
| 232 | def status(self) -> dict: |
| 233 | """Get background task manager status.""" |
no test coverage detected