perf: Make auto-verify non-blocking to prevent health check timeout

This commit is contained in:
Devid HW
2026-04-19 06:06:05 +07:00
parent 397f893bfc
commit 1ec0adb6e6
+69 -44
View File
@@ -29,41 +29,71 @@ impl McpServer {
} }
} }
/// Run verify_setup on initialization and return summary /// Run verify_setup in background - non blocking
async fn run_auto_verify(&self) -> AutoVerifyResult { fn spawn_auto_verify(&self) {
// Get config from tool_handler let result_arc = self.auto_verify_result.clone();
let config = ModelsConfig::load().unwrap_or_default();
// Check if config exists tokio::spawn(async move {
let config_path = ModelsConfig::writable_config_path(); // Get config
let config_exists = config_path.exists(); let config = ModelsConfig::load().unwrap_or_default();
let config_path = ModelsConfig::writable_config_path();
// Check wine and terminal
let wine_ok = config.wine_executable.as_ref() // Quick async file checks
.map(|p| std::path::Path::new(p).exists()) let config_exists = tokio::task::spawn_blocking({
.unwrap_or(false); let path = config_path.clone();
let term_ok = config.terminal_dir.as_ref() move || path.exists()
.map(|p| std::path::Path::new(p).is_dir()) }).await.unwrap_or(false);
.unwrap_or(false);
let wine_ok = if let Some(wine) = &config.wine_executable {
let all_ok = config_exists && wine_ok && term_ok; let wine = wine.clone();
tokio::task::spawn_blocking(move || {
let hint = if all_ok { std::path::Path::new(&wine).exists()
"Environment fully configured and ready".to_string() }).await.unwrap_or(false)
} else if !config_exists { } else {
format!("Auto-discovery will run on first request. Config will be written to {}", config_path.display()) false
} else if !wine_ok { };
"Wine/CrossOver not found - required for MT5 execution".to_string()
} else if !term_ok { let term_ok = if let Some(term) = &config.terminal_dir {
"MT5 directory not found - check installation".to_string() let term = term.clone();
} else { tokio::task::spawn_blocking(move || {
"Fix missing paths in config".to_string() std::path::Path::new(&term).is_dir()
}; }).await.unwrap_or(false)
} else {
AutoVerifyResult { false
all_ok, };
hint,
config_path: config_path.to_string_lossy().to_string(), let all_ok = config_exists && wine_ok && term_ok;
let hint = if all_ok {
"Environment fully configured and ready".to_string()
} else if !config_exists {
format!("Auto-discovery will run on first request. Config will be written to {}", config_path.display())
} else if !wine_ok {
"Wine/CrossOver not found - required for MT5 execution".to_string()
} else if !term_ok {
"MT5 directory not found - check installation".to_string()
} else {
"Fix missing paths in config".to_string()
};
let result = AutoVerifyResult {
all_ok,
hint,
config_path: config_path.to_string_lossy().to_string(),
};
// Store result
let mut guard = result_arc.lock().await;
*guard = Some(result);
});
}
/// Get current verify status (may be loading if called immediately after init)
async fn get_verify_status(&self) -> (Option<bool>, String) {
let guard = self.auto_verify_result.lock().await;
match guard.as_ref() {
Some(result) => (Some(result.all_ok), result.hint.clone()),
None => (None, "Checking environment...".to_string()),
} }
} }
@@ -83,23 +113,18 @@ impl McpServer {
"2024-11-05" "2024-11-05"
}; };
// Run auto-verify on first initialization // Start background verify (non-blocking)
let verify_result = self.run_auto_verify().await; self.spawn_auto_verify();
let all_ok = verify_result.all_ok;
let hint = verify_result.hint.clone();
// Store the result
*self.auto_verify_result.lock().await = Some(verify_result);
*self.initialized.lock().await = true; *self.initialized.lock().await = true;
// Include verify status in server info // Return immediately with fast status
let server_info = json!({ let server_info = json!({
"name": "MT5-Quant", "name": "MT5-Quant",
"version": "1.27.0", "version": "1.27.0",
"setup": { "setup": {
"verified": all_ok, "verified": null, // null = checking
"hint": hint, "hint": "Auto-verification running... Use verify_setup tool for detailed status",
} }
}); });