From a2738e9fc7718ed94498d4f1e0145dbc6836b19e Mon Sep 17 00:00:00 2001 From: prplV Date: Mon, 8 Jul 2024 14:40:46 +0300 Subject: [PATCH] refs to tx replaced by Arc<> --- src/main.rs | 54 +++++++++++++++++++++++++++-------------------------- 1 file changed, 28 insertions(+), 26 deletions(-) diff --git a/src/main.rs b/src/main.rs index 69175fe..04fe493 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,8 +3,10 @@ use serde_json; use tokio::join; use std::fmt::Debug; use std::fs; +use std::ops::RangeInclusive; use std::path::Path; use std::process::{Command, Output}; +use std::sync::Arc; use tokio::time::{Duration, Instant}; use tokio::sync::mpsc; @@ -99,10 +101,10 @@ async fn main() { // creating msg channel let (tx, mut rx) = mpsc::channel::(1); - let proc_clone = proc.clone(); - let tx_clone = tx.clone(); + let proc = Arc::new(proc.clone()); + let tx = Arc::new(tx.clone()); let event = tokio::spawn(async move { - run_daemons(&proc_clone, tx_clone, &mut rx).await; + run_daemons(proc, tx, &mut rx).await; }); handler.push(event); } @@ -117,8 +119,8 @@ async fn main() { /// > hint : give mpsc with capacity 1 to jump over potential errors during running process /// ** in [developing](https://github.com/prplV/runner-rs "REPOSITORY") ** async fn run_daemons( - proc: &TrackingProcess, - tx: mpsc::Sender, + proc: Arc, + tx: Arc>, rx: &mut mpsc::Receiver ) { @@ -270,31 +272,31 @@ async fn start_process(name: &str, path: &str) -> Result<(), CustomError> { } } // check process status daemon -async fn running_handler(prc: &TrackingProcess, tx: mpsc::Sender){ - // println!("running daemon on {}", prc.name); - // 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()); +async fn running_handler(prc: &TrackingProcess, tx: Arc>){ + // println!("running daemon on {}", prc.name); + // 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()); - let res = join!(files_check, services_check); - // if inactive -> spawn checks -> active is true - if !is_active(&prc.name) && res.0.is_ok() && res.1.is_ok(){ - 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) && res.0.is_ok() && res.1.is_ok(){ - tx.send(10).await.unwrap(); + let res = join!(files_check, services_check); + // if inactive -> spawn checks -> active is true + if !is_active(&prc.name) && res.0.is_ok() && res.1.is_ok(){ + if start_process(&prc.name, &prc.path).await.is_err() { + tx.send(3).await.unwrap(); return; } - tokio::time::sleep(Duration::from_millis(100)).await; - tokio::task::yield_now().await; + } + // if frozen -> spawn checks -> unfreeze is true + else if is_frozen(&prc.name) && res.0.is_ok() && res.1.is_ok(){ + tx.send(10).await.unwrap(); + return; + } + tokio::time::sleep(Duration::from_millis(100)).await; + tokio::task::yield_now().await; } -async fn file_handler(name: &str, files: &Vec, tx: mpsc::Sender) -> Result<(), CustomError>{ - // println!("file daemon on {}", name); +async fn file_handler(name: &str, files: &Vec, tx: Arc>) -> Result<(), CustomError>{ + // 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) { @@ -338,7 +340,7 @@ fn check_file(filename: &str, path: &str) -> Result<(), CustomError> { } } -async fn service_handler(name: &str, services: &Vec, tx: mpsc::Sender) -> Result<(), CustomError> { +async fn service_handler(name: &str, services: &Vec, tx: Arc>) -> Result<(), CustomError> { // println!("service daemon on {}", name); for serv in services { if check_service(&serv.hostname, &serv.port).await.is_err() {