OWNERSHIP FIX: files with Arc<str>
parent
09c1baed8e
commit
34979a035d
|
|
@ -12,46 +12,43 @@
|
|||
|
||||
pub mod v2 {
|
||||
use log::{error, info, warn};
|
||||
|
||||
// use std::collections::HashMap;
|
||||
use crate::options::structs::{FileTriggerType, FileTriggersForController as Triggers, ProcessUnit};
|
||||
use super::*;
|
||||
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<&'a str, (Triggers<'a>, MpscSender<'a>)>;
|
||||
type MpscSender = Arc<Sender<Events>>;
|
||||
type EventHandlers = HashMap<Arc<str>, (Triggers, MpscSender)>;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct FilesController<'a> {
|
||||
name : &'a str,
|
||||
pub struct FilesController {
|
||||
name : Arc<str>,
|
||||
path : String,
|
||||
code_name : Arc<str>,
|
||||
watcher : Option<Inotify>,
|
||||
// obj: Arc<Files>,
|
||||
triggers : EventHandlers<'a>,
|
||||
code_name : String,
|
||||
triggers : EventHandlers,
|
||||
}
|
||||
|
||||
impl<'a> PartialEq for FilesController<'a> {
|
||||
impl PartialEq for FilesController {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.path == other.path && self.name == other.name
|
||||
self.code_name == other.code_name
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> FilesController<'a> {
|
||||
pub fn new(name: &'a str, triggers: EventHandlers<'a>) -> FilesController<'a> {
|
||||
impl FilesController {
|
||||
pub fn new(name: &str, triggers: EventHandlers) -> FilesController {
|
||||
let name: Arc<str> = Arc::from(name);
|
||||
Self {
|
||||
name,
|
||||
name: name.clone(),
|
||||
path : String::new(),
|
||||
watcher: None,
|
||||
triggers,
|
||||
code_name : name.to_string(),
|
||||
code_name : name.clone(),
|
||||
}
|
||||
}
|
||||
pub fn with_path(mut self, path: impl AsRef<Path>) -> anyhow::Result<FilesController<'a>> {
|
||||
pub fn with_path(mut self, path: impl AsRef<Path>) -> anyhow::Result<FilesController> {
|
||||
self.path = path.as_ref().to_string_lossy().into_owned();
|
||||
self.watcher = {
|
||||
match create_watcher(self.name, &self.path) {
|
||||
match create_watcher(&self.name, &self.path) {
|
||||
Ok(val) => Some(val),
|
||||
Err(er) => {
|
||||
error!("Cannot create watcher for {} ({}) due to {}", self.name, &self.path, er);
|
||||
|
|
@ -59,25 +56,25 @@
|
|||
}
|
||||
}
|
||||
};
|
||||
self.code_name = format!("{}{}", &self.path, &self.code_name);
|
||||
self.code_name = Arc::from(format!("{}{}", &self.path, &self.code_name));
|
||||
Ok(self)
|
||||
}
|
||||
pub fn add_event(&mut self, file_controller : FilesController<'a>) {
|
||||
pub fn add_event(&mut self, file_controller : FilesController) {
|
||||
for (k, v) in file_controller.triggers {
|
||||
self.triggers.entry(k).or_insert(v);
|
||||
}
|
||||
}
|
||||
async fn trigger_on(&'a mut self, trigger_type: Option<FileTriggerType>) {
|
||||
async fn trigger_on(&mut self, trigger_type: Option<FileTriggerType>) {
|
||||
let _ = self.triggers.iter()
|
||||
.map(|(prc_name, (triggers, channel))| async {
|
||||
let _ = channel.send({
|
||||
match &trigger_type {
|
||||
None => {
|
||||
Events::Positive(&self.code_name)
|
||||
Events::Positive(self.code_name.clone())
|
||||
},
|
||||
Some(event) => {
|
||||
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.code_name.clone(), triggers)
|
||||
},
|
||||
}
|
||||
}).await;
|
||||
|
|
@ -85,11 +82,11 @@
|
|||
}
|
||||
}
|
||||
#[async_trait]
|
||||
impl<'a> ProcessUnit<'a> for FilesController<'a> {
|
||||
async fn process(&'a mut self) {
|
||||
impl ProcessUnit for FilesController {
|
||||
async fn process(&mut self) {
|
||||
// polling file check
|
||||
// 1) existing check
|
||||
if let Ok(_) = check_file(self.name, &self.path).await {
|
||||
if let Ok(_) = check_file(&self.name, &self.path).await {
|
||||
match &mut self.watcher {
|
||||
Some(notify) => {
|
||||
let mut buffer = [0; 1024];
|
||||
|
|
@ -100,7 +97,7 @@
|
|||
) {
|
||||
warn!("File {} ({}) was changed", self.name, &self.path);
|
||||
if recreate_watcher {
|
||||
self.watcher = match create_watcher(self.name, &self.path) {
|
||||
self.watcher = match create_watcher(&self.name, &self.path) {
|
||||
Ok(notifier) => Some(notifier),
|
||||
Err(er) => {
|
||||
error!("Failed to recreate watcher for {} ({}) due to {}",
|
||||
|
|
|
|||
Loading…
Reference in New Issue