Files
archie/internal/worktree/space.go
T
sam 52b5740c39
deploy / plan (push) Successful in 1m6s
deploy / archied-image (push) Canceled after 0s
deploy / agent-image (push) Canceled after 0s
deploy / dist-zip (push) Canceled after 0s
deploy / gate (push) Canceled after 11s
fix(daemon): cap tasks per issue poll and wait for disk before cloning (#1805)
2026-11-08 17:26:00 +11:00

55 lines
1.4 KiB
Go

package worktree
import (
"context"
"errors"
"fmt"
"os"
"path/filepath"
"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
}
}