From 61662f92a18276fdc7e5fa8a7f06c27ef87fdffe Mon Sep 17 00:00:00 2001 From: Jonson Petard Date: Sun, 7 Apr 2024 18:07:36 +0800 Subject: [PATCH] fix: restart application not work --- backend/tauri/src/cmds.rs | 31 ++++++++++++++---------- backend/tauri/src/core/commands/mod.rs | 14 +++++++++++ backend/tauri/src/core/tray/mod.rs | 2 +- backend/tauri/src/main.rs | 1 + backend/tauri/src/utils/help.rs | 16 +++++++++--- src/components/setting/setting-verge.tsx | 4 +-- src/services/cmds.ts | 4 +++ 7 files changed, 53 insertions(+), 19 deletions(-) diff --git a/backend/tauri/src/cmds.rs b/backend/tauri/src/cmds.rs index fa8c7afe82..fa484bedbc 100644 --- a/backend/tauri/src/cmds.rs +++ b/backend/tauri/src/cmds.rs @@ -390,13 +390,13 @@ pub async fn set_custom_app_dir(app_handle: tauri::AppHandle, path: String) -> C let path = PathBuf::from(path); // show a dialog to ask whether to migrate the data - let res = tauri::async_runtime::spawn_blocking(move || { - let msg = t!("dialog.custom_app_dir_migrate", path = path_str).to_string(); + let res = + tauri::async_runtime::spawn_blocking(move || { + let msg = t!("dialog.custom_app_dir_migrate", path = path_str).to_string(); - if migrate_dialog(&msg) { - let app_exe = tauri::utils::platform::current_exe()?; - let app_exe = dunce::canonicalize(app_exe)?.to_string_lossy().to_string(); - std::thread::spawn(move || { + if migrate_dialog(&msg) { + let app_exe = tauri::utils::platform::current_exe()?; + let app_exe = dunce::canonicalize(app_exe)?.to_string_lossy().to_string(); std::process::Command::new("powershell") .arg("-Command") .arg( @@ -408,17 +408,22 @@ pub async fn set_custom_app_dir(app_handle: tauri::AppHandle, path: String) -> C .as_str(), ).spawn().unwrap(); utils::help::quit_application(&app_handle); - }); - } else { - set_app_dir(&path)?; - } - Ok::<_, anyhow::Error>(()) - }) - .await; + } else { + set_app_dir(&path)?; + } + Ok::<_, anyhow::Error>(()) + }) + .await; wrap_err!(wrap_err!(res)?)?; Ok(()) } +#[tauri::command] +pub fn restart_application(app_handle: tauri::AppHandle) -> CmdResult { + crate::utils::help::restart_application(&app_handle); + Ok(()) +} + #[cfg(not(windows))] #[tauri::command] pub async fn set_custom_app_dir(_path: String) -> CmdResult { diff --git a/backend/tauri/src/core/commands/mod.rs b/backend/tauri/src/core/commands/mod.rs index 51d51a2898..0cd93cd4cd 100644 --- a/backend/tauri/src/core/commands/mod.rs +++ b/backend/tauri/src/core/commands/mod.rs @@ -14,14 +14,28 @@ enum Commands { MigrateHomeDir { target_path: String }, } +struct DelayedExitGuard; +impl DelayedExitGuard { + pub fn new() -> Self { + Self + } +} +impl Drop for DelayedExitGuard { + fn drop(&mut self) { + std::thread::sleep(std::time::Duration::from_secs(5)); + } +} + pub fn parse() -> anyhow::Result<()> { let cli = Cli::parse(); if let Some(commands) = &cli.command { + let guard = DelayedExitGuard::new(); match commands { Commands::MigrateHomeDir { target_path } => { self::handler::migrate_home_dir_handler(target_path).unwrap(); } } + drop(guard); std::process::exit(0); } Ok(()) // bypass diff --git a/backend/tauri/src/core/tray/mod.rs b/backend/tauri/src/core/tray/mod.rs index 777114f581..a9f412f244 100644 --- a/backend/tauri/src/core/tray/mod.rs +++ b/backend/tauri/src/core/tray/mod.rs @@ -151,7 +151,7 @@ impl Tray { "open_core_dir" => crate::log_err!(cmds::open_core_dir()), "open_logs_dir" => crate::log_err!(cmds::open_logs_dir()), "restart_clash" => feat::restart_clash_core(), - "restart_app" => api::process::restart(&app_handle.env()), + "restart_app" => utils::help::restart_application(app_handle), "quit" => { utils::help::quit_application(app_handle); } diff --git a/backend/tauri/src/main.rs b/backend/tauri/src/main.rs index ebb87e02a6..6633fe5f77 100644 --- a/backend/tauri/src/main.rs +++ b/backend/tauri/src/main.rs @@ -179,6 +179,7 @@ fn main() -> std::io::Result<()> { cmds::get_proxies, cmds::select_proxy, cmds::update_proxy_provider, + cmds::restart_application, ]); #[cfg(target_os = "macos")] diff --git a/backend/tauri/src/utils/help.rs b/backend/tauri/src/utils/help.rs index 642d64314e..029852fd99 100644 --- a/backend/tauri/src/utils/help.rs +++ b/backend/tauri/src/utils/help.rs @@ -216,17 +216,27 @@ pub fn get_max_scale_factor() -> f64 { } #[instrument(skip(app_handle))] -pub fn quit_application(app_handle: &AppHandle) { +fn cleanup_processes(app_handle: &AppHandle) { let _ = super::resolve::save_window_state(app_handle, true); - super::resolve::resolve_reset(); tauri::api::process::kill_children(); - app_handle.exit(0); // flush all data to disk crate::core::storage::Storage::global().destroy().unwrap(); +} + +#[instrument(skip(app_handle))] +pub fn quit_application(app_handle: &AppHandle) { + cleanup_processes(app_handle); + app_handle.exit(0); std::process::exit(0); } +#[instrument(skip(app_handle))] +pub fn restart_application(app_handle: &AppHandle) { + cleanup_processes(app_handle); + tauri::api::process::restart(&app_handle.env()); +} + #[macro_export] macro_rules! error { ($result: expr) => { diff --git a/src/components/setting/setting-verge.tsx b/src/components/setting/setting-verge.tsx index bd507e85e4..d7c0cc3a80 100644 --- a/src/components/setting/setting-verge.tsx +++ b/src/components/setting/setting-verge.tsx @@ -7,6 +7,7 @@ import { openAppDir, openCoreDir, openLogsDir, + restartApplication, setCustomAppDir, } from "@/services/cmds"; import { sleep } from "@/utils"; @@ -22,7 +23,6 @@ import { Typography, } from "@mui/material"; import { open } from "@tauri-apps/api/dialog"; -import { relaunch } from "@tauri-apps/api/process"; import { checkUpdate } from "@tauri-apps/api/updater"; import { useAsyncEffect, useLockFn } from "ahooks"; import { useRef, useState } from "react"; @@ -123,7 +123,7 @@ const SettingVerge = ({ onError }: Props) => { body: t("App directory changed successfully"), }); await sleep(1000); - await relaunch(); + await restartApplication(); } catch (err: any) { useMessage(err.message || err.toString(), { title: t("Error"), diff --git a/src/services/cmds.ts b/src/services/cmds.ts index fd825604a7..4834f9d06a 100644 --- a/src/services/cmds.ts +++ b/src/services/cmds.ts @@ -264,3 +264,7 @@ export async function getCustomAppDir() { export async function setCustomAppDir(path: string) { return invoke("set_custom_app_dir", { path }); } + +export async function restartApplication() { + return invoke("restart_application"); +}