diff --git a/internal/daemon/builtin_jobs.go b/internal/daemon/builtin_jobs.go index 43230c06e..00ca98255 100644 --- a/internal/daemon/builtin_jobs.go +++ b/internal/daemon/builtin_jobs.go @@ -29,6 +29,7 @@ const mirrorIdle = 7 * 24 * time.Hour func (d *Daemon) BuiltinJobs() []BuiltinJob { return []BuiltinJob{ {ID: "volume-cleanup", Name: "Remove expired volumes", DefaultInterval: time.Hour, Run: d.cleanupExpiredStorage}, + {ID: "clone-reclaim", Name: "Remove idle task clones", DefaultInterval: time.Hour, Run: d.reclaimIdleClones}, {ID: "issue-discovery", Name: "Discover forge issues", DefaultInterval: time.Minute, Run: d.IssuePoller.poll}, {ID: "pr-reconcile", Name: "Reconcile pull requests", DefaultInterval: time.Minute, Run: d.ReviewScanner.reconcilePRs}, {ID: "pr-review-scan", Name: "Scan pull request reviews", DefaultInterval: time.Minute, Run: d.ReviewScanner.scanPRReviews}, @@ -43,3 +44,26 @@ func (d *Daemon) pruneMirrors(ctx context.Context) error { } return err } + +// reclaimIdleClones removes every task clone idle for cloneRetention, so +// parked and finished tasks without a PR do not hold disk until the volume is +// full. A clone with uncommitted or unpushed work is kept and logged for the +// operator. +func (d *Daemon) reclaimIdleClones(ctx context.Context) error { + if d.Trees == nil { + return nil + } + noFloor := func() (uint64, error) { return 0, nil } + holdsWork := func(dir string) bool { + held := d.Trees.HoldsUnsharedWork(ctx, dir) + if held { + d.Log.WarnContext(ctx, "idle task clone kept: it holds uncommitted or unpushed work; archive the task to remove it", "dir", dir) + } + return held + } + n, err := worktree.ReclaimStale(d.Cfg.Get().WorkDir, "", cloneRetention, noFloor, 1, holdsWork) + if n > 0 { + d.Log.InfoContext(ctx, "idle task clones removed", "clones", n) + } + return err +}