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
+49 -24
View File
@@ -29,22 +29,38 @@ 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();
tokio::spawn(async move {
// Get config
let config = ModelsConfig::load().unwrap_or_default(); let config = ModelsConfig::load().unwrap_or_default();
// Check if config exists
let config_path = ModelsConfig::writable_config_path(); let config_path = ModelsConfig::writable_config_path();
let config_exists = config_path.exists();
// Check wine and terminal // Quick async file checks
let wine_ok = config.wine_executable.as_ref() let config_exists = tokio::task::spawn_blocking({
.map(|p| std::path::Path::new(p).exists()) let path = config_path.clone();
.unwrap_or(false); move || path.exists()
let term_ok = config.terminal_dir.as_ref() }).await.unwrap_or(false);
.map(|p| std::path::Path::new(p).is_dir())
.unwrap_or(false); let wine_ok = if let Some(wine) = &config.wine_executable {
let wine = wine.clone();
tokio::task::spawn_blocking(move || {
std::path::Path::new(&wine).exists()
}).await.unwrap_or(false)
} else {
false
};
let term_ok = if let Some(term) = &config.terminal_dir {
let term = term.clone();
tokio::task::spawn_blocking(move || {
std::path::Path::new(&term).is_dir()
}).await.unwrap_or(false)
} else {
false
};
let all_ok = config_exists && wine_ok && term_ok; let all_ok = config_exists && wine_ok && term_ok;
@@ -60,10 +76,24 @@ impl McpServer {
"Fix missing paths in config".to_string() "Fix missing paths in config".to_string()
}; };
AutoVerifyResult { let result = AutoVerifyResult {
all_ok, all_ok,
hint, hint,
config_path: config_path.to_string_lossy().to_string(), 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",
} }
}); });