From 145a60d65756a4fb0a1429418a5f11f60fb1adcb Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Mon, 18 May 2020 13:46:08 +0200 Subject: Add pidlock for server pid based locking Signed-off-by: Matthias Beyer --- Cargo.toml | 1 + src/main.rs | 10 +++++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index de1266a..c5d224b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -43,3 +43,4 @@ actix-rt = "1" actix-web = "2" port_check = "0.1" failure = "0.1" +pidlock = { git = "https://github.com/matthiasbeyer/pidlock", branch = "my-master" } diff --git a/src/main.rs b/src/main.rs index abd64e5..7561e0c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,6 +19,7 @@ extern crate web_view; extern crate actix_rt; extern crate actix_web; extern crate failure; +extern crate pidlock; #[macro_use] extern crate anyhow; #[macro_use] extern crate is_match; @@ -90,6 +91,13 @@ async fn main() -> Result<()> { let adr = format!("127.0.0.1:{}", port); if start_server(&cli) { + let mut lock = pidlock::Pidlock::new("/tmp/distrox_server.pid"); + if *lock.state() == pidlock::PidlockState::Acquired { + // We assume that the server is already running + return Ok(()) + } + + let _ = lock.acquire().map_err(|_| anyhow!("Error while getting the PID lock"))?; actix_web::HttpServer::new(|| { actix_web::App::new() .service(actix_web::web::resource("/{name}/{id}/index.html").to(index)) @@ -99,7 +107,7 @@ async fn main() -> Result<()> { .run() .await; - Ok(()) + lock.release().map_err(|_| anyhow!("Error while releasing the PID lock")) } else { let app = { let device_name = config.get_device_name(); -- cgit v1.2.3