refactor: streamline SWR configuration and improve error handling in AppDataProvider

This commit is contained in:
xmk23333
2025-10-21 17:51:12 +08:00
parent bafe2ae164
commit 0e933597f5
18 changed files with 1139 additions and 1383 deletions

View File

@@ -1,6 +1,7 @@
use super::{IClashTemp, IProfiles, IRuntime, IVerge};
use crate::{
config::{PrfItem, profiles_append_item_safe},
constants::{files, timing},
core::{CoreManager, handle, validate::CoreConfigValidator},
enhance, logging,
utils::{Draft, dirs, help, logging::Type},
@@ -8,13 +9,9 @@ use crate::{
use anyhow::{Result, anyhow};
use backoff::{Error as BackoffError, ExponentialBackoff};
use std::path::PathBuf;
use std::time::Duration;
use tokio::sync::OnceCell;
use tokio::time::sleep;
pub const RUNTIME_CONFIG: &str = "clash-verge.yaml";
pub const CHECK_CONFIG: &str = "clash-verge-check.yaml";
pub struct Config {
clash_config: Draft<Box<IClashTemp>>,
verge_config: Draft<Box<IVerge>>,
@@ -123,20 +120,18 @@ impl Config {
Some(("config_validate::error", String::new()))
};
// 在单独的任务中发送通知
if let Some((msg_type, msg_content)) = validation_result {
sleep(Duration::from_secs(2)).await;
sleep(timing::STARTUP_ERROR_DELAY).await;
handle::Handle::notice_message(msg_type, &msg_content);
}
Ok(())
}
/// 将订阅丢到对应的文件中
pub async fn generate_file(typ: ConfigType) -> Result<PathBuf> {
let path = match typ {
ConfigType::Run => dirs::app_home_dir()?.join(RUNTIME_CONFIG),
ConfigType::Check => dirs::app_home_dir()?.join(CHECK_CONFIG),
ConfigType::Run => dirs::app_home_dir()?.join(files::RUNTIME_CONFIG),
ConfigType::Check => dirs::app_home_dir()?.join(files::CHECK_CONFIG),
};
let runtime = Config::runtime().await;
@@ -152,7 +147,6 @@ impl Config {
Ok(path)
}
/// 生成订阅存好
pub async fn generate() -> Result<()> {
let (config, exists_keys, logs) = enhance::enhance().await;
@@ -166,8 +160,6 @@ impl Config {
}
pub async fn verify_config_initialization() {
logging!(info, Type::Setup, "Verifying config initialization...");
let backoff_strategy = ExponentialBackoff {
initial_interval: std::time::Duration::from_millis(100),
max_interval: std::time::Duration::from_secs(2),
@@ -178,48 +170,15 @@ impl Config {
let operation = || async {
if Config::runtime().await.latest_ref().config.is_some() {
logging!(
info,
Type::Setup,
"Config initialization verified successfully"
);
return Ok::<(), BackoffError<anyhow::Error>>(());
}
logging!(
warn,
Type::Setup,
"Runtime config not found, attempting to regenerate..."
);
match Config::generate().await {
Ok(_) => {
logging!(info, Type::Setup, "Config successfully regenerated");
Ok(())
}
Err(e) => {
logging!(warn, Type::Setup, "Failed to generate config: {}", e);
Err(BackoffError::transient(e))
}
}
Config::generate().await
.map_err(BackoffError::transient)
};
match backoff::future::retry(backoff_strategy, operation).await {
Ok(_) => {
logging!(
info,
Type::Setup,
"Config initialization verified with backoff retry"
);
}
Err(e) => {
logging!(
error,
Type::Setup,
"Failed to verify config initialization after retries: {}",
e
);
}
if let Err(e) = backoff::future::retry(backoff_strategy, operation).await {
logging!(error, Type::Setup, "Config init verification failed: {}", e);
}
}
}