use crate::server::task::schedule_check_mcp_live;
use log::{debug, info, warn};
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use tokio::time::{Duration, interval};
pub async fn start_schedule_task() {
info!("Start the MCP service status check scheduled task");
let mut interval = interval(Duration::from_secs(20));
let is_running = Arc::new(AtomicBool::new(false));
tokio::spawn(async move {
loop {
interval.tick().await;
if is_running.load(Ordering::SeqCst) {
warn!(
"The last MCP service status check task has not been completed and this execution will be skipped."
);
continue;
}
is_running.store(true, Ordering::SeqCst);
debug!("Perform periodic checks on MCP service status...");
let is_running_clone = is_running.clone();
tokio::spawn(async move {
match tokio::time::timeout(
Duration::from_secs(10), schedule_check_mcp_live(),
)
.await
{
Ok(_) => {
debug!("MCP service status check completed");
}
Err(_) => {
warn!("MCP service status check task timed out");
}
}
is_running_clone.store(false, Ordering::SeqCst);
});
}
});
info!("MCP service status check scheduled task has been started");
}