Files

106 lines
3.1 KiB
Go

package worktree
import (
"context"
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"regexp"
"sort"
"syscall"
"time"
)
// ErrDiskLow means the work volume stayed below the free-space floor.
var ErrDiskLow = errors.New("disk low")
// FreeBytes reports the space available to unprivileged writers on the volume
// holding the work directory, which may not exist yet.
func (m *Manager) FreeBytes() (uint64, error) {
path := m.WorkDir
for {
var st syscall.Statfs_t
err := syscall.Statfs(path, &st)
if err == nil {
return st.Bavail * uint64(st.Bsize), nil //nolint:gosec // Bsize is positive
}
parent := filepath.Dir(path)
if !errors.Is(err, os.ErrNotExist) || parent == path {
return 0, err
}
path = parent
}
}
// AwaitSpace blocks until free reports at least floor bytes, checking every
// interval for up to wait, so a full volume delays a clone instead of failing
// it half-written. A volume that cannot be measured does not block the clone.
func AwaitSpace(ctx context.Context, free func() (uint64, error), floor uint64, interval, wait time.Duration) error {
deadline := time.Now().Add(wait)
for {
have, err := free()
if err == nil && have < floor {
if !time.Now().Add(interval).Before(deadline) {
return fmt.Errorf("%w: %d MiB free on the work volume, %d MiB needed to clone", ErrDiskLow, have>>20, floor>>20)
}
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(interval):
}
continue
}
return nil
}
}
// ReclaimStale removes task clones under root that have been untouched for
// longer than minAge, oldest first, until free reports at least floor bytes.
// keep is never removed, nor is a clone holdsWork reports as holding work
// that exists nowhere else. It returns how many clones it removed.
func ReclaimStale(root, keep string, minAge time.Duration, free func() (uint64, error), floor uint64, holdsWork func(dir string) bool) (int, error) {
type clone struct {
dir string
used time.Time
}
var stale []clone
cutoff := time.Now().Add(-minAge)
err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
if err != nil || !d.IsDir() || !issueDir.MatchString(d.Name()) {
return nil //nolint:nilerr // an unreadable branch is skipped, not fatal
}
git, err := os.Stat(filepath.Join(path, ".git"))
if err != nil {
return filepath.SkipDir
}
// The task brief and every git write touch .git, so its mtime is the
// clone's last use.
if path != keep && git.ModTime().Before(cutoff) {
stale = append(stale, clone{path, git.ModTime()})
}
return filepath.SkipDir
})
if err != nil {
return 0, err
}
sort.Slice(stale, func(i, j int) bool { return stale[i].used.Before(stale[j].used) })
removed := 0
for _, c := range stale {
if have, err := free(); err != nil || have >= floor {
return removed, nil //nolint:nilerr // an unmeasurable volume stops the reclaim, it is not an error
}
if holdsWork(c.dir) {
continue
}
if err := os.RemoveAll(c.dir); err != nil {
return removed, err
}
removed++
}
return removed, nil
}
var issueDir = regexp.MustCompile(`^issue-\d+$`)