refs to tx replaced by Arc<>

pull/9/head
prplV 2024-07-08 14:40:46 +03:00
parent bcf59e5dcb
commit a2738e9fc7
1 changed files with 28 additions and 26 deletions

View File

@ -3,8 +3,10 @@ use serde_json;
use tokio::join; use tokio::join;
use std::fmt::Debug; use std::fmt::Debug;
use std::fs; use std::fs;
use std::ops::RangeInclusive;
use std::path::Path; use std::path::Path;
use std::process::{Command, Output}; use std::process::{Command, Output};
use std::sync::Arc;
use tokio::time::{Duration, Instant}; use tokio::time::{Duration, Instant};
use tokio::sync::mpsc; use tokio::sync::mpsc;
@ -99,10 +101,10 @@ async fn main() {
// creating msg channel // creating msg channel
let (tx, mut rx) = mpsc::channel::<u8>(1); let (tx, mut rx) = mpsc::channel::<u8>(1);
let proc_clone = proc.clone(); let proc = Arc::new(proc.clone());
let tx_clone = tx.clone(); let tx = Arc::new(tx.clone());
let event = tokio::spawn(async move { 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); handler.push(event);
} }
@ -117,8 +119,8 @@ async fn main() {
/// > hint : give mpsc with capacity 1 to jump over potential errors during running process /// > hint : give mpsc with capacity 1 to jump over potential errors during running process
/// ** in [developing](https://github.com/prplV/runner-rs "REPOSITORY") ** /// ** in [developing](https://github.com/prplV/runner-rs "REPOSITORY") **
async fn run_daemons( async fn run_daemons(
proc: &TrackingProcess, proc: Arc<TrackingProcess>,
tx: mpsc::Sender<u8>, tx: Arc<mpsc::Sender<u8>>,
rx: &mut mpsc::Receiver<u8> rx: &mut mpsc::Receiver<u8>
) )
{ {
@ -270,7 +272,7 @@ async fn start_process(name: &str, path: &str) -> Result<(), CustomError> {
} }
} }
// check process status daemon // check process status daemon
async fn running_handler(prc: &TrackingProcess, tx: mpsc::Sender<u8>){ async fn running_handler(prc: &TrackingProcess, tx: Arc<mpsc::Sender<u8>>){
// println!("running daemon on {}", prc.name); // println!("running daemon on {}", prc.name);
// services and files check (once) // services and files check (once)
let files_check = file_handler(&prc.name, &prc.dependencies.files, tx.clone()); let files_check = file_handler(&prc.name, &prc.dependencies.files, tx.clone());
@ -293,7 +295,7 @@ async fn running_handler(prc: &TrackingProcess, tx: mpsc::Sender<u8>){
tokio::task::yield_now().await; tokio::task::yield_now().await;
} }
async fn file_handler(name: &str, files: &Vec<Files>, tx: mpsc::Sender<u8>) -> Result<(), CustomError>{ async fn file_handler(name: &str, files: &Vec<Files>, tx: Arc<mpsc::Sender<u8>>) -> Result<(), CustomError>{
// println!("file daemon on {}", name); // println!("file daemon on {}", 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() {
@ -338,7 +340,7 @@ fn check_file(filename: &str, path: &str) -> Result<(), CustomError> {
} }
} }
async fn service_handler(name: &str, services: &Vec<Services>, tx: mpsc::Sender<u8>) -> Result<(), CustomError> { async fn service_handler(name: &str, services: &Vec<Services>, tx: Arc<mpsc::Sender<u8>>) -> Result<(), CustomError> {
// println!("service daemon on {}", name); // println!("service daemon on {}", name);
for serv in services { for serv in services {
if check_service(&serv.hostname, &serv.port).await.is_err() { if check_service(&serv.hostname, &serv.port).await.is_err() {