diff --git a/Cargo.lock b/Cargo.lock index 32a1931..5cfa34c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -167,6 +167,16 @@ version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0b6a852b24ab71dffc585bcb46eaf7959d175cb865a7152e35b348d1b2960422" +[[package]] +name = "combine" +version = "4.6.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ba5a308b75df32fe02788e748662718f03fde005016435c444eea572398219fd" +dependencies = [ + "bytes", + "memchr", +] + [[package]] name = "core-foundation-sys" version = "0.8.6" @@ -196,6 +206,15 @@ dependencies = [ "log", ] +[[package]] +name = "form_urlencoded" +version = "1.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e13624c2627564efccf4934284bdd98cbaa14e79b0b5a141218e507b3a823456" +dependencies = [ + "percent-encoding", +] + [[package]] name = "futures-core" version = "0.3.30" @@ -243,6 +262,16 @@ dependencies = [ "cc", ] +[[package]] +name = "idna" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "634d9b1461af396cad843f47fdba5597a4f9e6ddd4bfb6ff5d85028c25cb12f6" +dependencies = [ + "unicode-bidi", + "unicode-normalization", +] + [[package]] name = "inotify" version = "0.10.2" @@ -391,6 +420,12 @@ dependencies = [ "windows-targets 0.52.5", ] +[[package]] +name = "percent-encoding" +version = "2.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" + [[package]] name = "pin-project-lite" version = "0.2.14" @@ -415,6 +450,21 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "redis" +version = "0.25.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0d7a6955c7511f60f3ba9e86c6d02b3c3f144f8c24b288d1f4e18074ab8bbec" +dependencies = [ + "combine", + "itoa", + "percent-encoding", + "ryu", + "sha1_smol", + "socket2", + "url", +] + [[package]] name = "redox_syscall" version = "0.5.2" @@ -455,12 +505,13 @@ checksum = "7a66a03ae7c801facd77a29370b4faec201768915ac14a721ba36f20bc9c209b" [[package]] name = "runner-rs" -version = "0.4.2" +version = "0.4.5" dependencies = [ "chrono", "env_logger", "inotify", "log", + "redis", "serde", "serde_json", "tokio", @@ -515,6 +566,12 @@ dependencies = [ "serde", ] +[[package]] +name = "sha1_smol" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae1a47186c03a32177042e55dbc5fd5aee900b8e0069a8d70fba96a9375cd012" + [[package]] name = "signal-hook-registry" version = "1.4.2" @@ -551,6 +608,21 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "tinyvec" +version = "1.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "445e881f4f6d382d5f27c034e25eb92edd7c784ceab92a0937db7f2e9471b938" +dependencies = [ + "tinyvec_macros", +] + +[[package]] +name = "tinyvec_macros" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" + [[package]] name = "tokio" version = "1.38.0" @@ -581,12 +653,38 @@ dependencies = [ "syn", ] +[[package]] +name = "unicode-bidi" +version = "0.3.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "08f95100a766bf4f8f28f90d77e0a5461bbdb219042e7679bebe79004fed8d75" + [[package]] name = "unicode-ident" version = "1.0.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" +[[package]] +name = "unicode-normalization" +version = "0.1.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a56d1686db2308d901306f92a263857ef59ea39678a5458e7cb17f01415101f5" +dependencies = [ + "tinyvec", +] + +[[package]] +name = "url" +version = "2.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22784dbdf76fdde8af1aeda5622b546b422b6fc585325248a2bf9f5e41e94d6c" +dependencies = [ + "form_urlencoded", + "idna", + "percent-encoding", +] + [[package]] name = "utf8parse" version = "0.2.2" diff --git a/Cargo.toml b/Cargo.toml index c86f3af..f5b42a9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "runner-rs" -version = "0.4.2" +version = "0.4.5" edition = "2021" [dependencies] @@ -8,6 +8,7 @@ chrono = "0.4.38" env_logger = "0.11.3" inotify = "0.10.2" log = "0.4.22" +redis = "0.25.4" serde = { version = "1.0.203", features = ["derive"] } serde_json = "1.0.118" tokio = { version = "1.38.0", features = ["full", "time"] } diff --git a/src/main.rs b/src/main.rs index 62a40be..3accf9a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +use redis::Commands; // json parsing use serde::{ Deserialize, Serialize }; use serde_json; @@ -6,10 +7,11 @@ use tokio::time::{ Duration, Instant }; use tokio::sync::mpsc; use tokio::join; // fatal errors handler -use core::panic; +use core::{panic, time}; // utils use std::fmt::Debug; use std::fs; +use std::os::unix::process; use std::path::Path; use std::process::{ Command, Output }; use std::sync::Arc; @@ -19,7 +21,7 @@ use inotify::{ Inotify, WatchMask }; use std::io::Write; use chrono::Local; use env_logger::Builder; -use log::{error, max_level, warn, LevelFilter}; +use log::{error, log, max_level, warn, LevelFilter}; /// # an Error enum (nextly will be deleted and replaced) enum CustomError { @@ -108,9 +110,57 @@ async fn main() { .filter(None, LevelFilter::Info) .init(); - let processes = load_processes("settings.json"); + // OLD STYLE OF USING LOCAL CONFIG FILE + // let processes = load_processes("settings.json"); // let mut error_counter = 0; - + let client = redis::Client::open("redis://127.0.0.1").expect("error connecting redis server"); + let mut conn = client.get_connection().expect("error getting connection"); + // let mut pubsub = conn.as_pubsub(); + // let _ = pubsub.subscribe("config"); + log::info!("Waitng for a config file in DB ..."); + // match conn.xlen::<&str, usize>("config_stream") { + // Ok(len) => { + // if len > 0 { + + // } else { + + // } + // }, + // Err(er) => { + // error!("Cannot find needed stream to get configuration"); + // return; + // }, + // } + if let Ok(len) = conn.xlen::<&str, usize>("config_stream") { + if len <= 0 { + warn!("No configuration yet. Waiting ..."); + while conn.xlen::<&str, usize>("config_stream").unwrap() <= 0 { + std::thread::sleep(Duration::from_millis(400)); + } + } + } else { + error!("Cannot find needed stream to get configuration"); + return; + } + let config: String = conn.xrevrange("config_stream", "", "").inspect_err(|_| { + error!("Cannot fetch config"); + return String::new(); + }); + let processes = parse_extern_config(&config); + // let processes: Processes = { + // let mut _temp = String::new(); + // loop { + // if let Ok(msg) = pubsub.get_message() { + // log::info!("Actual configuration is found !"); + // _temp = msg.get_payload::().expect("error getting payload"); + // break; + // } else { + // continue; + // }; + // } + // parse_extern_config(&_temp) + // }; + if processes.processes.len() == 0 { error!("Processes list is null, runner-rs initialization is stopped"); // eprintln!("Error: Processes list is null, runner-rs initialization is stopped"); @@ -279,6 +329,9 @@ fn load_processes(json_filename: &str) -> Processes{ let read = fs::read_to_string(json_filename).expect(format!("Missing '{}' file. Cannot start runner", json_filename).as_str()); serde_json::from_str::(&read).expect(format!("Parsing error in '{}' file. Cannot start runner", json_filename).as_str()) } +fn parse_extern_config(json_string: &str) -> Processes { + serde_json::from_str::(json_string).expect("Parsing config file from Redis pubsub error!") +} async fn get_pid(name: &str) -> Output { let name = Arc::new(name.to_string());