Skip to main content

ralph/lock/
mod.rs

1//! Directory lock facade for queue and daemon coordination.
2//!
3//! Responsibilities:
4//! - Expose lock acquisition, owner metadata, stale-lock handling, and PID liveness helpers.
5//! - Keep concurrency-critical concerns split into focused submodules.
6//!
7//! Not handled here:
8//! - Queue mutation or config validation.
9//! - Cross-machine or distributed locking.
10//!
11//! Invariants/assumptions:
12//! - Callers hold `DirLock` for the full critical section.
13//! - The lock directory path is stable for the resource being protected.
14//! - The `task` label remains reserved for shared supervisor/task lock semantics.
15
16mod acquisition;
17mod cleanup;
18mod owner;
19mod pid;
20mod stale;
21
22use std::path::PathBuf;
23
24pub use acquisition::{acquire_dir_lock, is_supervising_process, queue_lock_dir};
25pub use owner::{LockOwner, TASK_OWNER_PREFIX, is_task_owner_file, read_lock_owner};
26pub use pid::{PidLiveness, pid_is_running, pid_liveness};
27pub(crate) use stale::{LockStaleness, classify_lock_owner};
28
29/// Held directory lock guard.
30#[derive(Debug)]
31pub struct DirLock {
32    lock_dir: PathBuf,
33    owner_path: PathBuf,
34}
35
36impl Drop for DirLock {
37    fn drop(&mut self) {
38        if let Err(error) = cleanup::cleanup_lock_dir(&self.lock_dir, &self.owner_path, false) {
39            log::warn!("Failed to clean up lock directory after retries: {}", error);
40        }
41    }
42}
43
44#[cfg(test)]
45mod tests;