From 6d97f463443823a060d77424990ce5d9cc238c29 Mon Sep 17 00:00:00 2001 From: nsosio Date: Thu, 2 Nov 2023 16:08:35 +0100 Subject: [PATCH 1/3] added SIGTERM before SIGKILL on stop swarm processes --- src-tauri/src/swarm.rs | 54 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 44 insertions(+), 10 deletions(-) diff --git a/src-tauri/src/swarm.rs b/src-tauri/src/swarm.rs index f652599c..c4c2f661 100644 --- a/src-tauri/src/swarm.rs +++ b/src-tauri/src/swarm.rs @@ -1,6 +1,13 @@ use reqwest::get; use serde::Deserialize; -use std::{collections::HashMap, env, path::PathBuf, str}; +use std::{ + collections::HashMap, + env, + path::PathBuf, + str, + thread::{self, JoinHandle}, + time::Duration, +}; use tauri::api::process::Command; #[derive(Deserialize)] @@ -207,22 +214,49 @@ pub fn get_swarm_processes() -> String { output_value } -#[tauri::command] +#[tauri::command(async)] pub fn stop_swarm_mode() { println!("🛑 Stopping the Swarm..."); let processes = get_swarm_processes().replace("\n", " "); println!("🛑 Stopping Processes: {}", processes); - let processes = processes.split(" ").collect::>(); + let processes: Vec = processes + .split(" ") + .collect::>() + .into_iter() + .filter_map(|s| s.parse::().ok()) + .collect(); for process in processes { - println!("🛑 Stopping Process: {}", process); let _ = Command::new("kill") - .args(&[process.to_string()]) - .output() - .map_err(|e| { - println!("🙈 Failed to execute command: {}", e); - e - }); + .args(["-s", "SIGTERM", &process.to_string()]) + .spawn() + .expect("🙈 Failed to execute kill command with SIGTERM"); + + let handle: JoinHandle<_> = thread::spawn(move || { + thread::sleep(Duration::from_millis(100)); + match Command::new("ps") + .args(["-p", &process.to_string()]) + .output() + { + Ok(output) => match output.status.code() { + Some(0) => true, + _ => false, + }, + Err(e) => { + eprintln!("Error executing ps command: {}", e); + false + } + } + }); + if handle.join().unwrap() { + let _ = Command::new("kill") + .args(["-s", "SIGKILL", &process.to_string()]) + .output() + .expect("🙈 Failed to execute kill command with SIGKILL"); + println!("🛑 Stopping Process with SIGKILL: {}", process); + } else { + println!("🛑 Stopping Process with SIGTERM: {}", process); + } } println!("🛑 Stopped all the Swarm Processes."); } From b134698665cbf14e0f2cd9c4802e7188646e6ea1 Mon Sep 17 00:00:00 2001 From: nsosio Date: Thu, 2 Nov 2023 16:19:39 +0100 Subject: [PATCH 2/3] improved get processes --- src-tauri/src/swarm.rs | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/src-tauri/src/swarm.rs b/src-tauri/src/swarm.rs index c4c2f661..ca64abe5 100644 --- a/src-tauri/src/swarm.rs +++ b/src-tauri/src/swarm.rs @@ -95,13 +95,10 @@ pub async fn get_petals_models() -> Result, String> { #[tauri::command] pub fn is_swarm_mode_running() -> bool { - let output_value = get_swarm_processes(); + let processes = get_swarm_processes(); - if !output_value.is_empty() { - println!( - "🏃‍♀️ Processeses running: {}", - output_value.replace("\n", " ") - ); + if processes.len() > 0 { + println!("🏃‍♀️ Processeses running: {:?}", processes); return true; } return false; @@ -172,7 +169,7 @@ fn get_petals_path(handle: tauri::AppHandle) -> String { petals_path.to_string() } -pub fn get_swarm_processes() -> String { +pub fn get_swarm_processes() -> Vec { // Check if create_env.sh is running let output = Command::new("/usr/bin/pgrep") .args(&["-f", "create_env.sh|(mamba|conda).*create.*prem_app"]) @@ -186,7 +183,7 @@ pub fn get_swarm_processes() -> String { // If create_env.sh is running, return an empty string if !output_value.is_empty() { - return "".to_string(); + return vec![]; } let config = Config::new(); @@ -211,20 +208,21 @@ pub fn get_swarm_processes() -> String { }); let output_value = output.unwrap().stdout; - output_value -} - -#[tauri::command(async)] -pub fn stop_swarm_mode() { - println!("🛑 Stopping the Swarm..."); - let processes = get_swarm_processes().replace("\n", " "); - println!("🛑 Stopping Processes: {}", processes); - let processes: Vec = processes + let processes: Vec = output_value + .replace("\n", " ") .split(" ") .collect::>() .into_iter() .filter_map(|s| s.parse::().ok()) .collect(); + processes +} + +#[tauri::command(async)] +pub fn stop_swarm_mode() { + println!("🛑 Stopping the Swarm..."); + let processes = get_swarm_processes(); + println!("🛑 Stopping Processes: {:?}", processes); for process in processes { let _ = Command::new("kill") @@ -233,7 +231,7 @@ pub fn stop_swarm_mode() { .expect("🙈 Failed to execute kill command with SIGTERM"); let handle: JoinHandle<_> = thread::spawn(move || { - thread::sleep(Duration::from_millis(100)); + thread::sleep(Duration::from_millis(50)); match Command::new("ps") .args(["-p", &process.to_string()]) .output() From ecb4ad73a9698ec604d38b2323c8a3051598c8ad Mon Sep 17 00:00:00 2001 From: nsosio Date: Thu, 2 Nov 2023 16:25:31 +0100 Subject: [PATCH 3/3] removed async --- src-tauri/src/swarm.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src-tauri/src/swarm.rs b/src-tauri/src/swarm.rs index ca64abe5..8f08e459 100644 --- a/src-tauri/src/swarm.rs +++ b/src-tauri/src/swarm.rs @@ -218,7 +218,7 @@ pub fn get_swarm_processes() -> Vec { processes } -#[tauri::command(async)] +#[tauri::command] pub fn stop_swarm_mode() { println!("🛑 Stopping the Swarm..."); let processes = get_swarm_processes();