utils rework

pull/14/head
prplV 2024-12-10 18:22:16 +03:00
parent 1c6729daab
commit b6ecb10a77
2 changed files with 123 additions and 116 deletions

View File

@ -117,6 +117,7 @@ impl PrebootParams {
eprintln!("Local config file {} doesn't exist", &self.config.display());
return Err(Error::msg("Local Config Not Found. Cannot start"));
}
// redis server check
Ok(self)
}
}

View File

@ -4,8 +4,9 @@ pub mod metrics;
pub mod prcs;
pub mod services;
//
// TODO : saving current flags state
use crate::options::structs::CustomError;
use crate::options::structs::TrackingProcess;
use files::create_watcher;
use files::file_handler;
@ -60,17 +61,27 @@ pub async fn run_daemons(
loop {
let run_hand = running_handler(proc.clone(), tx.clone(), watchers_clone.clone());
tokio::select! {
_ = run_hand => {},
_ = run_hand => continue,
_val = rx.recv() => {
match _val.unwrap() {
if process_protocol_symbol(proc.clone(), _val.unwrap()).await.is_err() {
return;
}
},
}
tokio::task::yield_now().await;
}
}
async fn process_protocol_symbol(proc: Arc<TrackingProcess>, val: u8) -> Result<(), CustomError>{
match val {
// 1 - File-dependency handling error -> terminating (after waiting)
1 => {
if is_active(&proc.name).await {
error!("File-dependency handling error: Terminating {} process ..." , &proc.name);
terminate_process(&proc.name).await;
tokio::time::sleep(Duration::from_millis(100)).await;
tokio::time::sleep(Duration::from_millis(500)).await;
}
return;
// return;
},
// 2 - File-dependency handling error -> holding (after waiting)
2 => {
@ -83,7 +94,7 @@ pub async fn run_daemons(
// 3 - Running process error
3 => {
error!("Error due to starting {} process", &proc.name);
break;
return Err(CustomError::Fatal)
},
// 4 - Timeout of waiting service-dependency -> staying (after waiting)
4 => {
@ -95,7 +106,7 @@ pub async fn run_daemons(
if is_active(&proc.name).await {
error!("Timeout of waiting service-dependency: Terminating {} process ..." , &proc.name);
terminate_process(&proc.name).await;
tokio::time::sleep(Duration::from_millis(1000)).await;
tokio::time::sleep(Duration::from_millis(500)).await;
}
},
// 6 - Timeout of waiting service-dependency -> holding (after waiting)
@ -112,7 +123,7 @@ pub async fn run_daemons(
error!("File-dependency warning (file changed). Terminating {} process...", &proc.name);
terminate_process(&proc.name).await;
tokio::time::sleep(Duration::from_millis(100)).await;
return;
return Err(CustomError::Fatal)
},
// // 8 - File-dependency change -> restarting (after check)
8 => {
@ -149,14 +160,14 @@ pub async fn run_daemons(
if is_active(&proc.name).await {
terminate_process(&proc.name).await;
}
break;
return Err(CustomError::Fatal)
},
//
// 121 - Cannot create valid watcher for file dependency
121 => {
error!("Cannot create valid watcher for {}'s file dependency. Terminating thread...", proc.name);
let _ = terminate_process("runner-rs").await;
break;
return Err(CustomError::Fatal)
},
// 111 - global thread termination with killing current child in a face
// of a current process
@ -170,15 +181,10 @@ pub async fn run_daemons(
log::info!("Process {} is already terminated!", proc.name);
},
}
break;
},
_ => {},
}
},
}
tokio::task::yield_now().await;
}
tokio::task::yield_now().await;
Ok(())
}
// check process status daemon
/// # Fn `run_daemons`