Fix: restart counting
This commit is contained in:
parent
b2707647d7
commit
9297451aa2
@ -10,4 +10,5 @@ pub struct PmrProcessInfo {
|
||||
pub program: String,
|
||||
pub workdir: String,
|
||||
pub args: Vec<String>,
|
||||
pub restarts: u32, // 重启次数
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ pub struct PmrProcess {
|
||||
pub program: String,
|
||||
pub args: Vec<String>,
|
||||
pub status: String,
|
||||
pub restarts: u32,
|
||||
}
|
||||
|
||||
#[derive(Tabled)]
|
||||
@ -71,6 +72,7 @@ pub fn read_pmr_processes() -> Vec<PmrProcess> {
|
||||
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(),
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user