diff --git a/noxis-rs/src/options/structs.rs b/noxis-rs/src/options/structs.rs index 55a5222..282a13d 100644 --- a/noxis-rs/src/options/structs.rs +++ b/noxis-rs/src/options/structs.rs @@ -36,6 +36,10 @@ pub enum NegativeOutcomes<'a> { ServiceIsUnreachable(&'a str, DependencyType, &'a str), } +pub trait ProcessUnit<'a> { + fn process(&mut self) -> impl std::future::Future + Send; +} + /// # an Error enum (next will be deleted and replaced) pub enum CustomError { Fatal, diff --git a/noxis-rs/src/utils/files.rs b/noxis-rs/src/utils/files.rs index ee5d942..678b084 100644 --- a/noxis-rs/src/utils/files.rs +++ b/noxis-rs/src/utils/files.rs @@ -10,18 +10,50 @@ use tokio::time::Duration; use crate::options::structs::Events; pub mod v2 { - use std::collections::HashMap; - - use crate::options::structs::Triggers; - + // use std::collections::HashMap; + use crate::options::structs::{Triggers, ProcessUnit}; use super::*; + use std::path::Path; + + type EventHandlers<'a> = Vec>>; struct FilesController<'a> { name: &'a str, - watcher: Inotify, + path: String, + watcher: Option, // obj: Arc, - triggers: HashMap<&'a str, Triggers<'a>>, - event_registrator: Vec>>, + triggers: Triggers<'a>, + event_registrator: EventHandlers<'a>, + } + + impl<'a> FilesController<'a> { + pub fn new(name: &'a str, triggers: Triggers<'a>, event_registrator: EventHandlers<'a>) -> FilesController<'a> { + Self { + name, + path : String::new(), + watcher: None, + triggers, + event_registrator, + } + } + pub async fn with_path(&mut self, path: impl AsRef) -> anyhow::Result<()> { + self.path = path.as_ref().to_string_lossy().into_owned(); + self.watcher = Some({ + match create_watcher(self.name, &self.path).await { + Ok(val) => val, + Err(er) => return Err(er) + } + }); + Ok(()) + } + pub async fn trigger_on(&mut self) { + // trigger handler + } + } + impl<'a> ProcessUnit<'a> for FilesController<'a> { + async fn process(&mut self) { + // polling file check + } } } diff --git a/noxis-rs/src/utils/prcs.rs b/noxis-rs/src/utils/prcs.rs index 22eca4a..ef88228 100644 --- a/noxis-rs/src/utils/prcs.rs +++ b/noxis-rs/src/utils/prcs.rs @@ -3,7 +3,7 @@ use log::{error, warn}; use std::process::{Command, Output}; use std::sync::Arc; use tokio::time::Duration; -use crate::options::structs::{TrackingProcess, ProcessState, Events, NegativeOutcomes}; +use crate::options::structs::{TrackingProcess, ProcessState, Events, NegativeOutcomes, ProcessUnit}; use std::collections::HashSet; use tokio::sync::mpsc::Receiver as MpscReciever; @@ -21,7 +21,37 @@ pub mod v2 { } impl<'a> ProcessController<'a> { - pub async fn process(&mut self) { + async fn trigger_on(&mut self, dep_name: &str, trigger: &str, dep_type: DependencyType) { + match trigger { + "stay" => { + info!("Event on {} `{}` for {}. Ignoring ...", dep_type, dep_name, self.name); + }, + "stop" => { + if is_active(self.name).await { + info!("Event on {} `{}` for {}. Stopping ...", dep_type, dep_name, self.name); + terminate_process(self.name).await; + self.state = ProcessState::Stopped; + } + }, + "hold" => { + if !is_frozen(self.name).await { + info!("Event on {} `{}` for {}. Freezing ...", dep_type, dep_name, self.name); + freeze_process(self.name).await; + self.state = ProcessState::Holding; + } + }, + "restart" => { + info!("Event on {} `{}` for {}. Restarting ...", dep_type, dep_name, self.name); + let _ = restart_process(self.name, &self.obj.path).await; + }, + _ => error!("Impermissible trigger in file-trigger for {}. Ignoring event ...", self.name), + } + tokio::time::sleep(Duration::from_micros(100)).await; + } + } + + impl<'a> ProcessUnit<'a> for ProcessController<'a> { + async fn process(&mut self) { if let Ok(event) = self.event_reader.try_recv() { match event { Events::Positive(target) => { @@ -64,36 +94,7 @@ pub mod v2 { } } } - - async fn trigger_on(&mut self, dep_name: &str, trigger: &str, dep_type: DependencyType) { - match trigger { - "stay" => { - info!("Event on {} `{}` for {}. Ignoring ...", dep_type, dep_name, self.name); - }, - "stop" => { - if is_active(self.name).await { - info!("Event on {} `{}` for {}. Stopping ...", dep_type, dep_name, self.name); - terminate_process(self.name).await; - self.state = ProcessState::Stopped; - } - }, - "hold" => { - if !is_frozen(self.name).await { - info!("Event on {} `{}` for {}. Freezing ...", dep_type, dep_name, self.name); - freeze_process(self.name).await; - self.state = ProcessState::Holding; - } - }, - "restart" => { - info!("Event on {} `{}` for {}. Restarting ...", dep_type, dep_name, self.name); - let _ = restart_process(self.name, &self.obj.path).await; - }, - _ => error!("Impermissible trigger in file-trigger for {}. Ignoring event ...", self.name), - } - tokio::time::sleep(Duration::from_millis(100)).await; - } - async fn trigger_on_servcie(&mut self, file_name: &str, trigger: &str) {} - } + } } /// # Fn `get_pid` diff --git a/noxis-rs/src/utils/services.rs b/noxis-rs/src/utils/services.rs index 608b914..b90007d 100644 --- a/noxis-rs/src/utils/services.rs +++ b/noxis-rs/src/utils/services.rs @@ -9,16 +9,24 @@ use tokio::sync::mpsc::Sender as MpscSender; use crate::options::structs::Events; pub mod v2 { - use crate::options::structs::Triggers; + use crate::options::structs::{Triggers, ProcessUnit}; use super::*; use std::collections::HashMap; + type EventHandlers<'a> = Vec>>; + struct ServicesController<'a> { name: &'a str, - obj: Arc, - triggers: HashMap<&'a str, Triggers<'a>>, - event_registrator: Vec>>, + // obj: Arc, + triggers: Triggers<'a>, + event_registrator: EventHandlers<'a>, + } + // self impl + impl<'a> ProcessUnit<'a> for ServicesController<'a> { + async fn process(&mut self) { + + } } }