rightkit_context/
activation.rs1use std::{path::Path, time::Duration};
8
9const LOCK_WAIT: Duration = Duration::from_secs(15);
10const POLL_INTERVAL: Duration = Duration::from_millis(250);
11
12pub struct ActivationLock {
15 _guard: rightkit_fs::lock::FileLock,
16}
17
18pub fn acquire_lock(state_root: &Path) -> Result<ActivationLock, String> {
20 acquire_lock_with_wait(state_root, LOCK_WAIT)
21}
22
23fn acquire_lock_with_wait(root: &Path, wait: Duration) -> Result<ActivationLock, String> {
24 let guard_path = root.join(".activation.guard");
25 let guard_options = rightkit_fs::lock::LockOptions {
26 timeout: wait,
27 poll: POLL_INTERVAL,
28 label: "activation".to_string(),
29 };
30 let guard =
31 rightkit_fs::lock::FileLock::acquire(&guard_path, &guard_options).map_err(|error| {
32 match error {
33 rightkit_fs::FsError::Locked { .. } | rightkit_fs::FsError::LockTimeout { .. } => {
34 "activation startup lock remained busy".to_string()
35 }
36 other => format!("open activation guard: {other}"),
37 }
38 })?;
39 Ok(ActivationLock { _guard: guard })
40}