From ddde0ea113bac7d228cc9947aeb427a455a25733 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sun, 9 Jan 2022 18:32:16 +0100 Subject: [PATCH] Add shutdown command to gracefully stop running daemons Signed-off-by: trivernis --- mediarepo-api/Cargo.toml | 2 +- mediarepo-api/src/client_api/mod.rs | 13 +++++++++++++ mediarepo-api/src/tauri_plugin/commands/repo.rs | 5 +++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/mediarepo-api/Cargo.toml b/mediarepo-api/Cargo.toml index b4551b3..7876ed4 100644 --- a/mediarepo-api/Cargo.toml +++ b/mediarepo-api/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "mediarepo-api" -version = "0.24.2" +version = "0.25.0" edition = "2018" license = "gpl-3" diff --git a/mediarepo-api/src/client_api/mod.rs b/mediarepo-api/src/client_api/mod.rs index 7c52aec..c3fbf2f 100644 --- a/mediarepo-api/src/client_api/mod.rs +++ b/mediarepo-api/src/client_api/mod.rs @@ -111,9 +111,22 @@ impl ApiClient { Ok(res.payload::()?) } + /// Shuts down the daemon that the client is connected to. + #[tracing::instrument(level = "debug", skip(self))] + pub async fn shutdown_daemon(&self) -> ApiResult<()> { + self.ctx + .acquire() + .emit("shutdown", ()) + .await_reply() + .with_timeout(Duration::from_secs(5)) + .await?; + Ok(()) + } + #[tracing::instrument(level = "debug", skip(self))] pub async fn exit(self) -> ApiResult<()> { let ctx = (*self.ctx.acquire()).clone(); + ctx.stop().await?; Ok(()) } diff --git a/mediarepo-api/src/tauri_plugin/commands/repo.rs b/mediarepo-api/src/tauri_plugin/commands/repo.rs index 956b0c3..a8a3373 100644 --- a/mediarepo-api/src/tauri_plugin/commands/repo.rs +++ b/mediarepo-api/src/tauri_plugin/commands/repo.rs @@ -140,6 +140,11 @@ pub async fn close_local_repository( let mut active_repo = app_state.active_repo.write().await; if let Some(path) = mem::take(&mut *active_repo).and_then(|r| r.path) { + if let Ok(api) = api_state.api().await { + if let Err(e) = api.shutdown_daemon().await { + tracing::error!("failed to ask the daemon to shut down daemon {:?}", e); + } + } app_state.stop_running_daemon(&path).await?; } api_state.disconnect().await;