fixed blocking bug

pull/9/head
prplV 2024-07-08 05:01:03 -04:00
parent 928c13ae11
commit 759ff64532
2 changed files with 27 additions and 14 deletions

View File

@ -57,8 +57,8 @@
"hostname" : "localhost", "hostname" : "localhost",
"port" : 8080, "port" : 8080,
"triggers" : { "triggers" : {
"wait" : 20, "wait" : 10,
"delay" : 5, "delay" : 2,
"onLost" : "stop" "onLost" : "stop"
} }
}] }]

View File

@ -1,6 +1,7 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json; use serde_json;
use tokio::io::Join; use tokio::io::Join;
use tokio::join;
use std::env::join_paths; use std::env::join_paths;
use std::fmt::Debug; use std::fmt::Debug;
use std::fs; use std::fs;
@ -108,14 +109,14 @@ async fn run_daemons(
) )
{ {
loop { loop {
let run_hand = running_handler(&proc.name, &proc.path, tx.clone()); let run_hand = running_handler(&proc, tx.clone());
let file_hand = file_handler(&proc.name,&proc.dependencies.files, 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 serv_hand = service_handler(&proc.name, &proc.dependencies.services, tx.clone());
tokio::select! { tokio::select! {
_ = run_hand => {}, _ = run_hand => {},
_ = file_hand => {}, // _ = file_hand => {},
_ = serv_hand => {}, // _ = serv_hand => {},
_val = rx.recv() => { _val = rx.recv() => {
match _val.unwrap() { match _val.unwrap() {
// 1 - File-dependency handling error -> terminating (after waiting) // 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 // check process status daemon
async fn running_handler(name: &str, path: &str, tx: mpsc::Sender<u8>){ async fn running_handler(prc: &TrackingProcess, tx: mpsc::Sender<u8>){
// println!("running daemon on {}", name); // println!("running daemon on {}", name);
loop {
let _ = Command::new("pidof") let _ = Command::new("pidof")
.arg(name) .arg(&prc.name)
.output() .output()
.expect("Failed to execute command 'pidof'"); .expect("Failed to execute command 'pidof'");
// services and files check (once) // 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 inactive -> spawn checks -> active is true
if start_process(name, path).await.is_err() {
if !is_active(&prc.name){
if start_process(&prc.name, &prc.path).await.is_err() {
tx.send(3).await.unwrap(); tx.send(3).await.unwrap();
// return; // return;
} }
} }
// if frozen -> spawn checks -> unfreeze is true
else if is_frozen(&prc.name){
}
tokio::time::sleep(Duration::from_millis(100)).await; tokio::time::sleep(Duration::from_millis(100)).await;
tokio::task::yield_now().await;
}
} }
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 {
// 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 {
@ -300,7 +314,6 @@ 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; tokio::task::yield_now().await;
}
} }
fn check_file(filename: &str, path: &str) -> Result<(), CustomError> { fn check_file(filename: &str, path: &str) -> Result<(), CustomError> {
let fileconcat = format!("{}{}", path, filename); let fileconcat = format!("{}{}", path, filename);