refs to tx replaced by Arc<>
parent
bcf59e5dcb
commit
a2738e9fc7
54
src/main.rs
54
src/main.rs
|
|
@ -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,31 +272,31 @@ 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());
|
||||||
let services_check = service_handler(&prc.name, &prc.dependencies.services, tx.clone());
|
let services_check = service_handler(&prc.name, &prc.dependencies.services, tx.clone());
|
||||||
|
|
||||||
let res = join!(files_check, services_check);
|
let res = join!(files_check, services_check);
|
||||||
// if inactive -> spawn checks -> active is true
|
// if inactive -> spawn checks -> active is true
|
||||||
if !is_active(&prc.name) && res.0.is_ok() && res.1.is_ok(){
|
if !is_active(&prc.name) && res.0.is_ok() && res.1.is_ok(){
|
||||||
if start_process(&prc.name, &prc.path).await.is_err() {
|
if start_process(&prc.name, &prc.path).await.is_err() {
|
||||||
tx.send(3).await.unwrap();
|
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();
|
|
||||||
return;
|
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<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() {
|
||||||
if !is_active(name) || is_frozen(name) {
|
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<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() {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue