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