Change daemons to be killed on drop

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/4/head
trivernis 3 years ago
parent 778bb2fe77
commit 1dc4a32953

@ -14,6 +14,7 @@ rmp-ipc = {version = "0.8.1", optional=true}
parking_lot = {version="0.11.2", optional=true} parking_lot = {version="0.11.2", optional=true}
serde_json = {version="1.0.68", optional=true} serde_json = {version="1.0.68", optional=true}
directories = {version="4.0.1", optional=true} directories = {version="4.0.1", optional=true}
serde_piecewise_default = "0.2.0"
[dependencies.serde] [dependencies.serde]
version = "1.0.130" version = "1.0.130"

@ -1,5 +1,6 @@
use crate::daemon_management::error::{DaemonError, DaemonResult}; use crate::daemon_management::error::{DaemonError, DaemonResult};
use std::ffi::OsStr; use std::ffi::OsStr;
use std::mem;
use tokio::process::{Child, Command}; use tokio::process::{Child, Command};
#[derive(Debug)] #[derive(Debug)]
@ -38,6 +39,17 @@ impl DaemonCli {
Ok(()) Ok(())
} }
/// Kills the running daemon process if there's one associated with the
/// daemon cli
#[tracing::instrument]
pub async fn stop_daemon(&mut self) -> DaemonResult<()> {
if let Some(mut child) = mem::take(&mut self.child) {
child.kill().await?;
}
Ok(())
}
/// Returns if the daemon is currently running /// Returns if the daemon is currently running
pub fn daemon_running(&mut self) -> bool { pub fn daemon_running(&mut self) -> bool {
if let Some(child) = &mut self.child { if let Some(child) = &mut self.child {
@ -76,6 +88,7 @@ impl DaemonCli {
) -> DaemonResult<Child> { ) -> DaemonResult<Child> {
Command::new(&self.daemon_path) Command::new(&self.daemon_path)
.args(args) .args(args)
.kill_on_drop(true)
.spawn() .spawn()
.map_err(DaemonError::from) .map_err(DaemonError::from)
} }

@ -19,6 +19,13 @@ pub async fn start_daemon(app_state: AppAccess<'_>, repo_path: String) -> Plugin
Ok(()) Ok(())
} }
#[tauri::command]
pub async fn stop_daemon(app_state: AppAccess<'_>, repo_path: String) -> PluginResult<()> {
app_state.stop_running_daemon(&repo_path).await?;
Ok(())
}
#[tauri::command] #[tauri::command]
pub async fn check_daemon_running(address: String) -> PluginResult<bool> { pub async fn check_daemon_running(address: String) -> PluginResult<bool> {
if let Ok(api_client) = ApiClient::connect(&address).await { if let Ok(api_client) = ApiClient::connect(&address).await {

@ -43,6 +43,7 @@ impl<R: Runtime> MediarepoPlugin<R> {
init_repository, init_repository,
start_daemon, start_daemon,
check_daemon_running, check_daemon_running,
stop_daemon,
]), ]),
} }
} }

@ -1,6 +1,7 @@
use crate::tauri_plugin::error::PluginResult; use crate::tauri_plugin::error::PluginResult;
use directories::ProjectDirs; use directories::ProjectDirs;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_piecewise_default::DeserializePiecewiseDefault;
use std::collections::HashMap; use std::collections::HashMap;
use std::fs; use std::fs;
use std::path::PathBuf; use std::path::PathBuf;
@ -14,7 +15,7 @@ pub struct Repository {
pub(crate) address: String, pub(crate) address: String,
} }
#[derive(Debug, Serialize, Deserialize)] #[derive(DeserializePiecewiseDefault, Debug, Serialize)]
pub struct Settings { pub struct Settings {
pub daemon_path: String, pub daemon_path: String,
pub repositories: HashMap<String, Repository>, pub repositories: HashMap<String, Repository>,

@ -148,4 +148,15 @@ impl AppState {
let mut daemons = self.running_daemons.write().await; let mut daemons = self.running_daemons.write().await;
daemons.insert(daemon.repo_path().to_owned(), daemon); daemons.insert(daemon.repo_path().to_owned(), daemon);
} }
/// Tries to stop a running daemon
pub async fn stop_running_daemon(&self, repo_path: &String) -> PluginResult<()> {
let mut daemons = self.running_daemons.write().await;
if let Some(mut daemon) = daemons.remove(repo_path) {
daemon.stop_daemon().await?;
}
Ok(())
}
} }

Loading…
Cancel
Save