123
parent
244508509c
commit
928c13ae11
63
src/main.rs
63
src/main.rs
|
|
@ -66,7 +66,7 @@ struct FIleTriggers {
|
|||
on_change : String,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
#[tokio::main(flavor = "multi_thread")]
|
||||
async fn main() {
|
||||
let processes = load_processes("settings.json");
|
||||
// let mut error_counter = 0;
|
||||
|
|
@ -247,11 +247,13 @@ 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<u8>){
|
||||
println!("running daemon on {}", name);
|
||||
// println!("running daemon on {}", name);
|
||||
let _ = Command::new("pidof")
|
||||
.arg(name)
|
||||
.output()
|
||||
.expect("Failed to execute command 'pidof'");
|
||||
// services and files check (once)
|
||||
// if inactive ->
|
||||
|
||||
if !is_active(name) && !is_frozen(name){
|
||||
if start_process(name, path).await.is_err() {
|
||||
|
|
@ -264,7 +266,7 @@ async fn running_handler(name: &str, path: &str, tx: mpsc::Sender<u8>){
|
|||
|
||||
async fn file_handler(name: &str, files: &Vec<Files>, tx: mpsc::Sender<u8>) {
|
||||
loop {
|
||||
println!("file daemon on {}", name);
|
||||
// println!("file daemon on {}", name);
|
||||
if is_active(name) && !is_frozen(name){
|
||||
for file in files {
|
||||
if check_file(&file.filename, &file.src).is_err() {
|
||||
|
|
@ -297,6 +299,7 @@ async fn file_handler(name: &str, files: &Vec<Files>, tx: mpsc::Sender<u8>) {
|
|||
// }
|
||||
}
|
||||
tokio::time::sleep(Duration::from_millis(100)).await;
|
||||
tokio::task::yield_now().await;
|
||||
}
|
||||
}
|
||||
fn check_file(filename: &str, path: &str) -> Result<(), CustomError> {
|
||||
|
|
@ -311,23 +314,22 @@ fn check_file(filename: &str, path: &str) -> Result<(), CustomError> {
|
|||
|
||||
// ??
|
||||
async fn service_handler(name: &str, services: &Vec<Services>, tx: mpsc::Sender<u8>) {
|
||||
loop {
|
||||
println!("service daemon on {}", name);
|
||||
// println!("service daemon on {}", name);
|
||||
if is_active(name) && !is_frozen(name) {
|
||||
for serv in services {
|
||||
if check_service(&serv.hostname, &serv.port).is_err() {
|
||||
if check_service(&serv.hostname, &serv.port).await.is_err() {
|
||||
// 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() {
|
||||
if looped_service_connecting(name, serv).await.is_err() {
|
||||
tx.send(5).await.unwrap();
|
||||
return;
|
||||
}
|
||||
},
|
||||
"hold" => {
|
||||
if looped_service_connecting(&name, serv).await.is_err() {
|
||||
if looped_service_connecting(name, serv).await.is_err() {
|
||||
tx.send(6).await.unwrap();
|
||||
}
|
||||
},
|
||||
|
|
@ -343,7 +345,9 @@ async fn service_handler(name: &str, services: &Vec<Services>, tx: mpsc::Sender<
|
|||
// }
|
||||
}
|
||||
tokio::time::sleep(Duration::from_millis(100)).await;
|
||||
}
|
||||
tokio::task::yield_now().await;
|
||||
|
||||
|
||||
}
|
||||
|
||||
async fn looped_service_connecting(
|
||||
|
|
@ -351,11 +355,12 @@ async fn looped_service_connecting(
|
|||
serv: &Services
|
||||
) -> Result<(), CustomError>
|
||||
{
|
||||
|
||||
if serv.triggers.wait == 0 {
|
||||
loop {
|
||||
tokio::time::sleep(Duration::from_secs(serv.triggers.delay.into())).await;
|
||||
println!("Attempting to connect from {} process to {}:{}",&name, &serv.hostname, &serv.port );
|
||||
match check_service(&serv.hostname, &serv.port) {
|
||||
match check_service(&serv.hostname, &serv.port).await {
|
||||
Ok(_) => {
|
||||
break;
|
||||
},
|
||||
|
|
@ -370,7 +375,7 @@ async fn looped_service_connecting(
|
|||
while start.elapsed().as_secs() < serv.triggers.wait.into() {
|
||||
tokio::time::sleep(Duration::from_secs(serv.triggers.delay.into())).await;
|
||||
println!("Attempting to connect from {} process to {}:{}",&name, &serv.hostname, &serv.port );
|
||||
match check_service(&serv.hostname, &serv.port) {
|
||||
match check_service(&serv.hostname, &serv.port).await {
|
||||
Ok(_) => {
|
||||
return Ok(());
|
||||
},
|
||||
|
|
@ -382,20 +387,26 @@ async fn looped_service_connecting(
|
|||
return Err(CustomError::Fatal);
|
||||
}
|
||||
}
|
||||
fn check_service(host: &str, port: &u32) -> Result<(), CustomError> {
|
||||
let mut command = Command::new("bash");
|
||||
command.args(["service-checker.sh", host, &port.to_string()]);
|
||||
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()]);
|
||||
|
||||
match command.output() {
|
||||
Ok(output) => {
|
||||
if output.status.success() {
|
||||
return Ok(());
|
||||
} else {
|
||||
return Err(CustomError::Fatal);
|
||||
}
|
||||
},
|
||||
Err(_) => {
|
||||
return Err(CustomError::Fatal);
|
||||
},
|
||||
};
|
||||
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))
|
||||
}
|
||||
Loading…
Reference in New Issue