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