diff --git a/settings.json b/settings.json index dfb86c6..b13319a 100644 --- a/settings.json +++ b/settings.json @@ -4,8 +4,7 @@ "name" : "web-server", "path" : "/home/vladislav/web/web-server", "dependencies" : { - "files" : [ - { + "files" : [{ "filename" : "control-file", "src" : "/home/vladislav/web/", "triggers" : { @@ -20,8 +19,7 @@ "onDelete" : "stop", "onChange" : "restart" } - } - ], + }], "services" : [{ "hostname" : "ya.ru", "port" : 443, @@ -37,8 +35,7 @@ "name" : "temp-process", "path" : "/home/vladislav/web/temp-process", "dependencies" : { - "files" : [ - { + "files" : [{ "filename" : "control-file", "src" : "/home/vladislav/web/", "triggers" : { @@ -64,6 +61,5 @@ } }] } - } - ] + }] } diff --git a/src/main.rs b/src/main.rs index f931414..c0f62ea 100644 --- a/src/main.rs +++ b/src/main.rs @@ -118,19 +118,19 @@ async fn main() { // 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 { + match conn.xlen::<&str, usize>("config_stream") { + Ok(len) => { + if len > 0 { - // } else { + } else { - // } - // }, - // Err(er) => { - // error!("Cannot find needed stream to get configuration"); - // return; - // }, - // } + } + }, + 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 ..."); @@ -143,16 +143,18 @@ async fn main() { return; } // error - let config: Vec<(String, Vec<(String, String)>)> = conn.xrevrange_count("config_stream", "+", "-", 1) - .unwrap_or_else(|er| { - println!("error: {}", er); - return Vec::new(); - }); + let conf: redis::RedisResult)>> = conn.xrevrange_count("config_stream", "+", "-", 1); + let mut config: Vec<(String, Vec<(String, String)>)> = Vec::new(); println!("external config: {:?}", config); - - let processes: Processes; + if conf.is_ok() { + config = conf.unwrap(); + } else { + println!("error: {}", conf.unwrap_err()); + } + let processes: Option; if config.is_empty() { - error!("No suitable config was given. Getting local config..."); + error!("No suitable configs were given!"); + log::info!("Trying to get local config..."); processes = load_processes("settings.json"); } else { println!("{:?}+++{}", std::time::SystemTime::now(), &config[1].0); @@ -172,6 +174,12 @@ async fn main() { // parse_extern_config(&_temp) // }; + if let None = processes { + error!("no config (temp log)"); + return; + } + let processes = processes.unwrap(); + 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"); @@ -340,12 +348,31 @@ async fn run_daemons( } // 4ever sync -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 load_processes(json_filename: &str) -> Option{ + // let read = fs::read_to_string(json_filename);//.expect(format!("Missing '{}' file. Cannot start runner", json_filename).as_str()); + match fs::read_to_string(json_filename) { + Ok(res) => { + match serde_json::from_str::(&res) { + Ok(conf) => { + return Some(conf); + }, + Err(_) => { + return None; + }, + } + }, + Err(_) => { + return None; + }, + } } -fn parse_extern_config(json_string: &str) -> Processes { - serde_json::from_str::(json_string).expect("Parsing config file from Redis pubsub error!") +fn parse_extern_config(json_string: &str) -> Option { + let des = serde_json::from_str::(json_string); + if des.is_err() { + return None; + } else { + return Some(des.unwrap()); + } } async fn get_pid(name: &str) -> Output {