mcp_stdio_proxy/server/task/
schedule_task.rs1use crate::server::task::schedule_check_mcp_live;
2use log::{debug, info, warn};
3use std::sync::Arc;
4use std::sync::atomic::{AtomicBool, Ordering};
5use tokio::time::{Duration, interval};
6
7pub async fn start_schedule_task() {
12 info!("启动MCP服务状态检查定时任务");
13
14 let mut interval = interval(Duration::from_secs(60));
16
17 let is_running = Arc::new(AtomicBool::new(false));
19
20 tokio::spawn(async move {
22 loop {
23 interval.tick().await;
25
26 if is_running.load(Ordering::SeqCst) {
28 warn!("上一次MCP服务状态检查任务尚未完成,跳过本次执行");
29 continue;
30 }
31
32 is_running.store(true, Ordering::SeqCst);
34
35 debug!("执行MCP服务状态定期检查...");
37
38 let is_running_clone = is_running.clone();
40 tokio::spawn(async move {
41 match tokio::time::timeout(
43 Duration::from_secs(25), schedule_check_mcp_live(),
45 )
46 .await
47 {
48 Ok(_) => {
49 debug!("MCP服务状态检查完成");
50 }
51 Err(_) => {
52 warn!("MCP服务状态检查任务超时");
53 }
54 }
55
56 is_running_clone.store(false, Ordering::SeqCst);
58 });
59 }
60 });
61
62 info!("MCP服务状态检查定时任务已启动");
63}