From d92a215b1a06829f137c10dc9a4cb82cd10d11cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcin=20Papie=C5=BC?= Date: Tue, 12 Nov 2024 11:16:26 +0100 Subject: [PATCH] fix(ui): prevent from displaying cli window --- src-tauri/src/external_dependencies.rs | 16 ++++++++++++++++ src-tauri/src/process_adapter.rs | 4 ++-- src-tauri/src/process_killer.rs | 25 ++++++++++++++----------- src-tauri/src/process_watcher.rs | 2 +- src-tauri/src/xmrig_adapter.rs | 2 -- 5 files changed, 33 insertions(+), 16 deletions(-) diff --git a/src-tauri/src/external_dependencies.rs b/src-tauri/src/external_dependencies.rs index b5e841775..aa9c2b5ca 100644 --- a/src-tauri/src/external_dependencies.rs +++ b/src-tauri/src/external_dependencies.rs @@ -286,6 +286,22 @@ impl ExternalDependencies { ) -> Result<(), Error> { info!(target: LOG_TARGET, "Installing missing dependency: {}", missing_dependency.display_name); let installer_path = self.download_installer(&missing_dependency).await?; + + #[cfg(target_os = "windows")] + use crate::consts::PROCESS_CREATION_NO_WINDOW; + #[cfg(target_os = "windows")] + let mut thread = tokio::process::Command::new(installer_path) + .creation_flags(PROCESS_CREATION_NO_WINDOW) + .spawn() + .map_err(|e| { + anyhow!( + "Failed to start installer for {}: {}", + missing_dependency.display_name, + e + ) + })?; + + #[cfg(not(target_os = "windows"))] let mut thread = tokio::process::Command::new(installer_path) .spawn() .map_err(|e| { diff --git a/src-tauri/src/process_adapter.rs b/src-tauri/src/process_adapter.rs index c9dba93bc..417a1e7a2 100644 --- a/src-tauri/src/process_adapter.rs +++ b/src-tauri/src/process_adapter.rs @@ -41,13 +41,13 @@ pub(crate) trait ProcessAdapter { fn pid_file_name(&self) -> &str; - fn kill_previous_instances(&self, base_folder: PathBuf) -> Result<(), Error> { + async fn kill_previous_instances(&self, base_folder: PathBuf) -> Result<(), Error> { info!(target: LOG_TARGET, "Killing previous instances of {}", self.name()); match fs::read_to_string(base_folder.join(self.pid_file_name())) { Ok(pid) => match pid.trim().parse::() { Ok(pid) => { warn!(target: LOG_TARGET, "{} process did not shut down cleanly: {} pid file was created", pid, self.pid_file_name()); - kill_process(pid)?; + kill_process(pid).await?; } Err(e) => { let error_msg = diff --git a/src-tauri/src/process_killer.rs b/src-tauri/src/process_killer.rs index 45f2d65f6..55842a23d 100644 --- a/src-tauri/src/process_killer.rs +++ b/src-tauri/src/process_killer.rs @@ -1,17 +1,20 @@ -#[cfg(target_os = "windows")] -const LOG_TARGET: &str = "tari::universe::process_killer"; +use anyhow::Result; -pub fn kill_process(pid: i32) -> Result<(), anyhow::Error> { +pub async fn kill_process(pid: i32) -> Result<(), anyhow::Error> { #[cfg(target_os = "windows")] { - use log::warn; - use std::process::Command; - let output = Command::new("taskkill") - .args(["/F", "/PID", &pid.to_string()]) - .output()?; - if !output.status.success() { - warn!(target: LOG_TARGET, "Failed to kill process: {:?}", output); - } + use crate::consts::PROCESS_CREATION_NO_WINDOW; + use anyhow::Context; + let command = format!("taskkill /F /PID {}", pid); + + let mut child = tokio::process::Command::new("cmd") + .arg("/C") + .arg(command) + .creation_flags(PROCESS_CREATION_NO_WINDOW) + .spawn() + .context(format!("Failed to start taskkill for PID {}: {}", pid, pid))?; + + child.wait().await?; } #[cfg(not(target_os = "windows"))] diff --git a/src-tauri/src/process_watcher.rs b/src-tauri/src/process_watcher.rs index 4c36fd4a8..3e1561e99 100644 --- a/src-tauri/src/process_watcher.rs +++ b/src-tauri/src/process_watcher.rs @@ -39,7 +39,7 @@ impl ProcessWatcher { &mut self, base_path: PathBuf, ) -> Result<(), anyhow::Error> { - self.adapter.kill_previous_instances(base_path)?; + self.adapter.kill_previous_instances(base_path).await?; Ok(()) } diff --git a/src-tauri/src/xmrig_adapter.rs b/src-tauri/src/xmrig_adapter.rs index f5d225c14..9f03739d1 100644 --- a/src-tauri/src/xmrig_adapter.rs +++ b/src-tauri/src/xmrig_adapter.rs @@ -66,8 +66,6 @@ impl ProcessAdapter for XmrigAdapter { log_dir: PathBuf, binary_version_path: PathBuf, ) -> Result<(ProcessInstance, Self::StatusMonitor), anyhow::Error> { - self.kill_previous_instances(data_dir.clone())?; - let xmrig_shutdown = Shutdown::new(); let mut args = self .node_connection