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",
"port" : 8080,
"triggers" : {
"wait" : 20,
"delay" : 5,
"wait" : 10,
"delay" : 2,
"onLost" : "stop"
}
}]

View File

@ -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<u8>){
async fn running_handler(prc: &TrackingProcess, tx: mpsc::Sender<u8>){
// 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<Files>, tx: mpsc::Sender<u8>) {
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<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> {
let fileconcat = format!("{}{}", path, filename);