output changes + redis micro chgs to handle reading errors

pull/9/head
prplV 2024-07-22 17:11:45 +03:00
parent 2a121589f7
commit 212f2f2a24
2 changed files with 55 additions and 32 deletions

View File

@ -4,8 +4,7 @@
"name" : "web-server", "name" : "web-server",
"path" : "/home/vladislav/web/web-server", "path" : "/home/vladislav/web/web-server",
"dependencies" : { "dependencies" : {
"files" : [ "files" : [{
{
"filename" : "control-file", "filename" : "control-file",
"src" : "/home/vladislav/web/", "src" : "/home/vladislav/web/",
"triggers" : { "triggers" : {
@ -20,8 +19,7 @@
"onDelete" : "stop", "onDelete" : "stop",
"onChange" : "restart" "onChange" : "restart"
} }
} }],
],
"services" : [{ "services" : [{
"hostname" : "ya.ru", "hostname" : "ya.ru",
"port" : 443, "port" : 443,
@ -37,8 +35,7 @@
"name" : "temp-process", "name" : "temp-process",
"path" : "/home/vladislav/web/temp-process", "path" : "/home/vladislav/web/temp-process",
"dependencies" : { "dependencies" : {
"files" : [ "files" : [{
{
"filename" : "control-file", "filename" : "control-file",
"src" : "/home/vladislav/web/", "src" : "/home/vladislav/web/",
"triggers" : { "triggers" : {
@ -64,6 +61,5 @@
} }
}] }]
} }
} }]
]
} }

View File

@ -118,19 +118,19 @@ async fn main() {
// let mut pubsub = conn.as_pubsub(); // let mut pubsub = conn.as_pubsub();
// let _ = pubsub.subscribe("config"); // let _ = pubsub.subscribe("config");
log::info!("Waitng for a config file in DB ..."); log::info!("Waitng for a config file in DB ...");
// match conn.xlen::<&str, usize>("config_stream") { match conn.xlen::<&str, usize>("config_stream") {
// Ok(len) => { Ok(len) => {
// if len > 0 { if len > 0 {
// } else { } else {
// } }
// }, },
// Err(er) => { Err(er) => {
// error!("Cannot find needed stream to get configuration"); error!("Cannot find needed stream to get configuration");
// return; return;
// }, },
// } }
if let Ok(len) = conn.xlen::<&str, usize>("config_stream") { if let Ok(len) = conn.xlen::<&str, usize>("config_stream") {
if len <= 0 { if len <= 0 {
warn!("No configuration yet. Waiting ..."); warn!("No configuration yet. Waiting ...");
@ -143,16 +143,18 @@ async fn main() {
return; return;
} }
// error // error
let config: Vec<(String, Vec<(String, String)>)> = conn.xrevrange_count("config_stream", "+", "-", 1) let conf: redis::RedisResult<Vec<(String, Vec<(String, String)>)>> = conn.xrevrange_count("config_stream", "+", "-", 1);
.unwrap_or_else(|er| { let mut config: Vec<(String, Vec<(String, String)>)> = Vec::new();
println!("error: {}", er);
return Vec::new();
});
println!("external config: {:?}", config); println!("external config: {:?}", config);
if conf.is_ok() {
let processes: Processes; config = conf.unwrap();
} else {
println!("error: {}", conf.unwrap_err());
}
let processes: Option<Processes>;
if config.is_empty() { 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"); processes = load_processes("settings.json");
} else { } else {
println!("{:?}+++{}", std::time::SystemTime::now(), &config[1].0); println!("{:?}+++{}", std::time::SystemTime::now(), &config[1].0);
@ -172,6 +174,12 @@ async fn main() {
// parse_extern_config(&_temp) // parse_extern_config(&_temp)
// }; // };
if let None = processes {
error!("no config (temp log)");
return;
}
let processes = processes.unwrap();
if processes.processes.len() == 0 { if processes.processes.len() == 0 {
error!("Processes list is null, runner-rs initialization is stopped"); error!("Processes list is null, runner-rs initialization is stopped");
// eprintln!("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 // 4ever sync
fn load_processes(json_filename: &str) -> Processes{ fn load_processes(json_filename: &str) -> Option<Processes>{
let read = fs::read_to_string(json_filename).expect(format!("Missing '{}' file. Cannot start runner", json_filename).as_str()); // let read = fs::read_to_string(json_filename);//.expect(format!("Missing '{}' file. Cannot start runner", json_filename).as_str());
serde_json::from_str::<Processes>(&read).expect(format!("Parsing error in '{}' file. Cannot start runner", json_filename).as_str()) match fs::read_to_string(json_filename) {
Ok(res) => {
match serde_json::from_str::<Processes>(&res) {
Ok(conf) => {
return Some(conf);
},
Err(_) => {
return None;
},
}
},
Err(_) => {
return None;
},
}
} }
fn parse_extern_config(json_string: &str) -> Processes { fn parse_extern_config(json_string: &str) -> Option<Processes> {
serde_json::from_str::<Processes>(json_string).expect("Parsing config file from Redis pubsub error!") let des = serde_json::from_str::<Processes>(json_string);
if des.is_err() {
return None;
} else {
return Some(des.unwrap());
}
} }
async fn get_pid(name: &str) -> Output { async fn get_pid(name: &str) -> Output {