Skip to content

Commit

Permalink
fix: restart application not work
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat616 committed Apr 7, 2024
1 parent d7bb676 commit 61662f9
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 19 deletions.
31 changes: 18 additions & 13 deletions backend/tauri/src/cmds.rs
Expand Up @@ -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(
Expand All @@ -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 {
Expand Down
14 changes: 14 additions & 0 deletions backend/tauri/src/core/commands/mod.rs
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion backend/tauri/src/core/tray/mod.rs
Expand Up @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions backend/tauri/src/main.rs
Expand Up @@ -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")]
Expand Down
16 changes: 13 additions & 3 deletions backend/tauri/src/utils/help.rs
Expand Up @@ -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) => {
Expand Down
4 changes: 2 additions & 2 deletions src/components/setting/setting-verge.tsx
Expand Up @@ -7,6 +7,7 @@ import {
openAppDir,
openCoreDir,
openLogsDir,
restartApplication,
setCustomAppDir,
} from "@/services/cmds";
import { sleep } from "@/utils";
Expand All @@ -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";
Expand Down Expand Up @@ -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"),
Expand Down
4 changes: 4 additions & 0 deletions src/services/cmds.ts
Expand Up @@ -264,3 +264,7 @@ export async function getCustomAppDir() {
export async function setCustomAppDir(path: string) {
return invoke<void>("set_custom_app_dir", { path });
}

export async function restartApplication() {
return invoke<void>("restart_application");
}

0 comments on commit 61662f9

Please sign in to comment.