Struct PublishPid

Source
pub struct PublishPid {
    config: PublishPidConfig,
}
Expand description

Contains the struct for configuration data and provides implementation for publication of process id (Pid).

Fields§

§config: PublishPidConfig

Implementations§

Source§

impl PublishPid

Source

pub fn new(config: PublishPidConfig) -> Result<PublishPid, PublishPidError>

Creates a new PublishPid instance and publishes the current process ID (PID) to a file.

This constructor performs a critical step of publishing the application’s PID to a designated file. This is often used to prevent multiple instances of the application from running concurrently.

The process involves:

  1. Checking for the existence of the PID file.
  2. If the file exists and contains a valid, positive PID, the function returns an error, indicating that another instance of the application is likely already running.
  3. If the file does not exist, is empty, or contains “0”, the function proceeds to write the current process’s PID to the file, creating it if necessary.
§Arguments
  • config - Configuration data for publishing the PID, primarily containing the pid_filename.
§Returns

A Result containing a new PublishPid struct on success.

§Errors

Returns a PublishPidError if the PID cannot be published:

  • PublishPidError::UnableToReadPidFile: If the specified PID file exists but cannot be read due to, for example, insufficient permissions.
  • PublishPidError::PidFileParseError: If the PID file contains content that cannot be parsed into a u32 integer.
  • PublishPidError::PidFileContainsPid: If the PID file already contains a valid, positive PID, indicating a potential duplicate instance is running.
  • PublishPidError::UnableToWritePidFile: If writing the current process’s PID to the file fails, for example due to insufficient write permissions or an invalid path.
Source

pub fn erase_pid(&self) -> Result<(), PublishPidError>

Erases the PID from the file by writing “0” to it.

This is typically called during a graceful shutdown to release the application “lock” and allow a new instance to start up without conflicts.

§Returns

An empty Result (Ok(())) on a successful write.

§Errors

Returns a PublishPidError::UnableToWritePidFile if the PID file cannot be written to. This could be due to a permissions issue, an invalid path, or if the file was unexpectedly deleted.

Trait Implementations§

Source§

impl Clone for PublishPid

Source§

fn clone(&self) -> PublishPid

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T