summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2020-05-18 13:46:08 +0200
committerMatthias Beyer <mail@beyermatthias.de>2020-05-18 14:25:41 +0200
commit145a60d65756a4fb0a1429418a5f11f60fb1adcb (patch)
tree914edfbbf45f63c153affbb66e67dfe7058da87c
parent0717aca15929ff476ac92ec3bf2242943f817a64 (diff)
Add pidlock for server pid based locking
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r--Cargo.toml1
-rw-r--r--src/main.rs10
2 files changed, 10 insertions, 1 deletions
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();