From 08ca2483e1a3ad11ae05fd313002d88f04caf85f Mon Sep 17 00:00:00 2001 From: prplV Date: Wed, 13 Nov 2024 14:11:04 +0300 Subject: [PATCH] docs: config --- src/options/config.rs | 154 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 151 insertions(+), 3 deletions(-) diff --git a/src/options/config.rs b/src/options/config.rs index 7643fdd..5f865ea 100644 --- a/src/options/config.rs +++ b/src/options/config.rs @@ -11,7 +11,19 @@ use tokio::time::Duration; 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 { if let Ok(res) = fs::read_to_string(json_filename) { if let Ok(conf) = serde_json::from_str::(&res) { @@ -21,6 +33,19 @@ fn load_processes(json_filename: &str) -> Option { 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 { // * if no local conf -> loop and +inf 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 { } } +/// # 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 { let mut conn = conn.as_pubsub(); let cont = crate::utils::get_container_id(); @@ -105,8 +143,22 @@ async fn get_remote_conf_watcher(conn : &mut Connection) -> Option { } 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 { let cont = crate::utils::get_container_id(); match Client::open(serv_info) { @@ -165,6 +217,21 @@ fn once_get_remote_configuration(serv_info: &str) -> Option { // ! 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` +/// +/// *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 { loop { 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 { loop { 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<()> { let current_exe = env::current_exe()?; Command::new(current_exe).exec(); Ok(()) } + +/// # Fn `subscribe_config_stream` +/// ## for subscribe on changes, pulling to Redis pubsub to get more actual config +/// +/// *input* : `Arc` +/// +/// *output* : `Ok(())` on end of work | `Err(er)` on error with subscribing mechanism +/// +/// *initiator* : fn `subscribe_config_stream` +/// +/// *managing* : `Arc` to compare old config with new pulled +/// +/// *depends on* : `Processes` +/// pub async fn subscribe_config_stream(actual_prcs: Arc) -> Result<(), CustomError> { if let Ok(client) = Client::open(format!("redis://{}/", &actual_prcs.config_server)) { if let Ok(mut conn) = client.get_connection() { @@ -259,6 +368,19 @@ pub async fn subscribe_config_stream(actual_prcs: Arc) -> Result<(), 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 { let local_date: u64 = local.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> { match serde_json::to_string_pretty(&config) { // 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 { if let Ok(des) = serde_json::from_str::(json_string) { return Some(des);