#7. PId support + new status + todo
parent
551223dd91
commit
4b9db02528
|
|
@ -14,9 +14,34 @@ pub mod v2 {
|
||||||
|
|
||||||
use super::*;
|
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<Output>) -> Self {
|
||||||
|
let temp = {
|
||||||
|
match pid {
|
||||||
|
Some(pid) => String::from_utf8_lossy(pid.stdout.trim_ascii()).into_owned(),
|
||||||
|
None => return Pid(-1),
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Pid(temp.parse::<i64>().unwrap_or_else(|_| { -1 }))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct ProcessesController {
|
pub struct ProcessesController {
|
||||||
name: Arc<str>,
|
pub name: Arc<str>,
|
||||||
|
pub pid : Pid,
|
||||||
bin: String,
|
bin: String,
|
||||||
// obj: Arc<TrackingProcess>,
|
// obj: Arc<TrackingProcess>,
|
||||||
state: ProcessState,
|
state: ProcessState,
|
||||||
|
|
@ -34,6 +59,7 @@ pub mod v2 {
|
||||||
pub fn new(name: &str, event_reader: MpscReciever<Events>) -> ProcessesController {
|
pub fn new(name: &str, event_reader: MpscReciever<Events>) -> ProcessesController {
|
||||||
ProcessesController {
|
ProcessesController {
|
||||||
name : Arc::from(name),
|
name : Arc::from(name),
|
||||||
|
pid : Pid::new(),
|
||||||
bin : String::new(),
|
bin : String::new(),
|
||||||
state : ProcessState::Stopped,
|
state : ProcessState::Stopped,
|
||||||
event_reader,
|
event_reader,
|
||||||
|
|
@ -55,6 +81,23 @@ pub mod v2 {
|
||||||
info!("Event on {} `{}` for {}. Stopping ...", dep_type, dep_name, self.name);
|
info!("Event on {} `{}` for {}. Stopping ...", dep_type, dep_name, self.name);
|
||||||
terminate_process(&self.name).await;
|
terminate_process(&self.name).await;
|
||||||
self.state = ProcessState::Stopped;
|
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" => {
|
"hold" => {
|
||||||
|
|
@ -62,11 +105,14 @@ pub mod v2 {
|
||||||
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;
|
freeze_process(&self.name).await;
|
||||||
self.state = ProcessState::Holding;
|
self.state = ProcessState::Holding;
|
||||||
|
self.pid = Pid::new();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"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 _ = 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),
|
_ => 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);
|
error!("Cannot unfreeze process {} : {}", self.name, er);
|
||||||
} else {
|
} else {
|
||||||
self.state = ProcessState::Pending;
|
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 => {
|
ProcessState::Stopped => {
|
||||||
|
|
@ -93,6 +141,8 @@ pub mod v2 {
|
||||||
error!("Cannot start process {} : {}", self.name, er);
|
error!("Cannot start process {} : {}", self.name, er);
|
||||||
} else {
|
} else {
|
||||||
self.state = ProcessState::Pending;
|
self.state = ProcessState::Pending;
|
||||||
|
self.pid = Pid::new_from_output(get_pid(self.name.as_ref()).await);
|
||||||
|
info!("{}: New PID - {}", self.name, self.pid);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
_ => {},
|
_ => {},
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue