diff --git a/noxis-rs/settings.json b/noxis-rs/settings.json index 10485c8..bb5f44a 100644 --- a/noxis-rs/settings.json +++ b/noxis-rs/settings.json @@ -12,7 +12,7 @@ "src": "./tests/examples/", "triggers": { "onDelete": "stop", - "onChange": "stay" + "onChange": "restart" } } ], @@ -23,7 +23,7 @@ "triggers": { "wait": 10, "delay": 2, - "onLost": "hold" + "onLost": "restart" } } ] diff --git a/noxis-rs/src/main.rs b/noxis-rs/src/main.rs index 7341bac..aa9eeb1 100644 --- a/noxis-rs/src/main.rs +++ b/noxis-rs/src/main.rs @@ -15,6 +15,7 @@ use utils::*; use options::preboot::PrebootParams; use tokio::sync::{broadcast, oneshot}; use options::config::v2::init_config_mechanism; +use utils::v2::init_monitoring; #[tokio::main(flavor = "multi_thread", worker_threads = 4)] async fn main() -> anyhow::Result<()>{ @@ -23,11 +24,13 @@ async fn main() -> anyhow::Result<()>{ let _ = setup_logger(); info!("Noxis is configurating..."); - - let (tx_brd, mut _rx_brd) = broadcast::channel::(1); - let (_tx_oneshot, rx_oneshot) = oneshot::channel::(); + // + let (tx_brd, mut rx_brd) = broadcast::channel::(1); + // cli <-> config + let (tx_oneshot, rx_oneshot) = oneshot::channel::(); let mut handler: Vec> = vec![]; + // initilaizing task for config manipulations let config_module = tokio::spawn(async move { let _ = init_config_mechanism( rx_oneshot, @@ -37,6 +40,7 @@ async fn main() -> anyhow::Result<()>{ }); handler.push(config_module); + // initilaizing task for cli manipulation let cli_module = tokio::spawn(async move { if let Err(er) = init_cli_pipeline().await { error!("CLI pipeline failed due to {}", er) @@ -44,14 +48,22 @@ async fn main() -> anyhow::Result<()>{ }); handler.push(cli_module); + // initilaizing task for deinitializing `Noxis` let ctrlc = tokio::spawn(async move { if let Err(er) = set_valid_destructor(vec![].into()).await { - error!("CTRLC mod failed!"); + error!("Destructor mod failed due to {}", er); } std::process::exit(0); }); handler.push(ctrlc); + let monitoring = tokio::spawn(async move { + if let Err(er) = init_monitoring(&mut rx_brd).await { + error!("Monitoring mod failed due to {}", er); + } + }); + handler.push(monitoring); + for i in handler { let _ = i.await; } diff --git a/noxis-rs/src/options/structs.rs b/noxis-rs/src/options/structs.rs index 18ae5e9..55a5222 100644 --- a/noxis-rs/src/options/structs.rs +++ b/noxis-rs/src/options/structs.rs @@ -3,6 +3,39 @@ use std::net::Ipv4Addr; use serde::{Deserialize, Serialize}; +pub enum DependencyType { + File, + Service, +} +pub enum Triggers<'a> { + File{ on_change: &'a str, on_delete: &'a str }, + Service(&'a str), +} + +impl std::fmt::Display for DependencyType { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + return match self { + DependencyType::File => write!(f, "File"), + DependencyType::Service => write!(f, "Service"), + } + } +} +pub enum ProcessState { + Pending, + Holding, + Stopped, + StoppedByCli, +} +pub enum Events<'a> { + Positive(&'a str), + Negative(NegativeOutcomes<'a>) +} +pub enum NegativeOutcomes<'a> { + FileWasChanged(&'a str, DependencyType, &'a str), + FileWasMovedOrDeleted(&'a str, DependencyType, &'a str), + ServiceIsUnreachable(&'a str, DependencyType, &'a str), +} + /// # an Error enum (next will be deleted and replaced) pub enum CustomError { Fatal, diff --git a/noxis-rs/src/utils.rs b/noxis-rs/src/utils.rs index 9ead34d..468075c 100644 --- a/noxis-rs/src/utils.rs +++ b/noxis-rs/src/utils.rs @@ -6,12 +6,11 @@ pub mod services; // TODO : saving current flags state -use crate::options::structs::CustomError; -use crate::options::structs::TrackingProcess; +use crate::options::structs::{CustomError, TrackingProcess, Processes}; use files::create_watcher; use files::file_handler; use inotify::Inotify; -use log::{error, warn}; +use log::{error, warn, info}; use prcs::{ freeze_process, is_active, is_frozen, restart_process, start_process, terminate_process, unfreeze_process, @@ -22,9 +21,50 @@ use std::sync::Arc; use tokio::join; use tokio::sync::mpsc; use tokio::time::Duration; +use tokio::sync::broadcast::Receiver; +use tokio::sync::mpsc::{Receiver as MpscReciever, Sender as MpscSender}; const GET_ID_CMD: &str = "hostname"; +pub mod v2 { + use super::*; + + // spawn tasks + // spawn prc + // spawn files + // spawn services + // ## for ... i.await in loop + pub async fn init_monitoring( + local_config: &mut Receiver, + ) -> anyhow::Result<()> { + let config = if !local_config.is_empty() { + local_config.recv().await? + } else { + let mut tick = tokio::time::interval(Duration::from_millis(500)); + loop { + tick.tick().await; + break match local_config.try_recv() { + Ok(conf) => conf, + Err(_) => continue, + } + } + }; + info!("Processing {} processes ...", config.processes.len()); + // LinkedList + // LinkedList + Ok(()) + } + + // spawn prc check with semaphore check + async fn prcs_monitoriing() -> anyhow::Result<()> { Ok(()) } + + // spawn file check with semaphore check + async fn files_monitoriing() -> anyhow::Result<()> { Ok(()) } + + // spawn service check with semaphore check + async fn services_monitoriing() -> anyhow::Result<()> { Ok(()) } +} + /// # Fn `run_daemons` /// ## async func to run 3 main daemons: process, service and file monitors and manage process state according to given messages into channel /// @@ -133,7 +173,6 @@ async fn process_protocol_symbol(proc: Arc, val: u8) -> Result< }, // // 9 - File-dependency change -> staying (after check) 9 => { - // no need to trash logs warn!("File-dependency warning (file changed). Ignoring event on {} process...", &proc.name); tokio::time::sleep(Duration::from_millis(100)).await; },