Files
archie/internal/daemon/builtin_jobs.go
T

70 lines
2.8 KiB
Go

package daemon
import (
"context"
"time"
"github.com/samcharles93/archie-core/internal/worktree"
)
// BuiltinJob is instance maintenance the daemon defines in code. The
// scheduler shows each as a locked schedule: it can be enabled, disabled,
// retimed and run now, but its work and ID are fixed here.
type BuiltinJob struct {
// ID is stable across releases; the scheduler keys its row on it.
ID string
Name string
// DefaultInterval is the schedule a new installation starts with.
DefaultInterval time.Duration
Run func(ctx context.Context) error
}
// mirrorIdle is how long a repository mirror no task clone borrows from
// survives before pruning; the next task for that repository rebuilds it.
const mirrorIdle = 7 * 24 * time.Hour
// BuiltinJobs lists the daemon's maintenance work. Binding dispatch and
// reaction draining are not here: they turn arrived events into tasks, so
// they stay on the intake tick rather than a schedule's coarser cadence.
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},
{ID: "mirror-prune", Name: "Prune repository mirrors", DefaultInterval: 24 * time.Hour, Run: d.pruneMirrors},
}
}
func (d *Daemon) pruneMirrors(ctx context.Context) error {
removed, err := worktree.PruneMirrors(ctx, d.Cfg.Get().WorkDir, mirrorIdle, time.Now())
for _, mirror := range removed {
d.Log.InfoContext(ctx, "idle repository mirror removed", "path", mirror)
}
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
}