docs: config

pull/7/head
prplV 2024-11-13 14:11:04 +03:00
parent b07a0e1212
commit 08ca2483e1
1 changed files with 151 additions and 3 deletions

View File

@ -11,7 +11,19 @@ use tokio::time::Duration;
const CONFIG_PATH: &str = "settings.json"; const CONFIG_PATH: &str = "settings.json";
// 4ever sync /// # Fn `load_processes`
/// ## for reading and parsing *local* storing config
///
/// *input* : `&str`
///
/// *output* : `None` if local conf file doesn't exist or invalid | `Some(conf)` on finish reading and parsing
///
/// *initiator* : func `get_actual_config`
///
/// *managing* : conf file name in `&str` format
///
/// *depends on* : struct `Processes`
///
fn load_processes(json_filename: &str) -> Option<Processes> { fn load_processes(json_filename: &str) -> Option<Processes> {
if let Ok(res) = fs::read_to_string(json_filename) { if let Ok(res) = fs::read_to_string(json_filename) {
if let Ok(conf) = serde_json::from_str::<Processes>(&res) { if let Ok(conf) = serde_json::from_str::<Processes>(&res) {
@ -21,6 +33,19 @@ fn load_processes(json_filename: &str) -> Option<Processes> {
None None
} }
/// # Fn `get_actual_config`
/// ## for getting actual Monitor's config from local and remote storages
///
/// *input* : -
///
/// *output* : `None` on fatal error in mechanisms | `Some(conf)` on finish reading and parsing
///
/// *initiator* : main thread
///
/// *managing* : -
///
/// *depends on* : struct `Processes`
///
pub async fn get_actual_config() -> Option<Processes> { pub async fn get_actual_config() -> Option<Processes> {
// * if no local conf -> loop and +inf getting conf from redis server // * if no local conf -> loop and +inf getting conf from redis server
// * if local conf -> once getting conf from redis server // * if local conf -> once getting conf from redis server
@ -64,6 +89,19 @@ pub async fn get_actual_config() -> Option<Processes> {
} }
} }
/// # Fn `get_remote_conf_watcher`
/// ## for infinitive pulling remote config
///
/// *input* : `&mut Connection`
///
/// *output* : `None` on fatal error | `Some(conf)` on succesfull pulling
///
/// *initiator* : fn `get_actual_config`
///
/// *managing* : mut ref `Connection` object
///
/// *depends on* : struct `Processes`
///
async fn get_remote_conf_watcher(conn : &mut Connection) -> Option<Processes> { async fn get_remote_conf_watcher(conn : &mut Connection) -> Option<Processes> {
let mut conn = conn.as_pubsub(); let mut conn = conn.as_pubsub();
let cont = crate::utils::get_container_id(); let cont = crate::utils::get_container_id();
@ -105,8 +143,22 @@ async fn get_remote_conf_watcher(conn : &mut Connection) -> Option<Processes> {
} }
None None
} }
// ! once iter exec
// ! only for situation when local isn't None (no need to fck redis server) /// # Fn `get_remote_conf_watcher`
/// ## for trying to pull remote config
///
/// > only for situation when local isn't None (no need to fck redis server)
///
/// *input* : `&str`
///
/// *output* : `None` on empty pubsub or error | `Some(conf)` on succesfull pulling
///
/// *initiator* : fn `get_actual_config`
///
/// *managing* : &str of Redis Server credentials
///
/// *depends on* : struct `Processes`
///
fn once_get_remote_configuration(serv_info: &str) -> Option<Processes> { fn once_get_remote_configuration(serv_info: &str) -> Option<Processes> {
let cont = crate::utils::get_container_id(); let cont = crate::utils::get_container_id();
match Client::open(serv_info) { match Client::open(serv_info) {
@ -165,6 +217,21 @@ fn once_get_remote_configuration(serv_info: &str) -> Option<Processes> {
// ! watchers // ! watchers
/// # Fn `open_watcher`
/// ## for infinitive opening Redis client
///
/// > only for situation when local isn't None (no need to fck redis server)
///
/// *input* : `Option<Processes>`
///
/// *output* : redis::Client on successful opening client
///
/// *initiator* : fn `get_actual_config`
///
/// *managing* : &str of Redis Server credentials
///
/// *depends on* : struct `redis::Client`
///
fn open_watcher(serv_info: &str) -> Client { fn open_watcher(serv_info: &str) -> Client {
loop { loop {
match Client::open(serv_info) { match Client::open(serv_info) {
@ -180,6 +247,21 @@ fn open_watcher(serv_info: &str) -> Client {
} }
} }
/// # Fn `get_connection_watcher`
/// ## for infinitive establishing Redis connection on existing client
///
/// > only for situation when local isn't None (no need to fck redis server)
///
/// *input* : `&Client`
///
/// *output* : `Connection`
///
/// *initiator* : fn `get_actual_config`
///
/// *managing* : &Client for opening connection
///
/// *depends on* : struct `redis::Connection`
///
fn get_connection_watcher(client: &Client) -> Connection { fn get_connection_watcher(client: &Client) -> Connection {
loop { loop {
match client.get_connection() { match client.get_connection() {
@ -197,11 +279,38 @@ fn get_connection_watcher(client: &Client) -> Connection {
} }
} }
/// # Fn `restart_main_thread`
/// ## for restart monitor with new config
///
/// *input* : -
///
/// *output* : `Ok(())` on valid restart | `Err(er)` on error
///
/// *initiator* : fn `subscribe_config_stream`
///
/// *managing* : -
///
/// *depends on* : -
///
fn restart_main_thread() -> std::io::Result<()> { fn restart_main_thread() -> std::io::Result<()> {
let current_exe = env::current_exe()?; let current_exe = env::current_exe()?;
Command::new(current_exe).exec(); Command::new(current_exe).exec();
Ok(()) Ok(())
} }
/// # Fn `subscribe_config_stream`
/// ## for subscribe on changes, pulling to Redis pubsub to get more actual config
///
/// *input* : `Arc<Processes>`
///
/// *output* : `Ok(())` on end of work | `Err(er)` on error with subscribing mechanism
///
/// *initiator* : fn `subscribe_config_stream`
///
/// *managing* : `Arc<Processes>` to compare old config with new pulled
///
/// *depends on* : `Processes`
///
pub async fn subscribe_config_stream(actual_prcs: Arc<Processes>) -> Result<(), CustomError> { pub async fn subscribe_config_stream(actual_prcs: Arc<Processes>) -> Result<(), CustomError> {
if let Ok(client) = Client::open(format!("redis://{}/", &actual_prcs.config_server)) { if let Ok(client) = Client::open(format!("redis://{}/", &actual_prcs.config_server)) {
if let Ok(mut conn) = client.get_connection() { if let Ok(mut conn) = client.get_connection() {
@ -259,6 +368,19 @@ pub async fn subscribe_config_stream(actual_prcs: Arc<Processes>) -> Result<(),
Err(CustomError::Fatal) Err(CustomError::Fatal)
} }
/// # Fn `config_comparing`
/// ## for compare old and new configs
///
/// *input* : local: `&Processes`, remote: `&Processes`
///
/// *output* : `ConfigActuality::Local` or `ConfigActuality::Remote`
///
/// *initiator* : fn `subscribe_config_stream`, fn `get_actual_config`
///
/// *managing* : two objects `&Processes`
///
/// *depends on* : `Processes`, `ConfigActuality`
///
fn config_comparing(local: &Processes, remote: &Processes) -> ConfigActuality { fn config_comparing(local: &Processes, remote: &Processes) -> ConfigActuality {
let local_date: u64 = local.date_of_creation.parse().unwrap(); let local_date: u64 = local.date_of_creation.parse().unwrap();
let remote_date: u64 = remote.date_of_creation.parse().unwrap(); let remote_date: u64 = remote.date_of_creation.parse().unwrap();
@ -277,6 +399,19 @@ fn config_comparing(local: &Processes, remote: &Processes) -> ConfigActuality {
// } // }
// } // }
/// # Fn `save_new_config`
/// ## mechanism for saving new config in local storage
///
/// *input* : `&Processes`, `&str`
///
/// *output* : `Ok(())` on succesfull saving | Err(er) on fs error
///
/// *initiator* : fn `subscribe_config_stream`, fn `get_actual_config`
///
/// *managing* : new config object: `&Processes` and config file name: `&str`
///
/// *depends on* : `Processes`
///
fn save_new_config(config: &Processes, config_file: &str) -> Result<(), CustomError> { fn save_new_config(config: &Processes, config_file: &str) -> Result<(), CustomError> {
match serde_json::to_string_pretty(&config) { match serde_json::to_string_pretty(&config) {
// Ok(st) => match fs::write(config_file, st) { // Ok(st) => match fs::write(config_file, st) {
@ -305,6 +440,19 @@ fn save_new_config(config: &Processes, config_file: &str) -> Result<(), CustomEr
} }
} }
/// # Fn `parse_extern_config`
/// ## for parsing &str to Processes
///
/// *input* : `&str`
///
/// *output* : parsed config in Some(Processes) | None on error with parsing
///
/// *initiator* : fn `subscribe_config_stream`, fn `once_get_remote_configuration`, fn `get_remote_conf`
///
/// *managing* : unparsed config `&str`
///
/// *depends on* : `Processes`
///
fn parse_extern_config(json_string: &str) -> Option<Processes> { fn parse_extern_config(json_string: &str) -> Option<Processes> {
if let Ok(des) = serde_json::from_str::<Processes>(json_string) { if let Ok(des) = serde_json::from_str::<Processes>(json_string) {
return Some(des); return Some(des);