preboot fix work with config params + refactor

pull/14/head
prplV 2024-12-16 11:44:34 +03:00
parent 2dbfb4a93a
commit 56a20eb65c
1 changed files with 17 additions and 15 deletions

View File

@ -7,10 +7,11 @@ use std::os::unix::process::CommandExt;
use std::process::Command; use std::process::Command;
use std::sync::Arc; use std::sync::Arc;
use std::{env, fs}; use std::{env, fs};
use tokio::time::Duration; use std::fmt::format;
use super::preboot::PrebootParams; use super::preboot::PrebootParams;
use tokio::time::{Duration, sleep};
const CONFIG_PATH: &str = "settings.json"; // const CONFIG_PATH: &str = "settings.json";
/// # Fn `load_processes` /// # Fn `load_processes`
/// ## for reading and parsing *local* storing config /// ## for reading and parsing *local* storing config
@ -70,7 +71,7 @@ pub async fn get_actual_config(params : Arc<PrebootParams>) -> Option<Processes>
} }
ConfigActuality::Remote => { ConfigActuality::Remote => {
info!("Pulled config is more actual. Saving changes!"); info!("Pulled config is more actual. Saving changes!");
if save_new_config(&remote_conf, CONFIG_PATH).is_err() { if save_new_config(&remote_conf, config_path).is_err() {
error!("Saving changes process failed due to unexpected error...") error!("Saving changes process failed due to unexpected error...")
} }
Some(remote_conf) Some(remote_conf)
@ -83,11 +84,10 @@ pub async fn get_actual_config(params : Arc<PrebootParams>) -> Option<Processes>
None => { None => {
warn!("No local valid conf was found. Trying to pull remote one..."); warn!("No local valid conf was found. Trying to pull remote one...");
if !params.no_remote_config { if !params.no_remote_config {
let mut conn = get_connection_watcher(&open_watcher("redis://localhost/")); let mut conn = get_connection_watcher(&open_watcher(&format!("redis://{}/", &params.remote_server_url)));
let remote_config = get_remote_conf_watcher(&mut conn).await; if let Some(conf) = get_remote_conf_watcher(&mut conn).await {
if let Some(conf) = remote_config {
info!("Config {} was pulled from Redis-Server. Starting...", &conf.date_of_creation); info!("Config {} was pulled from Redis-Server. Starting...", &conf.date_of_creation);
let _ = save_new_config(&conf, CONFIG_PATH); let _ = save_new_config(&conf, config_path);
return Some(conf); return Some(conf);
} }
} }
@ -189,23 +189,23 @@ fn once_get_remote_configuration(serv_info: &str) -> Option<Processes> {
if remote.is_none() { if remote.is_none() {
error!("Pulled config is invalid. Check it in Redis Server"); error!("Pulled config is invalid. Check it in Redis Server");
} }
return remote; remote
}, },
Err(_) => { Err(_) => {
error!("Cannot extract payload from new message. Check Redis Server state"); error!("Cannot extract payload from new message. Check Redis Server state");
return None; None
}, },
} }
}, },
Err(_) => { Err(_) => {
warn!("Cannot get config from Redis Server. Empty channel"); warn!("Cannot get config from Redis Server. Empty channel");
return None; None
}, },
} }
}, },
Err(_) => { Err(_) => {
error!("Redis subscription process failed. Check Redis configuration!"); error!("Redis subscription process failed. Check Redis configuration!");
return None; None
} }
} }
} }
@ -319,6 +319,8 @@ fn restart_main_thread() -> std::io::Result<()> {
/// *depends on* : `Processes` /// *depends on* : `Processes`
/// ///
pub async fn subscribe_config_stream(actual_prcs: Arc<Processes>, params: Arc<PrebootParams>) -> Result<(), CustomError> { pub async fn subscribe_config_stream(actual_prcs: Arc<Processes>, params: Arc<PrebootParams>) -> Result<(), CustomError> {
let config_path = params.config.to_str().unwrap_or_else(|| "settings.json");
if params.no_sub || params.no_remote_config { if params.no_sub || params.no_remote_config {
return Err(CustomError::Fatal); return Err(CustomError::Fatal);
} }
@ -340,8 +342,8 @@ pub async fn subscribe_config_stream(actual_prcs: Arc<Processes>, params: Arc<Pr
match config_comparing(&actual_prcs, &remote_config) { match config_comparing(&actual_prcs, &remote_config) {
ConfigActuality::Remote => { ConfigActuality::Remote => {
warn!("Pulled config is actual. Saving and restarting..."); warn!("Pulled config is actual. Saving and restarting...");
if save_new_config(&remote_config, CONFIG_PATH).is_err() { if save_new_config(&remote_config, config_path).is_err() {
error!("Error with saving new config to {}. Stopping sub mechanism...", &CONFIG_PATH); error!("Error with saving new config to {}. Stopping sub mechanism...", config_path);
return Err(CustomError::Fatal); return Err(CustomError::Fatal);
} }
if restart_main_thread().is_err() { if restart_main_thread().is_err() {
@ -362,7 +364,7 @@ pub async fn subscribe_config_stream(actual_prcs: Arc<Processes>, params: Arc<Pr
}, },
} }
} }
tokio::time::sleep(tokio::time::Duration::from_secs(30)).await; sleep(Duration::from_secs(30)).await;
} }
} else { } else {
error!("Cannot subscribe channel {}. Check Redis Server status", &channel_name); error!("Cannot subscribe channel {}. Check Redis Server status", &channel_name);
@ -443,7 +445,7 @@ fn save_new_config(config: &Processes, config_file: &str) -> Result<(), CustomEr
Err(_) => Err(CustomError::Fatal), Err(_) => Err(CustomError::Fatal),
} }
} }
Err(_) => return Err(CustomError::Fatal), Err(_) => Err(CustomError::Fatal),
} }
} }
Err(_) => Err(CustomError::Fatal), Err(_) => Err(CustomError::Fatal),