fix(init): ensure runtime config is ready before core manager startup (#4942)

* fix(init): ensure runtime config is ready before core manager startup

* refactor: simplify verify_config_initialization function
This commit is contained in:
Sline
2025-10-06 16:51:47 +08:00
committed by GitHub
Unverified
parent f9bc739c51
commit 5ec5fdcfc7

View File

@@ -62,7 +62,12 @@ pub fn resolve_setup_async() {
init_once_auto_lightweight().await;
init_auto_lightweight_mode().await;
// 确保配置完全初始化后再启动核心管理器
init_verge_config().await;
// 添加配置验证,确保运行时配置已正确生成
verify_config_initialization().await;
init_core_manager().await;
init_system_proxy().await;
@@ -190,6 +195,81 @@ pub(super) async fn init_auto_lightweight_mode() {
logging_error!(Type::Setup, true, auto_lightweight_mode_init().await);
}
/// 验证配置初始化是否成功
async fn verify_config_initialization() {
logging!(
info,
Type::Setup,
true,
"Verifying config initialization..."
);
// 检查运行时配置是否已正确生成
if Config::runtime().await.latest_ref().config.is_some() {
logging!(
info,
Type::Setup,
true,
"Config initialization verified successfully"
);
return;
}
logging!(
warn,
Type::Setup,
true,
"Runtime config not found, regenerating..."
);
// 尝试重新生成配置最多3次
for attempt in 1..=3 {
logging!(
info,
Type::Setup,
true,
"Attempt {}/3 to regenerate config...",
attempt
);
match Config::generate().await {
Ok(_) => {
logging!(
info,
Type::Setup,
true,
"Config successfully regenerated on attempt {}",
attempt
);
return;
}
Err(e) => {
logging!(
warn,
Type::Setup,
true,
"Failed to generate config on attempt {}: {}",
attempt,
e
);
if attempt == 3 {
logging!(
error,
Type::Setup,
true,
"Failed to generate config after 3 attempts"
);
return;
}
// 等待一段时间再重试
tokio::time::sleep(tokio::time::Duration::from_millis(100 * attempt as u64)).await;
}
}
}
}
pub async fn init_work_config() {
logging!(
info,