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::*;
#[derive(Debug, Serialize, Clone, Copy)]
pub struct Pid(i64);
pub struct Pid(u32);
impl std::fmt::Display for Pid {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
@ -27,16 +27,16 @@ pub mod v2 {
impl Pid {
fn new() -> Self {
Pid(-1)
Pid(0)
}
fn new_from_output(pid: Option<Output>) -> Self {
let temp = {
match pid {
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 {
sysinfo::Pid::from_u32(self.0 as u32)
@ -88,41 +88,67 @@ pub mod v2 {
"stop" => {
if is_active(&self.name).await {
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.pid = Pid::new();
},
Err(er) => {
error!("Cannot stop process {} : {}", self.name, er);
},
}
}
},
"user-stop" => {
if is_active(&self.name).await {
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.pid = Pid::new();
},
Err(er) => {
error!("Cannot forcefully stop process {} : {}", self.name, er);
},
}
}
},
"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;
match freeze_process(&self.name).await {
Ok(_) => {
self.state = ProcessState::HoldingByCli;
self.pid = Pid::new();
},
Err(er) => {
error!("Cannot forcefully freeze process {} : {}", self.name, er);
},
}
}
},
"hold" => {
if !is_frozen(&self.name).await {
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.pid = Pid::new();
},
Err(er) => {
error!("Cannot freeze process {} : {}", self.name, er);
},
}
}
},
"restart" => {
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;
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);
}
},
_ => 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<()> {
terminate_process(&self.name).await?;
self.state = ProcessState::StoppedByCli;
self.pid = Pid::new();
Ok(())
}
pub async fn freeze_by_user_call(&mut self) -> anyhow::Result<()> {
@ -139,8 +166,9 @@ pub mod v2 {
Ok(())
}
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.pid = Pid(pid);
Ok(())
}
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`
///
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?;
tokio::time::sleep(Duration::from_millis(100)).await;
start_process(name, path).await
@ -399,18 +427,19 @@ pub async fn restart_process(name: &str, path: &str) -> anyhow::Result<()> {
///
/// *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 mut command = Command::new(path);
// command.arg(path);
match command.spawn() {
Ok(_) => {
Ok(child) => {
let pid = child.id();
warn!("Process {} is running now!", name);
Ok(())
Ok(pid)
}
Err(er) => {
Err(anyhow::Error::msg(format!("Cannot start process {} due to {}", name, er)))
Err(anyhow::Error::msg(format!("Cannot start process {} : {}", name, er)))
}
}
}