summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Beyer <matthias.beyer@ifm.com>2022-07-26 11:19:13 +0200
committerMatthias Beyer <matthias.beyer@ifm.com>2022-08-30 10:03:21 +0200
commitf5b7f376e00950bba6eef07d2ef093174d702389 (patch)
tree537646c9b683a4f618d4c791c3fc3a81c8b2448d
parent7ddc423d8961efa3acc443bb8deb5828e08201fa (diff)
Simplify impl of check_another_instance_is_not_running()
This patch refactors the `check_another_instance_is_not_running()` function by introducing a `FlockfileError::path()` helper function to get the path for the error instance and re-implementing the function with that. Signed-off-by: Matthias Beyer <matthias.beyer@ifm.com>
-rw-r--r--crates/common/flockfile/src/unix.rs19
1 files changed, 13 insertions, 6 deletions
diff --git a/crates/common/flockfile/src/unix.rs b/crates/common/flockfile/src/unix.rs
index c3cfebe0..72fe1260 100644
--- a/crates/common/flockfile/src/unix.rs
+++ b/crates/common/flockfile/src/unix.rs
@@ -26,6 +26,15 @@ pub enum FlockfileError {
},
}
+impl FlockfileError {
+ fn path(&self) -> &Path {
+ match self {
+ FlockfileError::FromIo { path, .. } => path,
+ FlockfileError::FromNix { path, .. } => path,
+ }
+ }
+}
+
/// flockfile creates a lockfile in the filesystem under `/run/lock` and then creates a filelock using system fcntl with flock.
/// flockfile will automatically remove lockfile on application exit and the OS should cleanup the filelock afterwards.
/// If application exits unexpectedly the filelock will be dropped, but the lockfile will not be removed unless handled in signal handler.
@@ -110,12 +119,10 @@ pub fn check_another_instance_is_not_running(
) -> Result<Flockfile, FlockfileError> {
let lock_path = run_dir.join(format!("{}{}.lock", LOCK_CHILD_DIRECTORY, app_name));
- Flockfile::new_lock(lock_path.as_path()).map_err(|err| match &err {
- FlockfileError::FromIo { path, .. } | FlockfileError::FromNix { path, .. } => {
- error!("Another instance of {} is running.", app_name);
- error!("Lock file path: {}", path.as_path().to_str().unwrap());
- err
- }
+ Flockfile::new_lock(lock_path.as_path()).map_err(|err| {
+ error!("Another instance of {} is running.", app_name);
+ error!("Lock file path: {}", err.path().to_str().unwrap());
+ err
})
}