pub struct PublishPid {
config: PublishPidConfig,
}Expand description
Contains the struct for configuration data and provides implementation for publication of process id (Pid).
Fields§
§config: PublishPidConfigImplementations§
Source§impl PublishPid
impl PublishPid
Sourcepub fn new(config: PublishPidConfig) -> Result<PublishPid, PublishPidError>
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:
- Checking for the existence of the PID file.
- 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.
- 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 thepid_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 au32integer.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.
Sourcepub fn erase_pid(&self) -> Result<(), PublishPidError>
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
impl Clone for PublishPid
Source§fn clone(&self) -> PublishPid
fn clone(&self) -> PublishPid
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more