PID bug fixed

migrate
prplV 2025-05-28 15:06:28 +03:00
parent bd5e21fce7
commit 3147c73006
1 changed files with 54 additions and 25 deletions

View File

@ -17,7 +17,7 @@ pub mod v2 {
use super::*; use super::*;
#[derive(Debug, Serialize, Clone, Copy)] #[derive(Debug, Serialize, Clone, Copy)]
pub struct Pid(i64); pub struct Pid(u32);
impl std::fmt::Display for Pid { impl std::fmt::Display for Pid {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
@ -27,16 +27,16 @@ pub mod v2 {
impl Pid { impl Pid {
fn new() -> Self { fn new() -> Self {
Pid(-1) Pid(0)
} }
fn new_from_output(pid: Option<Output>) -> Self { fn new_from_output(pid: Option<Output>) -> Self {
let temp = { let temp = {
match pid { match pid {
Some(pid) => String::from_utf8_lossy(pid.stdout.trim_ascii()).into_owned(), Some(pid) => String::from_utf8_lossy(pid.stdout.trim_ascii()).into_owned(),
None => return Pid(-1), None => return Pid(0),
} }
}; };
Pid(temp.parse::<i64>().unwrap_or_else(|_| { -1 })) Pid(temp.parse::<u32>().unwrap_or_else(|_| { 0 }))
} }
pub fn new_sysinfo_pid(&self) -> sysinfo::Pid { pub fn new_sysinfo_pid(&self) -> sysinfo::Pid {
sysinfo::Pid::from_u32(self.0 as u32) sysinfo::Pid::from_u32(self.0 as u32)
@ -88,41 +88,67 @@ pub mod v2 {
"stop" => { "stop" => {
if is_active(&self.name).await { if is_active(&self.name).await {
info!("Event on {} `{}` for {}. Stopping ...", dep_type, dep_name, self.name); info!("Event on {} `{}` for {}. Stopping ...", dep_type, dep_name, self.name);
let _ = terminate_process(&self.name).await; match terminate_process(&self.name).await {
Ok(_) => {
self.state = ProcessState::Stopped; self.state = ProcessState::Stopped;
self.pid = Pid::new(); self.pid = Pid::new();
},
Err(er) => {
error!("Cannot stop process {} : {}", self.name, er);
},
}
} }
}, },
"user-stop" => { "user-stop" => {
if is_active(&self.name).await { if is_active(&self.name).await {
info!("Event on {} `{}` for {}. Stopping ...", dep_type, "User Stop Call", self.name); info!("Event on {} `{}` for {}. Stopping ...", dep_type, "User Stop Call", self.name);
let _ = terminate_process(&self.name).await; match terminate_process(&self.name).await {
Ok(_) => {
self.state = ProcessState::StoppedByCli; self.state = ProcessState::StoppedByCli;
self.pid = Pid::new(); self.pid = Pid::new();
},
Err(er) => {
error!("Cannot forcefully stop process {} : {}", self.name, er);
},
}
} }
}, },
"user-hold" => { "user-hold" => {
if is_active(&self.name).await { if is_active(&self.name).await {
info!("Event on {} `{}` for {}. Stopping ...", dep_type, "User Hold Call", self.name); info!("Event on {} `{}` for {}. Stopping ...", dep_type, "User Hold Call", self.name);
freeze_process(&self.name).await; match freeze_process(&self.name).await {
Ok(_) => {
self.state = ProcessState::HoldingByCli; self.state = ProcessState::HoldingByCli;
self.pid = Pid::new(); self.pid = Pid::new();
},
Err(er) => {
error!("Cannot forcefully freeze process {} : {}", self.name, er);
},
}
} }
}, },
"hold" => { "hold" => {
if !is_frozen(&self.name).await { if !is_frozen(&self.name).await {
info!("Event on {} `{}` for {}. Freezing ...", dep_type, dep_name, self.name); info!("Event on {} `{}` for {}. Freezing ...", dep_type, dep_name, self.name);
freeze_process(&self.name).await; match freeze_process(&self.name).await {
Ok(_) => {
self.state = ProcessState::Holding; self.state = ProcessState::Holding;
self.pid = Pid::new(); self.pid = Pid::new();
},
Err(er) => {
error!("Cannot freeze process {} : {}", self.name, er);
},
}
} }
}, },
"restart" => { "restart" => {
info!("Event on {} `{}` for {}. Restarting ...", dep_type, dep_name, self.name); info!("Event on {} `{}` for {}. Restarting ...", dep_type, dep_name, self.name);
let _ = restart_process(&self.name, &self.bin).await; let pid = restart_process(&self.name, &self.bin).await;
sleep(Duration::from_millis(100)).await; sleep(Duration::from_millis(100)).await;
self.pid = Pid::new_from_output(get_pid(self.name.as_ref()).await); if let Ok(pid) = pid {
self.pid = Pid(pid);
info!("{}: New PID - {}", self.name, self.pid); info!("{}: New PID - {}", self.name, self.pid);
}
}, },
_ => error!("Impermissible trigger in file-trigger for {}. Ignoring event ...", self.name), _ => error!("Impermissible trigger in file-trigger for {}. Ignoring event ...", self.name),
} }
@ -131,6 +157,7 @@ pub mod v2 {
pub async fn stop_by_user_call(&mut self) -> anyhow::Result<()> { pub async fn stop_by_user_call(&mut self) -> anyhow::Result<()> {
terminate_process(&self.name).await?; terminate_process(&self.name).await?;
self.state = ProcessState::StoppedByCli; self.state = ProcessState::StoppedByCli;
self.pid = Pid::new();
Ok(()) Ok(())
} }
pub async fn freeze_by_user_call(&mut self) -> anyhow::Result<()> { pub async fn freeze_by_user_call(&mut self) -> anyhow::Result<()> {
@ -139,8 +166,9 @@ pub mod v2 {
Ok(()) Ok(())
} }
pub async fn start_by_user_call(&mut self) -> anyhow::Result<()> { pub async fn start_by_user_call(&mut self) -> anyhow::Result<()> {
start_process(&self.name, &self.bin).await?; let pid = start_process(&self.name, &self.bin).await?;
self.state = ProcessState::Pending; self.state = ProcessState::Pending;
self.pid = Pid(pid);
Ok(()) Ok(())
} }
pub async fn unfreeze_by_user_call(&mut self) -> anyhow::Result<()> { pub async fn unfreeze_by_user_call(&mut self) -> anyhow::Result<()> {
@ -380,7 +408,7 @@ pub async fn unfreeze_process(name: &str) -> anyhow::Result<()> {
/// ///
/// *depends on* : fn `start_process`, fn `terminate_process` /// *depends on* : fn `start_process`, fn `terminate_process`
/// ///
pub async fn restart_process(name: &str, path: &str) -> anyhow::Result<()> { pub async fn restart_process(name: &str, path: &str) -> anyhow::Result<u32> {
terminate_process(name).await?; terminate_process(name).await?;
tokio::time::sleep(Duration::from_millis(100)).await; tokio::time::sleep(Duration::from_millis(100)).await;
start_process(name, path).await start_process(name, path).await
@ -399,18 +427,19 @@ pub async fn restart_process(name: &str, path: &str) -> anyhow::Result<()> {
/// ///
/// *depends on* : - /// *depends on* : -
/// ///
pub async fn start_process(name: &str, path: &str) -> anyhow::Result<()> { pub async fn start_process(name: &str, path: &str) -> anyhow::Result<u32> {
// let runsh = format!("{} {}", "exec", path); // let runsh = format!("{} {}", "exec", path);
let mut command = Command::new(path); let mut command = Command::new(path);
// command.arg(path); // command.arg(path);
match command.spawn() { match command.spawn() {
Ok(_) => { Ok(child) => {
let pid = child.id();
warn!("Process {} is running now!", name); warn!("Process {} is running now!", name);
Ok(()) Ok(pid)
} }
Err(er) => { Err(er) => {
Err(anyhow::Error::msg(format!("Cannot start process {} due to {}", name, er))) Err(anyhow::Error::msg(format!("Cannot start process {} : {}", name, er)))
} }
} }
} }