CleanupExpired removes tasks that reached a terminal state more than the store's retention duration ago. It is safe to call periodically.
()
| 94 | // CleanupExpired removes tasks that reached a terminal state more than the |
| 95 | // store's retention duration ago. It is safe to call periodically. |
| 96 | func CleanupExpired() { |
| 97 | now := time.Now() |
| 98 | store.mu.Lock() |
| 99 | defer store.mu.Unlock() |
| 100 | for id, info := range store.tasks { |
| 101 | info.mu.Lock() |
| 102 | terminal := info.Status == TaskStatusCompleted || info.Status == TaskStatusFailed || info.Status == TaskStatusCancelled |
| 103 | stale := terminal && now.Sub(info.UpdatedAt) > store.retention |
| 104 | info.mu.Unlock() |
| 105 | if stale { |
| 106 | delete(store.tasks, id) |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | // StartCleanupLoop runs CleanupExpired on a fixed interval until ctx is done. |
| 112 | // It should be started once during API server initialization. |