diff --git a/app_env/src/config.rs b/app_env/src/config.rs index 728b790..884abd4 100644 --- a/app_env/src/config.rs +++ b/app_env/src/config.rs @@ -1,7 +1,8 @@ // // Copyright 2021, Jesús Pérez Lorenzo // -use serde::{Deserialize}; +use serde::{Serialize,Deserialize,Deserializer}; +use std::fmt; #[derive(Clone, Debug, Deserialize)] pub struct Notifier { @@ -130,27 +131,80 @@ impl WebServer { } } +#[derive(Eq, PartialEq, Clone, Serialize, Debug, Deserialize)] +pub enum SchedTaskStore { + None, + File(String), + Acid(String), +} +impl Default for SchedTaskStore { + fn default() -> Self { + SchedTaskStore::None + } +} +impl fmt::Display for SchedTaskStore { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + SchedTaskStore::None => write!(f,"none"), + SchedTaskStore::File(p) => write!(f,"file: {}",p), + SchedTaskStore::Acid(p) => write!(f,"acid: {}",p), + } + //write!(f, "{:?}", self) + } +} + +impl SchedTaskStore { + pub fn get_value(&self) -> String { + match self { + SchedTaskStore::None => format!(""), + SchedTaskStore::File(p) => format!("{}",p), + SchedTaskStore::Acid(p) => format!("{}",p), + } + } +} + +fn deserialize_schedtaskstore<'de, D>(deserializer: D) -> Result +where D: Deserializer<'de> { + let buf = String::deserialize(deserializer)?; + let v: Vec<&str> = buf.split(':').collect(); + if v.len() > 1 { + Ok(match v[0] { + "File" => SchedTaskStore::File(v[1].to_owned()), + "Acid" => SchedTaskStore::Acid(v[1].to_owned()), + _ => SchedTaskStore::None + }) + } else { + Ok(SchedTaskStore::None) + } +} + +#[derive(Clone, Debug, Deserialize, Default)] +pub struct SchedTask { + pub name: String, + #[serde(deserialize_with = "deserialize_schedtaskstore")] + pub store: SchedTaskStore, + pub schedule: String, + pub on_start: bool, + pub pause: u64, +} + #[derive(Clone, Debug, Deserialize, Default)] pub struct Config { pub run_mode: String, // warp log name pub log_name: String, - pub loop_duration: u64, - pub run_cache: bool, pub cache_path: String, pub cache_lock_path: String, pub cache_lock_ext: String, - pub run_check: bool, pub check_path: String, - pub run_monitor: bool, pub monitor_rules_path: String, pub monitor_rules_file: String, pub monitor_rules_format: String, - pub monitor_states_path: String, - pub monitor_states_file: String, pub default_lang: String, pub logs_store: String, pub logs_format: String, + pub run_schedtasks: bool, + pub schedtasks: Vec, pub run_websrvrs: bool, pub websrvrs: Vec, } @@ -185,7 +239,17 @@ impl Config { app_cfg.cache_lock_path=format!("{}/{}",&app_home,&app_cfg.cache_lock_path); app_cfg.check_path=format!("{}/{}",&app_home,&app_cfg.check_path); app_cfg.monitor_rules_path=format!("{}/{}",&app_home,&app_cfg.monitor_rules_path); - app_cfg.monitor_states_path=format!("{}/{}",&app_home,&app_cfg.monitor_states_path); + + // let mut schedtasks: Vec = Vec::new(); + // app_cfg.schedtasks.iter().enumerate().for_each(|(pos,it)| { + // schedtasks.push(it.to_owned()); + // if !it.path.is_empty() { + // schedtasks[pos].path=format!("{}/{}",&app_home,&it.path) + // } + // if !it.lock_path.is_empty() { + // schedtasks[pos].lock_path=format!("{}/{}",&app_home,&it.lock_path) + // } + // }); let mut websrvrs: Vec = Vec::new(); @@ -218,4 +282,18 @@ impl Config { pub fn st_log_name(&self) -> &'static str { Box::leak(self.log_name.to_owned().into_boxed_str()) } + pub fn get_schedtask(&self,name: &str) -> SchedTask { + let filter: Vec<&SchedTask> = self.schedtasks.iter().filter(|it| it.name.as_str() == name).collect(); + if filter.len() > 0 { + return filter[0].to_owned() + } + SchedTask::default() + } + pub fn get_websrvr(&self,name: &str) -> WebServer { + let filter: Vec<&WebServer> = self.websrvrs.iter().filter(|it| it.name.as_str() == name).collect(); + if filter.len() > 0 { + return filter[0].to_owned() + } + WebServer::default() + } }