doc + refactor of visual defects

pull/9/head
prplV 2024-07-08 14:12:59 +03:00
parent 379eb97769
commit bcf59e5dcb
1 changed files with 105 additions and 107 deletions

View File

@ -1,28 +1,28 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json; use serde_json;
use tokio::io::Join;
use tokio::join; use tokio::join;
use std::env::join_paths;
use std::fmt::Debug; use std::fmt::Debug;
use std::fs; use std::fs;
use std::path::Path; use std::path::Path;
use std::process::{Command, Output}; use std::process::{Command, Output};
// to use in time-trigger
use tokio::time::{Duration, Instant}; use tokio::time::{Duration, Instant};
// to store condition between asynchronous tasks use tokio::sync::mpsc;
use tokio::sync::{futures, mpsc};
/// # an Error enum (nextly will be deleted and replaced)
enum CustomError { enum CustomError {
Fatal, Fatal,
} }
/// # struct for the 1st level in json conf file
/// > (needed in serialization and deserialization)
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
struct Processes { struct Processes {
#[serde(default)] #[serde(default)]
processes : Vec<TrackingProcess>, processes : Vec<TrackingProcess>,
} }
/// # struct for each process to contain info, such as name, path and dependencies
/// > (needed in serialization and deserialization)
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
struct TrackingProcess { struct TrackingProcess {
name : String, name : String,
@ -30,6 +30,8 @@ struct TrackingProcess {
dependencies: Dependencies, dependencies: Dependencies,
} }
/// # struct for processes' dependecies including files and services
/// > (needed in serialization and deserialization)
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
struct Dependencies { struct Dependencies {
#[serde(default)] #[serde(default)]
@ -37,6 +39,9 @@ struct Dependencies {
#[serde(default)] #[serde(default)]
services: Vec<Services>, services: Vec<Services>,
} }
/// # struct for containing file object with its triggers to manipulate in daemons
/// > (needed in serialization and deserialization)
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
struct Files { struct Files {
filename : String, filename : String,
@ -44,6 +49,8 @@ struct Files {
triggers : FIleTriggers, triggers : FIleTriggers,
} }
/// # struct for containing service object with its triggers to manipulate in daemons
/// > (needed in serialization and deserialization)
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
struct Services { struct Services {
hostname : String, hostname : String,
@ -51,7 +58,8 @@ struct Services {
triggers : ServiceTriggers, triggers : ServiceTriggers,
} }
// policy /// # struct for instancing each service's policies such as on lost or time to wait till reachable
/// > (needed in serialization and deserialization)
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
struct ServiceTriggers { struct ServiceTriggers {
wait : u32, wait : u32,
@ -59,6 +67,9 @@ struct ServiceTriggers {
#[serde(rename="onLost")] #[serde(rename="onLost")]
on_lost : String, on_lost : String,
} }
/// # struct for instancing each file's policies such as ondelete or onupdate events
/// > (needed in serialization and deserialization)
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
struct FIleTriggers { struct FIleTriggers {
#[serde(rename="onDelete")] #[serde(rename="onDelete")]
@ -102,6 +113,9 @@ async fn main() {
return; return;
} }
/// # async func to run 3 main daemons (now its more like tree-form than classiacl 0.1.0 form )
/// > 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( async fn run_daemons(
proc: &TrackingProcess, proc: &TrackingProcess,
tx: mpsc::Sender<u8>, tx: mpsc::Sender<u8>,
@ -110,13 +124,9 @@ async fn run_daemons(
{ {
loop { loop {
let run_hand = running_handler(&proc, 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! { tokio::select! {
_ = run_hand => {}, _ = run_hand => {},
// _ = file_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)
@ -262,11 +272,6 @@ 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: mpsc::Sender<u8>){
// println!("running daemon on {}", prc.name); // println!("running daemon on {}", prc.name);
// loop {
// let _ = Command::new("pidof")
// .arg(&prc.name)
// .output()
// .expect("Failed to execute command 'pidof'");
// 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());
@ -286,7 +291,6 @@ async fn running_handler(prc: &TrackingProcess, 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;
// }
} }
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: mpsc::Sender<u8>) -> Result<(), CustomError>{
@ -334,11 +338,8 @@ 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: mpsc::Sender<u8>) -> Result<(), CustomError> {
// println!("service daemon on {}", name); // println!("service daemon on {}", name);
// let state = is_active(name);
// let condition = is_frozen(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() {
if !is_active(name) || is_frozen(name) { if !is_active(name) || is_frozen(name) {
@ -375,8 +376,6 @@ async fn service_handler(name: &str, services: &Vec<Services>, tx: mpsc::Sender<
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;
Ok(()) Ok(())
} }
async fn looped_service_connecting( async fn looped_service_connecting(
@ -384,7 +383,6 @@ async fn looped_service_connecting(
serv: &Services serv: &Services
) -> Result<(), CustomError> ) -> Result<(), CustomError>
{ {
if serv.triggers.wait == 0 { if serv.triggers.wait == 0 {
loop { loop {
tokio::time::sleep(Duration::from_secs(serv.triggers.delay.into())).await; tokio::time::sleep(Duration::from_secs(serv.triggers.delay.into())).await;