feat(v0.4): add WithLock helper that warns-and-skips on timeout

This commit is contained in:
2026-04-21 17:01:36 -04:00
parent dc16713f11
commit 1c62410a06
2 changed files with 66 additions and 0 deletions
+17
View File
@@ -52,3 +52,20 @@ func Acquire(lockPath string, timeout, staleAfter time.Duration) (func(), error)
}
}
}
// WithLock acquires the lock at lockPath, runs fn while holding it, and
// releases the lock. If the lock cannot be acquired within timeout, fn is
// NOT called and (skipped=true, err=nil) is returned — this matches the
// ctask "warn and skip, never hang" contract for metadata writes.
// Any error returned by fn is surfaced to the caller with skipped=false.
func WithLock(lockPath string, timeout, staleAfter time.Duration, fn func() error) (skipped bool, err error) {
release, err := Acquire(lockPath, timeout, staleAfter)
if err != nil {
if errors.Is(err, ErrTimeout) {
return true, nil
}
return false, err
}
defer release()
return false, fn()
}