From 9297451aa295f05023ec715237a6dcc528503b62 Mon Sep 17 00:00:00 2001 From: gaofei Date: Tue, 14 Jan 2025 12:20:38 +0800 Subject: [PATCH] Fix: restart counting --- src/base/process.rs | 1 + src/commands/list.rs | 8 +++++--- src/commands/restart.rs | 5 +++++ src/config/dump.rs | 44 ++++++++++++++++++++++++++++++++++++++++- 4 files changed, 54 insertions(+), 4 deletions(-) diff --git a/src/base/process.rs b/src/base/process.rs index 1906f80..ec0572a 100644 --- a/src/base/process.rs +++ b/src/base/process.rs @@ -10,4 +10,5 @@ pub struct PmrProcessInfo { pub program: String, pub workdir: String, pub args: Vec, + pub restarts: u32, // 重启次数 } diff --git a/src/commands/list.rs b/src/commands/list.rs index 8c36a1c..e64e62b 100644 --- a/src/commands/list.rs +++ b/src/commands/list.rs @@ -12,6 +12,7 @@ pub struct PmrProcess { pub program: String, pub args: Vec, pub status: String, + pub restarts: u32, } #[derive(Tabled)] @@ -71,6 +72,7 @@ pub fn read_pmr_processes() -> Vec { program: p.program, args: p.args, status: p.status, + restarts: p.restarts, }) .collect(), Err(e) => { @@ -125,7 +127,7 @@ pub fn list_processes(system: bool) { version: "N/A".to_string(), pid: p.pid.to_string(), uptime: time_to_readable(run_time), - restarts: "0".to_string(), + restarts: p.restarts.to_string(), status: "running".to_string(), cpu: format!("{:.1}%", sys_proc.cpu_usage()), mem: format!("{:.1} MB", sys_proc.memory() as f64 / 1024.0 / 1024.0), @@ -143,7 +145,7 @@ pub fn list_processes(system: bool) { version: "N/A".to_string(), pid: p.pid.to_string(), uptime: "0s".to_string(), - restarts: "0".to_string(), + restarts: p.restarts.to_string(), status: "stopped".to_string(), cpu: "0%".to_string(), mem: "0 MB".to_string(), @@ -159,7 +161,7 @@ pub fn list_processes(system: bool) { version: "N/A".to_string(), pid: "0".to_string(), uptime: "0s".to_string(), - restarts: "0".to_string(), + restarts: p.restarts.to_string(), status: "stopped".to_string(), cpu: "0%".to_string(), mem: "0 MB".to_string(), diff --git a/src/commands/restart.rs b/src/commands/restart.rs index 85bd6da..e7343a6 100644 --- a/src/commands/restart.rs +++ b/src/commands/restart.rs @@ -58,6 +58,11 @@ fn restart_existing_process(process: &PmrProcessInfo) { .update_process_status(process.pmr_id, pid, "running".to_string()) .expect("无法更新进程状态"); + // 增加重启次数 + dump_config + .increment_restarts(process.pmr_id) + .expect("无法更新重启次数"); + // 显示进程列表 list_processes(false); } diff --git a/src/config/dump.rs b/src/config/dump.rs index dcf8a52..972e555 100644 --- a/src/config/dump.rs +++ b/src/config/dump.rs @@ -29,7 +29,38 @@ impl DumpConfig { let dump_file = config_dir.join("dump.json"); let data = if dump_file.exists() { let file_contents = fs::read_to_string(&dump_file)?; - serde_json::from_str(&file_contents)? + // 使用 serde_json::Value 先解析JSON + let json: serde_json::Value = serde_json::from_str(&file_contents)?; + + // 手动构建进程列表 + let processes = if let Some(processes) = json.get("processes").and_then(|v| v.as_array()) { + processes + .iter() + .map(|p| PmrProcessInfo { + pmr_id: p["pmr_id"].as_u64().unwrap_or(0) as u32, + pid: p["pid"].as_u64().unwrap_or(0) as u32, + name: p["name"].as_str().unwrap_or("").to_string(), + namespace: p["namespace"].as_str().unwrap_or("").to_string(), + status: p["status"].as_str().unwrap_or("").to_string(), + program: p["program"].as_str().unwrap_or("").to_string(), + workdir: p["workdir"].as_str().unwrap_or("").to_string(), + args: p["args"] + .as_array() + .map(|a| { + a.iter() + .filter_map(|v| v.as_str()) + .map(String::from) + .collect() + }) + .unwrap_or_default(), + restarts: p["restarts"].as_u64().unwrap_or(0) as u32, + }) + .collect() + } else { + Vec::new() + }; + + DumpData { processes } } else { let initial_data = DumpData { processes: Vec::new(), @@ -75,6 +106,7 @@ impl DumpConfig { workdir, program, args, + restarts: 0, // 初始化重启次数为0 }); self.save_data(&data) @@ -101,4 +133,14 @@ impl DumpConfig { Ok(()) } } + + pub fn increment_restarts(&self, pmr_id: u32) -> io::Result<()> { + let mut data = self.data.lock().unwrap(); + if let Some(process) = data.processes.iter_mut().find(|p| p.pmr_id == pmr_id) { + process.restarts = process.restarts.saturating_add(1); + self.save_data(&data) + } else { + Ok(()) + } + } }