pub struct Guard { /* private fields */ }guard only.Expand description
The running guard. Dropping it closes the lock file, which releases the lock; the lock file itself is never deleted (deleting it would let two copies run, see the plan’s guard section).
The lock belongs to the file, not to its path. On Windows the file cannot
be deleted or renamed while the guard holds it. On Unix it can (a temporary
file cleaner, an operator’s rm), and a copy started afterwards would
create a new lock file and run; Guard::is_held tells a long-running
service when that has happened.
Implementations§
Source§impl Guard
impl Guard
Sourcepub fn builder(name: &str) -> GuardBuilder
pub fn builder(name: &str) -> GuardBuilder
A builder for options other than the defaults.
use sys_lib::process::Guard;
// A database locks beside its data, so two copies under different users
// or run directories still exclude each other.
let guard = Guard::builder("myapp")
.lock_dir("/var/lib/myapp")
.open_files_target(1_048_576)
.open_files_minimum(65_536)
.build()?;Sourcepub fn report(&self) -> &GuardReport
pub fn report(&self) -> &GuardReport
What the guard did.
Sourcepub fn is_held(&self) -> bool
pub fn is_held(&self) -> bool
Whether the lock file at its path is still the one this guard locked. False when the file was deleted or replaced since (only possible on Unix), so another copy could now start, or when no lock was taken. A service that runs for long can check it periodically and stop.
use sys_lib as sys;
let guard = sys::process::guard("site")?;
// ... later, on a timer:
if !guard.is_held() {
eprintln!("the lock file was removed; stopping so only one copy runs");
}