reding config lc+pubsub rework
parent
064611823a
commit
35a21da431
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"dateOfCreation": "1721381809110",
|
"dateOfCreation": "1721381809112",
|
||||||
"configServer": "localhost",
|
"configServer": "192.168.2.37",
|
||||||
"processes": [
|
"processes": [
|
||||||
{
|
{
|
||||||
"name": "temp-process",
|
"name": "temp-process",
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,7 @@ use tokio::time::{sleep, Duration};
|
||||||
use std::{borrow::BorrowMut, net::{IpAddr, Ipv4Addr}};
|
use std::{borrow::BorrowMut, net::{IpAddr, Ipv4Addr}};
|
||||||
// use std::io::BufReader;
|
// use std::io::BufReader;
|
||||||
use tokio::io::{BufReader, AsyncWriteExt, AsyncBufReadExt};
|
use tokio::io::{BufReader, AsyncWriteExt, AsyncBufReadExt};
|
||||||
|
use tokio::{io::AsyncReadExt, net::UnixListener};
|
||||||
use noxis_cli::Cli;
|
use noxis_cli::Cli;
|
||||||
use serde_json::from_str;
|
use serde_json::from_str;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -119,12 +119,6 @@ pub mod v2 {
|
||||||
let _ = restart_main_thread();
|
let _ = restart_main_thread();
|
||||||
},
|
},
|
||||||
cli_config_option = cli_future => {
|
cli_config_option = cli_future => {
|
||||||
// match cli_config_option {
|
|
||||||
// Some(config) => {},
|
|
||||||
// None => {
|
|
||||||
// error!("Cli pulling new config mechanism crushed, restarting ...")
|
|
||||||
// },
|
|
||||||
// }
|
|
||||||
match cli_config_option {
|
match cli_config_option {
|
||||||
Err(_) => error!("Cli pulling new config mechanism crushed, restarting ..."),
|
Err(_) => error!("Cli pulling new config mechanism crushed, restarting ..."),
|
||||||
Ok(option_config) => {
|
Ok(option_config) => {
|
||||||
|
|
@ -144,22 +138,20 @@ pub mod v2 {
|
||||||
// TODO! futures + select! [OK]
|
// TODO! futures + select! [OK]
|
||||||
// TODO! tests config
|
// TODO! tests config
|
||||||
}
|
}
|
||||||
pub async fn get_redis_connection(params: Arc<PrebootParams>) -> Option<Connection> {
|
pub async fn get_redis_connection(params: &str) -> Option<Connection> {
|
||||||
if params.no_sub {
|
for i in 1..=3 {
|
||||||
return None;
|
let redis_url = format!("redis://{}/", params);
|
||||||
}
|
info!("Trying to connect Redis pubsub `{}`. Attempt {}", &redis_url, i);
|
||||||
let mut connection_delay: u64 = 1;
|
if let Ok(client) = Client::open(redis_url) {
|
||||||
loop {
|
|
||||||
if let Ok(client) = Client::open(format!("redis://{}/", ¶ms.remote_server_url)) {
|
|
||||||
if let Ok(conn) = client.get_connection() {
|
if let Ok(conn) = client.get_connection() {
|
||||||
info!("Successfully opened Redis connection");
|
info!("Successfully opened Redis connection");
|
||||||
return Some(conn);
|
return Some(conn);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
error!("Error with subscribing Redis stream on update. Retrying in {} secs...", connection_delay);
|
error!("Error with subscribing Redis stream on update. Retrying in 5 secs...");
|
||||||
sleep(Duration::from_secs(connection_delay)).await;
|
sleep(Duration::from_secs(5)).await;
|
||||||
connection_delay *= 2;
|
|
||||||
}
|
}
|
||||||
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
// loop checking redis pubsub
|
// loop checking redis pubsub
|
||||||
|
|
@ -171,29 +163,15 @@ pub mod v2 {
|
||||||
) -> anyhow::Result<()>{
|
) -> anyhow::Result<()>{
|
||||||
/*...*/
|
/*...*/
|
||||||
// dbg!("start of pb");
|
// dbg!("start of pb");
|
||||||
let mut tx_brd_local = tx_brd_local;
|
sleep(Duration::from_secs(1)).await;
|
||||||
let mut _local_config = Processes::default();
|
|
||||||
return match get_redis_connection(params.clone()).await {
|
|
||||||
Some(mut conn) => {
|
|
||||||
//
|
|
||||||
let mut pub_sub = conn.as_pubsub();
|
|
||||||
let channel_name = get_container_id().unwrap_or(String::from("default"));
|
|
||||||
let channel_name = channel_name.trim();
|
|
||||||
match pub_sub.subscribe(channel_name) {
|
|
||||||
Err(er) => {
|
|
||||||
error!("Cannot subscribe pubsub channel due to {}", &er);
|
|
||||||
Err(anyhow::Error::msg(format!("Cannot subscribe pubsub channel due to {}", er)))
|
|
||||||
},
|
|
||||||
Ok(_) => {
|
|
||||||
info!("Successfully subscribed to {} pubsub channel", channel_name);
|
|
||||||
loop {
|
|
||||||
// brd check
|
|
||||||
// if let Ok(new_lc) = tx_brd_local.recv().await {
|
|
||||||
|
|
||||||
// }
|
let mut tx_brd_local = tx_brd_local;
|
||||||
|
let mut local_config = Processes::default();
|
||||||
|
|
||||||
|
for retry in 1..=5 {
|
||||||
if !tx_brd_local.is_empty() {
|
if !tx_brd_local.is_empty() {
|
||||||
match tx_brd_local.recv().await {
|
match tx_brd_local.recv().await {
|
||||||
Ok(lc) => _local_config = lc,
|
Ok(lc) => local_config = lc,
|
||||||
Err(er) => {
|
Err(er) => {
|
||||||
error!("Cannot get imported local config due to {}", &er);
|
error!("Cannot get imported local config due to {}", &er);
|
||||||
return Err(anyhow::Error::msg(
|
return Err(anyhow::Error::msg(
|
||||||
|
|
@ -202,6 +180,20 @@ pub mod v2 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
match get_redis_connection(&local_config.config_server).await {
|
||||||
|
Some(mut conn) => {
|
||||||
|
//
|
||||||
|
let mut pub_sub = conn.as_pubsub();
|
||||||
|
let channel_name = get_container_id().unwrap_or(String::from("default"));
|
||||||
|
let channel_name = channel_name.trim();
|
||||||
|
match pub_sub.subscribe(channel_name) {
|
||||||
|
Err(er) => {
|
||||||
|
error!("Cannot subscribe pubsub channel due to {}", &er);
|
||||||
|
return Err(anyhow::Error::msg(format!("Cannot subscribe pubsub channel due to {}", er)))
|
||||||
|
},
|
||||||
|
Ok(_) => {
|
||||||
|
info!("Successfully subscribed to {} pubsub channel", channel_name);
|
||||||
|
loop {
|
||||||
// pubsub check
|
// pubsub check
|
||||||
if let Ok(msg) = pub_sub.get_message() {
|
if let Ok(msg) = pub_sub.get_message() {
|
||||||
// dbg!("ok on get message");
|
// dbg!("ok on get message");
|
||||||
|
|
@ -210,7 +202,7 @@ pub mod v2 {
|
||||||
Err(_) => error!("Cannot read new config from Redis channel. Check network or Redis configuration "),
|
Err(_) => error!("Cannot read new config from Redis channel. Check network or Redis configuration "),
|
||||||
Ok(payload) => {
|
Ok(payload) => {
|
||||||
if let Some(remote) = parse_extern_config(&payload) {
|
if let Some(remote) = parse_extern_config(&payload) {
|
||||||
match config_comparing(&_local_config, &remote) {
|
match config_comparing(&local_config, &remote) {
|
||||||
ConfigActuality::Local => {
|
ConfigActuality::Local => {
|
||||||
warn!("Pulled new config from Redis channel, it's outdated. Ignoring ...");
|
warn!("Pulled new config from Redis channel, it's outdated. Ignoring ...");
|
||||||
},
|
},
|
||||||
|
|
@ -243,9 +235,17 @@ pub mod v2 {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
None => Err(anyhow::Error::msg("Cannot create Redis connection"))
|
None => {
|
||||||
|
warn!("Cannot validly connect Redis connection. Blocking task for 20 secs and restarting tries (attempt {})", retry);
|
||||||
|
sleep(Duration::from_secs(20)).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
error!("End of retries. Stopping pubsub...");
|
||||||
|
return Err(anyhow::Error::msg(
|
||||||
|
format!("End of retries. Stopping pubsub...")
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
async fn local_config_reciever(
|
async fn local_config_reciever(
|
||||||
|
|
@ -373,7 +373,7 @@ pub mod v2 {
|
||||||
to_local_tx: OneShotSender<bool>
|
to_local_tx: OneShotSender<bool>
|
||||||
) -> Option<Processes> {
|
) -> Option<Processes> {
|
||||||
/* match awaits til channel*/
|
/* match awaits til channel*/
|
||||||
dbg!("start of cli");
|
// dbg!("start of cli");
|
||||||
match cli_oneshot.await {
|
match cli_oneshot.await {
|
||||||
Ok(config_from_cli) => {
|
Ok(config_from_cli) => {
|
||||||
info!("New actual config `{}` from CLI was pulled. Saving and restaring ...", &config_from_cli.date_of_creation);
|
info!("New actual config `{}` from CLI was pulled. Saving and restaring ...", &config_from_cli.date_of_creation);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue