From da0841b836a42aa64f81cf331e0ab82712b35240 Mon Sep 17 00:00:00 2001 From: ravenclaw900 <50060110+ravenclaw900@users.noreply.github.com> Date: Fri, 5 Nov 2021 17:22:26 -0500 Subject: [PATCH] fix(processes): stop NoSuchProcess error --- src/backend/src/systemdata.rs | 47 +++++++++++++++++++++++------------ 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/src/backend/src/systemdata.rs b/src/backend/src/systemdata.rs index 18d0ade1..f40cfe92 100644 --- a/src/backend/src/systemdata.rs +++ b/src/backend/src/systemdata.rs @@ -122,32 +122,47 @@ pub async fn processes() -> Vec { } thread::sleep(time::Duration::from_millis(500)); for element in processes { - // Name could fail if the process terminates, if so skip the process + let pid = element.pid(); + // Everything could fail if the process terminates, if so skip the process let name; match element.name().await { Ok(unwrapped_name) => name = unwrapped_name, Err(_) => continue, } let status: String; - match element.status().await.unwrap() { - // The proceses that are running show up as sleeping, for some reason - process::Status::Sleeping => status = "running".to_string(), - process::Status::Idle => status = "idle".to_string(), - process::Status::Stopped => status = "stopped".to_string(), - process::Status::Zombie => status = "zombie".to_string(), - process::Status::Dead => status = "dead".to_string(), - _ => status = String::new(), + match element.status().await { + Ok(unwrapped_status) => match unwrapped_status { + // The proceses that are running show up as sleeping, for some reason + process::Status::Sleeping => status = "running".to_string(), + process::Status::Idle => status = "idle".to_string(), + process::Status::Stopped => status = "stopped".to_string(), + process::Status::Zombie => status = "zombie".to_string(), + process::Status::Dead => status = "dead".to_string(), + _ => status = String::new(), + }, + Err(_) => continue, + } + let cpu: f32; + match element.cpu_usage().await { + Ok(unwrapped_cpu) => { + cpu = round_percent( + (unwrapped_cpu - cpu_list.remove(&pid).unwrap()) + .get::() + .into(), + ); + } + Err(_) => continue, + } + let ram: u64; + match element.memory().await { + Ok(unwrapped_mem) => ram = unwrapped_mem.vms().get::(), + Err(_) => continue, } - let pid = element.pid(); process_list.push(types::ProcessData { pid, name, - cpu: round_percent( - (element.cpu_usage().await.unwrap() - cpu_list.remove(&pid).unwrap()) - .get::() - .into(), - ), - ram: element.memory().await.unwrap().vms().get::(), + cpu, + ram, status, }); }