Skip to main content

Module loop_lock

Module loop_lock 

Source
Expand description

Loop lock mechanism for preventing concurrent Ralph loops in the same workspace.

Uses flock() on .ralph/loop.lock to ensure only one primary loop runs at a time. When a second loop attempts to start, it can detect the existing lock and spawn into a git worktree instead.

§Example

use ralph_core::loop_lock::{LoopLock, LockError};

fn main() -> Result<(), Box<dyn std::error::Error>> {
    match LoopLock::try_acquire(".", "implement auth") {
        Ok(guard) => {
            // We're the primary loop - run normally
            println!("Acquired lock, running as primary loop");
            // Lock is held until guard is dropped
        }
        Err(LockError::AlreadyLocked(existing)) => {
            // Another loop is running - spawn into worktree
            println!("Lock held by PID {}, spawning worktree", existing.pid);
        }
        Err(e) => return Err(e.into()),
    }
    Ok(())
}

Structs§

LockGuard
A guard that holds the loop lock. The lock is released when this is dropped.
LockMetadata
Metadata stored in the lock file, readable by other processes.
LoopLock
The loop lock mechanism.

Enums§

LockError
Errors that can occur during lock operations.