diff --git a/settings.json b/settings.json index 86ff13d..8d1b56d 100644 --- a/settings.json +++ b/settings.json @@ -57,8 +57,8 @@ "hostname" : "localhost", "port" : 8080, "triggers" : { - "wait" : 20, - "delay" : 5, + "wait" : 10, + "delay" : 2, "onLost" : "stop" } }] diff --git a/src/main.rs b/src/main.rs index 08f7bbd..4f940f8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ use serde::{Deserialize, Serialize}; use serde_json; use tokio::io::Join; +use tokio::join; use std::env::join_paths; use std::fmt::Debug; use std::fs; @@ -108,14 +109,14 @@ async fn run_daemons( ) { loop { - let run_hand = running_handler(&proc.name, &proc.path, tx.clone()); - let file_hand = file_handler(&proc.name,&proc.dependencies.files, tx.clone()); - let serv_hand = service_handler(&proc.name, &proc.dependencies.services, tx.clone()); + let run_hand = running_handler(&proc, tx.clone()); + // let file_hand = file_handler(&proc.name,&proc.dependencies.files, tx.clone()); + // let serv_hand = service_handler(&proc.name, &proc.dependencies.services, tx.clone()); tokio::select! { _ = run_hand => {}, - _ = file_hand => {}, - _ = serv_hand => {}, + // _ = file_hand => {}, + // _ = serv_hand => {}, _val = rx.recv() => { match _val.unwrap() { // 1 - File-dependency handling error -> terminating (after waiting) @@ -246,26 +247,39 @@ async fn start_process(name: &str, path: &str) -> Result<(), CustomError> { } } // check process status daemon -async fn running_handler(name: &str, path: &str, tx: mpsc::Sender){ +async fn running_handler(prc: &TrackingProcess, tx: mpsc::Sender){ // println!("running daemon on {}", name); + loop { let _ = Command::new("pidof") - .arg(name) + .arg(&prc.name) .output() .expect("Failed to execute command 'pidof'"); // services and files check (once) - // if inactive -> + let files_check = file_handler(&prc.name, &prc.dependencies.files, tx.clone()); + let services_check = service_handler(&prc.name, &prc.dependencies.services, tx.clone()); + + join!(files_check, services_check); - if !is_active(name) && !is_frozen(name){ - if start_process(name, path).await.is_err() { + // if inactive -> spawn checks -> active is true + + if !is_active(&prc.name){ + if start_process(&prc.name, &prc.path).await.is_err() { tx.send(3).await.unwrap(); // return; } } + + // if frozen -> spawn checks -> unfreeze is true + + else if is_frozen(&prc.name){ + + } tokio::time::sleep(Duration::from_millis(100)).await; + tokio::task::yield_now().await; + } } async fn file_handler(name: &str, files: &Vec, tx: mpsc::Sender) { - loop { // println!("file daemon on {}", name); if is_active(name) && !is_frozen(name){ for file in files { @@ -300,7 +314,6 @@ async fn file_handler(name: &str, files: &Vec, tx: mpsc::Sender) { } tokio::time::sleep(Duration::from_millis(100)).await; tokio::task::yield_now().await; - } } fn check_file(filename: &str, path: &str) -> Result<(), CustomError> { let fileconcat = format!("{}{}", path, filename);