diff --git a/noxis-rs/src/utils/prcs.rs b/noxis-rs/src/utils/prcs.rs index 3f10903..9e72d4e 100644 --- a/noxis-rs/src/utils/prcs.rs +++ b/noxis-rs/src/utils/prcs.rs @@ -13,10 +13,35 @@ pub mod v2 { use std::path::Path; use super::*; + + #[derive(Debug)] + struct Pid(i64); + + impl std::fmt::Display for Pid { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + return write!(f, "{}", self.0); + } +} + + impl Pid { + fn new() -> Self { + Pid(-1) + } + fn new_from_output(pid: Option) -> Self { + let temp = { + match pid { + Some(pid) => String::from_utf8_lossy(pid.stdout.trim_ascii()).into_owned(), + None => return Pid(-1), + } + }; + Pid(temp.parse::().unwrap_or_else(|_| { -1 })) + } + } #[derive(Debug)] pub struct ProcessesController { - name: Arc, + pub name: Arc, + pub pid : Pid, bin: String, // obj: Arc, state: ProcessState, @@ -34,6 +59,7 @@ pub mod v2 { pub fn new(name: &str, event_reader: MpscReciever) -> ProcessesController { ProcessesController { name : Arc::from(name), + pid : Pid::new(), bin : String::new(), state : ProcessState::Stopped, event_reader, @@ -55,6 +81,23 @@ pub mod v2 { info!("Event on {} `{}` for {}. Stopping ...", dep_type, dep_name, self.name); terminate_process(&self.name).await; self.state = ProcessState::Stopped; + self.pid = Pid::new(); + } + }, + "user-stop" => { + if is_active(&self.name).await { + info!("Event on {} `{}` for {}. Stopping ...", dep_type, "User Stop Call", self.name); + terminate_process(&self.name).await; + self.state = ProcessState::StoppedByCli; + self.pid = Pid::new(); + } + }, + "user-hold" => { + if is_active(&self.name).await { + info!("Event on {} `{}` for {}. Stopping ...", dep_type, "User Hold Call", self.name); + freeze_process(&self.name).await; + self.state = ProcessState::HoldingByCli; + self.pid = Pid::new(); } }, "hold" => { @@ -62,11 +105,14 @@ pub mod v2 { info!("Event on {} `{}` for {}. Freezing ...", dep_type, dep_name, self.name); freeze_process(&self.name).await; self.state = ProcessState::Holding; + self.pid = Pid::new(); } }, "restart" => { info!("Event on {} `{}` for {}. Restarting ...", dep_type, dep_name, self.name); let _ = restart_process(&self.name, &self.bin).await; + self.pid = Pid::new_from_output(get_pid(self.name.as_ref()).await); + info!("{}: New PID - {}", self.name, self.pid); }, _ => error!("Impermissible trigger in file-trigger for {}. Ignoring event ...", self.name), } @@ -85,6 +131,8 @@ pub mod v2 { error!("Cannot unfreeze process {} : {}", self.name, er); } else { self.state = ProcessState::Pending; + self.pid = Pid::new_from_output(get_pid(self.name.as_ref()).await); + info!("{}: New PID - {}", self.name, self.pid); } }, ProcessState::Stopped => { @@ -93,6 +141,8 @@ pub mod v2 { error!("Cannot start process {} : {}", self.name, er); } else { self.state = ProcessState::Pending; + self.pid = Pid::new_from_output(get_pid(self.name.as_ref()).await); + info!("{}: New PID - {}", self.name, self.pid); } }, _ => {},