controllers impls and trait

feature/configv2
prplV 2025-04-15 10:32:29 -04:00
parent 721fa6c758
commit cd7669d942
4 changed files with 88 additions and 43 deletions

View File

@ -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<Output = ()> + Send;
}
/// # an Error enum (next will be deleted and replaced)
pub enum CustomError {
Fatal,

View File

@ -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<MpscSender<Events<'a>>>;
struct FilesController<'a> {
name: &'a str,
watcher: Inotify,
path: String,
watcher: Option<Inotify>,
// obj: Arc<Files>,
triggers: HashMap<&'a str, Triggers<'a>>,
event_registrator: Vec<MpscSender<Events<'a>>>,
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<Path>) -> 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
}
}
}

View File

@ -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`

View File

@ -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<MpscSender<Events<'a>>>;
struct ServicesController<'a> {
name: &'a str,
obj: Arc<Services>,
triggers: HashMap<&'a str, Triggers<'a>>,
event_registrator: Vec<MpscSender<Events<'a>>>,
// obj: Arc<Services>,
triggers: Triggers<'a>,
event_registrator: EventHandlers<'a>,
}
// self impl
impl<'a> ProcessUnit<'a> for ServicesController<'a> {
async fn process(&mut self) {
}
}
}