diff --git a/settings.json b/settings.json index b877d66..4c3d125 100644 --- a/settings.json +++ b/settings.json @@ -41,7 +41,7 @@ "filename" : "control-file", "src" : "/home/vladislav/web/", "triggers" : { - "onDelete" : "stop", + "onDelete" : "hold", "onChange" : "hold" } }], diff --git a/src/main.rs b/src/main.rs index 3a2984a..7f662df 100644 --- a/src/main.rs +++ b/src/main.rs @@ -79,7 +79,7 @@ async fn main() { let mut handler: Vec> = vec![]; for proc in processes.processes.iter() { - println!("\nProcess '{}' on stage:\n{}\nDepends on {} file(s), {} service(s)\n", + println!("Process '{}' on stage:\n{}\nDepends on {} file(s), {} service(s)\n", proc.name, proc.path, proc.dependencies.files.len(), @@ -263,10 +263,10 @@ async fn start_process(name: &str, path: &str) -> Result<(), CustomError> { async fn running_handler(prc: &TrackingProcess, tx: mpsc::Sender){ // println!("running daemon on {}", prc.name); // loop { - let _ = Command::new("pidof") - .arg(&prc.name) - .output() - .expect("Failed to execute command 'pidof'"); + // let _ = Command::new("pidof") + // .arg(&prc.name) + // .output() + // .expect("Failed to execute command 'pidof'"); // services and files check (once) let files_check = file_handler(&prc.name, &prc.dependencies.files, tx.clone()); let services_check = service_handler(&prc.name, &prc.dependencies.services, tx.clone()); @@ -293,6 +293,9 @@ async fn file_handler(name: &str, files: &Vec, tx: mpsc::Sender) -> R // println!("file daemon on {}", name); for file in files { if check_file(&file.filename, &file.src).is_err() { + if !is_active(name) || is_frozen(name) { + return Err(CustomError::Fatal); + } match file.triggers.on_delete.as_str() { "stay" => { continue; @@ -301,12 +304,10 @@ async fn file_handler(name: &str, files: &Vec, tx: mpsc::Sender) -> R if is_active(name) { tx.send(1).await.unwrap(); } - tokio::time::sleep(Duration::from_millis(50)).await; return Err(CustomError::Fatal); }, "hold" => { if is_active(name) { - tokio::time::sleep(Duration::from_millis(50)).await; tx.send(2).await.unwrap(); return Err(CustomError::Fatal); } @@ -336,15 +337,21 @@ fn check_file(filename: &str, path: &str) -> Result<(), CustomError> { // ?? async fn service_handler(name: &str, services: &Vec, tx: mpsc::Sender) -> Result<(), CustomError> { // println!("service daemon on {}", name); + // let state = is_active(name); + // let condition = is_frozen(name); for serv in services { if check_service(&serv.hostname, &serv.port).await.is_err() { - // println!("Service {}:{} is unreachable for process {}", &serv.hostname, &serv.port, &name); + if !is_active(name) || is_frozen(name) { + return Err(CustomError::Fatal); + } + println!("Service {}:{} is unreachable for process {}", &serv.hostname, &serv.port, &name); match serv.triggers.on_lost.as_str() { "stay" => { }, "stop" => { if looped_service_connecting(name, serv).await.is_err() { tx.send(5).await.unwrap(); + tokio::time::sleep(Duration::from_millis(100)).await; return Err(CustomError::Fatal); } }, @@ -354,6 +361,7 @@ async fn service_handler(name: &str, services: &Vec, tx: mpsc::Sender< } if looped_service_connecting(name, serv).await.is_err() { tx.send(6).await.unwrap(); + tokio::time::sleep(Duration::from_millis(100)).await; return Err(CustomError::Fatal); } }, @@ -383,6 +391,7 @@ async fn looped_service_connecting( println!("Attempting to connect from {} process to {}:{}",&name, &serv.hostname, &serv.port ); match check_service(&serv.hostname, &serv.port).await { Ok(_) => { + println!("SUCCESS!"); break; }, Err(_) => { @@ -398,6 +407,7 @@ async fn looped_service_connecting( println!("Attempting to connect from {} process to {}:{}",&name, &serv.hostname, &serv.port ); match check_service(&serv.hostname, &serv.port).await { Ok(_) => { + println!("SUCCESS!"); return Ok(()); }, Err(_) => { @@ -409,25 +419,19 @@ async fn looped_service_connecting( } } async fn check_service(host: &str, port: &u32) -> Result<(), CustomError> { - let host = host.to_string(); - let port = *port; - - let result = tokio::task::spawn_blocking(move || { - let mut command = Command::new("bash"); - command.args(["service-checker.sh", &host, &port.to_string()]); + let mut command = Command::new("bash"); + command.args(["service-checker.sh", host, &port.to_string()]); - match command.output() { - Ok(output) => { - if output.status.success() { - Ok(()) - } else { - Err(CustomError::Fatal) - } - }, - Err(_) => { - Err(CustomError::Fatal) - }, - } - }).await; - result.unwrap_or(Err(CustomError::Fatal)) + match command.output() { + Ok(output) => { + if output.status.success() { + return Ok(()); + } else { + return Err(CustomError::Fatal); + } + }, + Err(_) => { + return Err(CustomError::Fatal); + }, + }; } \ No newline at end of file