Use a stable workspace lock for coordination
This commit is contained in:
@@ -15,10 +15,10 @@ import (
|
||||
"gitea.maximumdirect.net/eric/narratio/internal/pathsafe"
|
||||
)
|
||||
|
||||
// ErrLockConflict is returned when a session lock is currently held.
|
||||
var ErrLockConflict = errors.New("session workdir is already locked")
|
||||
// ErrLockConflict is returned when a workspace lock is currently held.
|
||||
var ErrLockConflict = errors.New("workspace is already locked")
|
||||
|
||||
// LockHandle tracks a held lock for a session work directory.
|
||||
// LockHandle tracks a held workspace lock.
|
||||
type LockHandle struct {
|
||||
path string
|
||||
file *os.File
|
||||
@@ -190,27 +190,24 @@ func (s *LocalStore) Checksum(path string) (string, error) {
|
||||
return digest, nil
|
||||
}
|
||||
|
||||
// AcquireSessionLockFor acquires an exclusive lock file for a campaign/session workdir.
|
||||
func (s *LocalStore) AcquireSessionLockFor(campaign, sessionID string) (*LockHandle, error) {
|
||||
paths, err := s.EnsureLayoutFor(campaign, sessionID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
// AcquireWorkspaceLock acquires the exclusive workspace lock without waiting.
|
||||
func (s *LocalStore) AcquireWorkspaceLock() (*LockHandle, error) {
|
||||
if strings.TrimSpace(s.WorkspaceRoot) == "" {
|
||||
return nil, fmt.Errorf("workspace root is required")
|
||||
}
|
||||
return s.acquireSessionLockForPaths(paths)
|
||||
return s.acquireWorkspaceLock(WorkspaceLockPath(s.WorkspaceRoot))
|
||||
}
|
||||
|
||||
// AcquireSessionLockForContext waits for a session lock until it becomes
|
||||
// available or ctx is cancelled.
|
||||
func (s *LocalStore) AcquireSessionLockForContext(ctx context.Context, campaign, sessionID string) (*LockHandle, error) {
|
||||
// AcquireWorkspaceLockContext waits until the workspace lock is available or cancelled.
|
||||
func (s *LocalStore) AcquireWorkspaceLockContext(ctx context.Context) (*LockHandle, error) {
|
||||
if ctx == nil {
|
||||
ctx = context.Background()
|
||||
}
|
||||
paths, err := s.EnsureLayoutFor(campaign, sessionID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for {
|
||||
lock, err := s.acquireSessionLockForPaths(paths)
|
||||
if err := context.Cause(ctx); err != nil {
|
||||
return nil, fmt.Errorf("wait for workspace lock %q: %w", WorkspaceLockPath(s.WorkspaceRoot), err)
|
||||
}
|
||||
lock, err := s.AcquireWorkspaceLock()
|
||||
if !errors.Is(err, ErrLockConflict) {
|
||||
return lock, err
|
||||
}
|
||||
@@ -220,29 +217,29 @@ func (s *LocalStore) AcquireSessionLockForContext(ctx context.Context, campaign,
|
||||
if !timer.Stop() {
|
||||
<-timer.C
|
||||
}
|
||||
return nil, fmt.Errorf("wait for session lock %q: %w", paths.LockPath, ctx.Err())
|
||||
return nil, fmt.Errorf("wait for workspace lock %q: %w", WorkspaceLockPath(s.WorkspaceRoot), context.Cause(ctx))
|
||||
case <-timer.C:
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *LocalStore) acquireSessionLockForPaths(paths SessionPaths) (*LockHandle, error) {
|
||||
if err := fileops.EnsureWorkspaceDirectory(filepath.Dir(paths.LockPath)); err != nil {
|
||||
return nil, fmt.Errorf("create lock directory %q: %w", filepath.Dir(paths.LockPath), err)
|
||||
func (s *LocalStore) acquireWorkspaceLock(lockPath string) (*LockHandle, error) {
|
||||
if err := fileops.EnsureWorkspaceDirectory(filepath.Dir(lockPath)); err != nil {
|
||||
return nil, fmt.Errorf("create lock directory %q: %w", filepath.Dir(lockPath), err)
|
||||
}
|
||||
f, err := os.OpenFile(paths.LockPath, os.O_CREATE|os.O_RDWR, fileops.WorkspaceFileMode)
|
||||
f, err := os.OpenFile(lockPath, os.O_CREATE|os.O_RDWR, fileops.WorkspaceFileMode)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("acquire lock %q: %w", paths.LockPath, err)
|
||||
return nil, fmt.Errorf("acquire lock %q: %w", lockPath, err)
|
||||
}
|
||||
if err := acquireHeldFileLockFn(f); err != nil {
|
||||
closeErr := f.Close()
|
||||
if errors.Is(err, errHeldLockConflict) {
|
||||
return nil, fmt.Errorf("%w: %s", ErrLockConflict, paths.LockPath)
|
||||
return nil, fmt.Errorf("%w: %s", ErrLockConflict, lockPath)
|
||||
}
|
||||
if closeErr != nil {
|
||||
return nil, errors.Join(fmt.Errorf("acquire lock %q: hold: %w", paths.LockPath, err), closeErr)
|
||||
return nil, errors.Join(fmt.Errorf("acquire lock %q: hold: %w", lockPath, err), closeErr)
|
||||
}
|
||||
return nil, fmt.Errorf("acquire lock %q: hold: %w", paths.LockPath, err)
|
||||
return nil, fmt.Errorf("acquire lock %q: hold: %w", lockPath, err)
|
||||
}
|
||||
failed := true
|
||||
defer func() {
|
||||
@@ -252,29 +249,29 @@ func (s *LocalStore) acquireSessionLockForPaths(paths SessionPaths) (*LockHandle
|
||||
}
|
||||
}()
|
||||
if err := f.Chmod(fileops.WorkspaceFileMode); err != nil {
|
||||
return nil, fmt.Errorf("acquire lock %q: set permissions: %w", paths.LockPath, err)
|
||||
return nil, fmt.Errorf("acquire lock %q: set permissions: %w", lockPath, err)
|
||||
}
|
||||
if err := f.Truncate(0); err != nil {
|
||||
return nil, fmt.Errorf("acquire lock %q: clear metadata: %w", paths.LockPath, err)
|
||||
return nil, fmt.Errorf("acquire lock %q: clear metadata: %w", lockPath, err)
|
||||
}
|
||||
if _, err := f.Seek(0, io.SeekStart); err != nil {
|
||||
return nil, fmt.Errorf("acquire lock %q: seek metadata: %w", paths.LockPath, err)
|
||||
return nil, fmt.Errorf("acquire lock %q: seek metadata: %w", lockPath, err)
|
||||
}
|
||||
|
||||
metadata := "pid=" + strconv.Itoa(os.Getpid()) + "\nacquired_at=" + time.Now().UTC().Format(time.RFC3339Nano) + "\n"
|
||||
if _, err := io.WriteString(f, metadata); err != nil {
|
||||
return nil, fmt.Errorf("acquire lock %q: write metadata: %w", paths.LockPath, err)
|
||||
return nil, fmt.Errorf("acquire lock %q: write metadata: %w", lockPath, err)
|
||||
}
|
||||
if err := f.Sync(); err != nil {
|
||||
return nil, fmt.Errorf("acquire lock %q: sync: %w", paths.LockPath, err)
|
||||
return nil, fmt.Errorf("acquire lock %q: sync: %w", lockPath, err)
|
||||
}
|
||||
|
||||
failed = false
|
||||
return &LockHandle{path: paths.LockPath, file: f}, nil
|
||||
return &LockHandle{path: lockPath, file: f}, nil
|
||||
}
|
||||
|
||||
// ReleaseSessionLock releases a previously acquired session lock.
|
||||
func (s *LocalStore) ReleaseSessionLock(lock *LockHandle) error {
|
||||
// ReleaseWorkspaceLock releases a previously acquired workspace lock.
|
||||
func (s *LocalStore) ReleaseWorkspaceLock(lock *LockHandle) error {
|
||||
if lock == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user