files logic fixed

feature/configv2
prplV 2025-04-22 11:22:07 -04:00
parent 502ea114a6
commit c3fd0dd09f
1 changed files with 276 additions and 270 deletions

View File

@ -5,7 +5,7 @@ use std::borrow::BorrowMut;
use std::path::Path; use std::path::Path;
use std::sync::Arc; use std::sync::Arc;
use tokio::sync::mpsc; use tokio::sync::mpsc;
use tokio::sync::mpsc::Sender as MpscSender; use tokio::sync::mpsc::Sender as Sender;
use tokio::time::Duration; use tokio::time::Duration;
use crate::options::structs::Events; use crate::options::structs::Events;
@ -17,8 +17,9 @@ pub mod v2 {
use super::*; use super::*;
use std::{collections::HashMap, path::Path}; use std::{collections::HashMap, path::Path};
type MpscSender<'a> = Arc<Sender<Events<'a>>>;
// type EventHandlers<'a> = HashMap<service name, sender object> // type EventHandlers<'a> = HashMap<service name, sender object>
type EventHandlers<'a> = HashMap<&'a str, (Triggers<'a>, MpscSender<Events<'a>>)>; type EventHandlers<'a> = HashMap<&'a str, (Triggers<'a>, MpscSender<'a>)>;
struct FilesController<'a> { struct FilesController<'a> {
name : &'a str, name : &'a str,
@ -26,6 +27,7 @@ pub mod v2 {
watcher : Option<Inotify>, watcher : Option<Inotify>,
// obj: Arc<Files>, // obj: Arc<Files>,
triggers : EventHandlers<'a>, triggers : EventHandlers<'a>,
code_name : String,
} }
impl<'a> FilesController<'a> { impl<'a> FilesController<'a> {
@ -35,6 +37,7 @@ pub mod v2 {
path : String::new(), path : String::new(),
watcher: None, watcher: None,
triggers, triggers,
code_name : name.to_string(),
} }
} }
pub async fn with_path(&mut self, path: impl AsRef<Path>) -> anyhow::Result<()> { pub async fn with_path(&mut self, path: impl AsRef<Path>) -> anyhow::Result<()> {
@ -48,14 +51,17 @@ pub mod v2 {
} }
} }
}; };
self.code_name = format!("{}{}", &self.path, &self.code_name);
Ok(()) Ok(())
} }
async fn trigger_on(&mut self, trigger_type: Option<FileTriggerType>) { async fn trigger_on(&'a mut self, trigger_type: Option<FileTriggerType>) {
let _ = self.triggers.iter() let _ = self.triggers.iter()
.map(|(prc_name, (triggers, channel))| async { .map(|(prc_name, (triggers, channel))| async {
let _ = channel.send({ let _ = channel.send({
match &trigger_type { match &trigger_type {
None => Events::Positive(self.name), None => {
Events::Positive(&self.code_name)
},
Some(event) => { Some(event) => {
info!("Event on file {} ({}) : {}. Notifying `{}` ...", self.name, &self.path, event, *prc_name); info!("Event on file {} ({}) : {}. Notifying `{}` ...", self.name, &self.path, event, *prc_name);
event.event_from_file_trigger_controller(self.name, triggers) event.event_from_file_trigger_controller(self.name, triggers)
@ -66,7 +72,7 @@ pub mod v2 {
} }
} }
impl<'a> ProcessUnit<'a> for FilesController<'a> { impl<'a> ProcessUnit<'a> for FilesController<'a> {
async fn process(&mut self) { async fn process(&'a mut self) {
// polling file check // polling file check
// 1) existing check // 1) existing check
if let Ok(_) = check_file(self.name, &self.path).await { if let Ok(_) = check_file(self.name, &self.path).await {