fix(webdav/app): reset client on errors and improve app restart (#4941)

* fix(webdav/app): reset client on errors and improve app restart

* refactor: rm unused function
This commit is contained in:
Sline
2025-10-06 16:54:35 +08:00
committed by GitHub
parent 5ec5fdcfc7
commit b0decf824e
3 changed files with 45 additions and 9 deletions

View File

@@ -127,13 +127,24 @@ impl WebDavClient {
.set_auth(reqwest_dav::Auth::Basic(config.username, config.password))
.build()?;
// 尝试检查目录是否存在,如果不存在尝试创建,但创建失败不报错
// 尝试检查目录是否存在,如果不存在尝试创建
if client
.list(dirs::BACKUP_DIR, reqwest_dav::Depth::Number(0))
.await
.is_err()
{
let _ = client.mkcol(dirs::BACKUP_DIR).await;
match client.mkcol(dirs::BACKUP_DIR).await {
Ok(_) => log::info!("Successfully created backup directory"),
Err(e) => {
log::warn!("Failed to create backup directory: {}", e);
// 清除缓存,强制下次重新尝试
self.reset();
return Err(anyhow::Error::msg(format!(
"Failed to create backup directory: {}",
e
)));
}
}
}
// 缓存客户端

View File

@@ -20,6 +20,8 @@ pub async fn create_backup_and_upload_webdav() -> Result<()> {
.await
{
log::error!(target: "app", "Failed to upload to WebDAV: {err:#?}");
// 上传失败时重置客户端缓存
backup::WebDavClient::global().reset();
return Err(err);
}

View File

@@ -7,6 +7,8 @@ use crate::{
utils::{logging::Type, resolve},
};
use serde_yaml_ng::{Mapping, Value};
use std::env;
use std::process::{Command, exit};
/// Restart the Clash core
pub async fn restart_clash_core() {
@@ -27,19 +29,40 @@ pub async fn restart_app() {
// logging_error!(Type::Core, true, CoreManager::global().stop_core().await);
resolve::resolve_reset_async().await;
handle::Handle::global()
.app_handle()
.map(|app_handle| {
match handle::Handle::global().app_handle() {
Some(app_handle) => {
app_handle.restart();
})
.unwrap_or_else(|| {
}
None => {
logging_error!(
Type::System,
false,
"{}",
"Failed to get app handle for restart"
"Failed to get app handle for restart, attempting alternative restart method"
);
});
// Fallback: launch a new instance of the application and exit the current one
let current_exe = env::current_exe().unwrap_or_else(|_| {
exit(1); // Exit if can't find the executable path
});
let mut cmd = Command::new(current_exe);
cmd.args(env::args().skip(1));
match cmd.spawn() {
Ok(child) => {
log::info!(target: "app", "New application instance started with PID: {}", child.id());
// Successfully started new process, now exit current process
exit(0);
}
Err(e) => {
log::error!(target: "app", "Failed to start new application instance: {}", e);
// Unable to start new process, exit with error
exit(1);
}
}
}
}
}
fn after_change_clash_mode() {