controllers impls and trait
parent
721fa6c758
commit
cd7669d942
|
|
@ -36,6 +36,10 @@ pub enum NegativeOutcomes<'a> {
|
||||||
ServiceIsUnreachable(&'a str, DependencyType, &'a str),
|
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)
|
/// # an Error enum (next will be deleted and replaced)
|
||||||
pub enum CustomError {
|
pub enum CustomError {
|
||||||
Fatal,
|
Fatal,
|
||||||
|
|
|
||||||
|
|
@ -10,18 +10,50 @@ use tokio::time::Duration;
|
||||||
use crate::options::structs::Events;
|
use crate::options::structs::Events;
|
||||||
|
|
||||||
pub mod v2 {
|
pub mod v2 {
|
||||||
use std::collections::HashMap;
|
// use std::collections::HashMap;
|
||||||
|
use crate::options::structs::{Triggers, ProcessUnit};
|
||||||
use crate::options::structs::Triggers;
|
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
type EventHandlers<'a> = Vec<MpscSender<Events<'a>>>;
|
||||||
|
|
||||||
struct FilesController<'a> {
|
struct FilesController<'a> {
|
||||||
name: &'a str,
|
name: &'a str,
|
||||||
watcher: Inotify,
|
path: String,
|
||||||
|
watcher: Option<Inotify>,
|
||||||
// obj: Arc<Files>,
|
// obj: Arc<Files>,
|
||||||
triggers: HashMap<&'a str, Triggers<'a>>,
|
triggers: Triggers<'a>,
|
||||||
event_registrator: Vec<MpscSender<Events<'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
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,7 +3,7 @@ use log::{error, warn};
|
||||||
use std::process::{Command, Output};
|
use std::process::{Command, Output};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use tokio::time::Duration;
|
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 std::collections::HashSet;
|
||||||
use tokio::sync::mpsc::Receiver as MpscReciever;
|
use tokio::sync::mpsc::Receiver as MpscReciever;
|
||||||
|
|
||||||
|
|
@ -21,7 +21,37 @@ pub mod v2 {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> ProcessController<'a> {
|
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() {
|
if let Ok(event) = self.event_reader.try_recv() {
|
||||||
match event {
|
match event {
|
||||||
Events::Positive(target) => {
|
Events::Positive(target) => {
|
||||||
|
|
@ -64,35 +94,6 @@ 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) {}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -9,16 +9,24 @@ use tokio::sync::mpsc::Sender as MpscSender;
|
||||||
use crate::options::structs::Events;
|
use crate::options::structs::Events;
|
||||||
|
|
||||||
pub mod v2 {
|
pub mod v2 {
|
||||||
use crate::options::structs::Triggers;
|
use crate::options::structs::{Triggers, ProcessUnit};
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
type EventHandlers<'a> = Vec<MpscSender<Events<'a>>>;
|
||||||
|
|
||||||
struct ServicesController<'a> {
|
struct ServicesController<'a> {
|
||||||
name: &'a str,
|
name: &'a str,
|
||||||
obj: Arc<Services>,
|
// obj: Arc<Services>,
|
||||||
triggers: HashMap<&'a str, Triggers<'a>>,
|
triggers: Triggers<'a>,
|
||||||
event_registrator: Vec<MpscSender<Events<'a>>>,
|
event_registrator: EventHandlers<'a>,
|
||||||
|
}
|
||||||
|
// self impl
|
||||||
|
impl<'a> ProcessUnit<'a> for ServicesController<'a> {
|
||||||
|
async fn process(&mut self) {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue