use std::{path::Path, time::Duration};
const LOCK_WAIT: Duration = Duration::from_secs(15);
const POLL_INTERVAL: Duration = Duration::from_millis(250);
pub struct ActivationLock {
_guard: rightkit_fs::lock::FileLock,
}
pub fn acquire_lock(state_root: &Path) -> Result<ActivationLock, String> {
acquire_lock_with_wait(state_root, LOCK_WAIT)
}
fn acquire_lock_with_wait(root: &Path, wait: Duration) -> Result<ActivationLock, String> {
let guard_path = root.join(".activation.guard");
let guard_options = rightkit_fs::lock::LockOptions {
timeout: wait,
poll: POLL_INTERVAL,
label: "activation".to_string(),
};
let guard =
rightkit_fs::lock::FileLock::acquire(&guard_path, &guard_options).map_err(|error| {
match error {
rightkit_fs::FsError::Locked { .. } | rightkit_fs::FsError::LockTimeout { .. } => {
"activation startup lock remained busy".to_string()
}
other => format!("open activation guard: {other}"),
}
})?;
Ok(ActivationLock { _guard: guard })
}