|
1 | 1 | use std::path::PathBuf; |
2 | 2 | use std::sync::Mutex; |
| 3 | +#[cfg(target_os = "windows")] |
| 4 | +use std::os::windows::process::CommandExt; |
3 | 5 | use tauri::Manager; |
4 | 6 | use tauri_plugin_shell::process::{CommandChild, CommandEvent}; |
5 | 7 | use tauri_plugin_shell::ShellExt; |
@@ -122,6 +124,39 @@ fn check_browser_availability() -> BrowserInfo { |
122 | 124 | detect_browsers() |
123 | 125 | } |
124 | 126 |
|
| 127 | +#[cfg(target_os = "windows")] |
| 128 | +const CREATE_NO_WINDOW: u32 = 0x0800_0000; |
| 129 | + |
| 130 | +/// Tue le worker local et toute son arborescence de sous-processus (Chromium |
| 131 | +/// lancés par nodriver notamment). Indispensable avant un remplacement sur |
| 132 | +/// disque de `goupix-vinted-worker.exe` (mise à jour NSIS) pour ne pas se |
| 133 | +/// retrouver avec un fichier verrouillé. |
| 134 | +fn kill_worker_tree(child: CommandChild) { |
| 135 | + #[cfg(target_os = "windows")] |
| 136 | + { |
| 137 | + let pid = child.pid(); |
| 138 | + let _ = std::process::Command::new("taskkill") |
| 139 | + .args(["/F", "/T", "/PID", &pid.to_string()]) |
| 140 | + .creation_flags(CREATE_NO_WINDOW) |
| 141 | + .status(); |
| 142 | + } |
| 143 | + let _ = child.kill(); |
| 144 | +} |
| 145 | + |
| 146 | +/// Commande exposée au front-end pour fermer proprement le worker avant une |
| 147 | +/// mise à jour Tauri (voir `DesktopUpdaterPanel.vue`). |
| 148 | +#[tauri::command] |
| 149 | +fn stop_local_worker(state: tauri::State<'_, VintedLocalChild>) -> Result<(), String> { |
| 150 | + let child_opt = match state.0.lock() { |
| 151 | + Ok(mut guard) => guard.take(), |
| 152 | + Err(_) => None, |
| 153 | + }; |
| 154 | + if let Some(child) = child_opt { |
| 155 | + kill_worker_tree(child); |
| 156 | + } |
| 157 | + Ok(()) |
| 158 | +} |
| 159 | + |
125 | 160 | #[cfg(debug_assertions)] |
126 | 161 | fn dev_repo_api_dir() -> Option<PathBuf> { |
127 | 162 | let manifest = PathBuf::from(env!("CARGO_MANIFEST_DIR")); |
@@ -276,7 +311,10 @@ pub fn run() { |
276 | 311 | .plugin(tauri_plugin_process::init()) |
277 | 312 | .plugin(tauri_plugin_shell::init()) |
278 | 313 | .manage(VintedLocalChild(Mutex::new(None))) |
279 | | - .invoke_handler(tauri::generate_handler![check_browser_availability]) |
| 314 | + .invoke_handler(tauri::generate_handler![ |
| 315 | + check_browser_availability, |
| 316 | + stop_local_worker |
| 317 | + ]) |
280 | 318 | .setup(|app| { |
281 | 319 | let handle = app.handle().clone(); |
282 | 320 | match spawn_worker(&handle) { |
@@ -304,7 +342,7 @@ pub fn run() { |
304 | 342 | if let tauri::RunEvent::Exit = event { |
305 | 343 | if let Ok(mut guard) = app.state::<VintedLocalChild>().0.lock() { |
306 | 344 | if let Some(child) = guard.take() { |
307 | | - let _ = child.kill(); |
| 345 | + kill_worker_tree(child); |
308 | 346 | } |
309 | 347 | } |
310 | 348 | } |
|
0 commit comments