Skip to main content

Guard

Struct Guard 

Source
pub struct Guard { /* private fields */ }
Available on crate feature 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

Source

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()?;
Source

pub fn report(&self) -> &GuardReport

What the guard did.

Source

pub fn lock_file(&self) -> Option<&Path>

The lock file, when a lock was taken.

Source

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");
}

Trait Implementations§

Source§

impl Debug for Guard

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Forget for Guard

§

impl Freeze for Guard

§

impl Move for Guard

§

impl RefUnwindSafe for Guard

§

impl Send for Guard

§

impl Sync for Guard

§

impl Unpin for Guard

§

impl UnsafeUnpin for Guard

§

impl UnwindSafe for Guard

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.