Cleanup removes tasks that have been in a terminal state (Completed/Failed) for longer than maxAge, and cleans up their subIndex entries.
(maxAge time.Duration)
| 315 | // Cleanup removes tasks that have been in a terminal state (Completed/Failed) |
| 316 | // for longer than maxAge, and cleans up their subIndex entries. |
| 317 | func (tm *TaskManager) Cleanup(maxAge time.Duration) { |
| 318 | cutoff := time.Now().Add(-maxAge) |
| 319 | |
| 320 | tm.mu.Lock() |
| 321 | defer tm.mu.Unlock() |
| 322 | |
| 323 | for key, task := range tm.tasks { |
| 324 | task.mu.Lock() |
| 325 | terminal := task.State == TaskCompleted || task.State == TaskFailed |
| 326 | stale := task.UpdatedAt.Before(cutoff) |
| 327 | cmds := task.subCmds |
| 328 | task.mu.Unlock() |
| 329 | |
| 330 | if terminal && stale { |
| 331 | for _, cmdID := range cmds { |
| 332 | delete(tm.subIndex, cmdID) |
| 333 | } |
| 334 | delete(tm.tasks, key) |
| 335 | } |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | // CleanupLoop runs Cleanup periodically until ctx is cancelled. |
| 340 | func (tm *TaskManager) CleanupLoop(done <-chan struct{}, maxAge time.Duration) { |
no outgoing calls