diff --git a/src/defs.rs b/src/defs.rs index 175f758..9f108cd 100644 --- a/src/defs.rs +++ b/src/defs.rs @@ -81,7 +81,7 @@ pub type MapCheckInfo = BTreeMap>; #[derive(Clone, Debug, Serialize, Deserialize, Default)] pub struct KldCheck { pub name: String, - pub cloud: HashMap, + pub liveness: HashMap, pub apps: HashMap, pub infos: Vec, } diff --git a/src/handlers/h_config.rs b/src/handlers/h_config.rs index 901d70c..d3ab3b4 100644 --- a/src/handlers/h_config.rs +++ b/src/handlers/h_config.rs @@ -15,12 +15,13 @@ use kloud::{ }; use clds::clouds::defs::{Cloud}; use crate::defs::{DataDBs}; // ,CollsData}; -use clds::clouds::on_clouds::{on_cloud_req,on_cloud_name_req_info,get_cloud_check,get_apps_check}; +use clds::clouds::on_req::{on_cloud_req,on_cloud_name_req_info,get_cache_data}; pub async fn cloud_req(reqname: &str,cloud: &Cloud,reqenv: &ReqEnv,opts: &KloudQueryConfigFilters) -> String { let mut result: String; - let mut liveness_result = String::from(""); - let mut apps_result = String::from(""); + let mut liveness_result = String::from("\"\""); + let mut apps_result = String::from("\"\""); + let mut status_result = String::from("\"\""); if opts.grp.is_empty() { result = on_cloud_req(&reqname,&cloud,&reqenv,&opts.tsksrvcs,&opts.srvrs,&opts.cld).await; } else { @@ -30,15 +31,23 @@ pub async fn cloud_req(reqname: &str,cloud: &Cloud,reqenv: &ReqEnv,opts: &KloudQ } else { source = format!("{}/{}/{}",&opts.cld,&opts.grp,&opts.name); } - result = on_cloud_name_req_info(&reqname,&cloud,&reqenv,&opts.tsksrvcs,&opts.srvrs,&source).await; + let info = on_cloud_name_req_info(&reqname,&cloud,&reqenv,&opts.tsksrvcs,&opts.srvrs,"",&source).await; + result = serde_json::to_string(&info).unwrap_or_else(|e|{ + println!("Error serde info {} {}: ",&reqname,e); + String::from("") + }); } +// cloud_result = get_cloud_cache_req(&reqenv,&cloud, reqname: &str, reqname_job: &str).await if opts.tsksrvcs.contains("liveness") { - liveness_result = get_cloud_check(&reqenv).await; + liveness_result = get_cache_data("liveness",&reqenv).await; } if opts.tsksrvcs.contains("apps") { - apps_result = get_apps_check(&reqenv).await; + apps_result = get_cache_data("apps",&reqenv).await; } - result = format!("{}:{}{}:{}{}:{}{}",r#"{"req""#,&result,r#","apps""#,&apps_result,r#","liveness""#,&liveness_result,r#"}"#); + if opts.tsksrvcs.contains("status") { + status_result = get_cache_data("status",&reqenv).await; + } + result = format!("{}:{}{}:{}{}:{}{}:{}{}",r#"{"clouds""#,&result,r#","apps""#,&apps_result,r#","status""#,&status_result,r#","liveness""#,&liveness_result,r#"}"#); result.to_owned() } pub async fn config ( diff --git a/src/handlers/h_defs.rs b/src/handlers/h_defs.rs index 8c8c4d5..75a081e 100644 --- a/src/handlers/h_defs.rs +++ b/src/handlers/h_defs.rs @@ -35,7 +35,32 @@ use kloud::{ }; use crate::defs::{DataDBs}; // ,CollsData}; +use app_auth::{User}; +pub async fn get_defs_from_req(reqenv: &ReqEnv, prfx: String, user: &User) -> String { + let prfx_path: String; + if prfx.is_empty() { + prfx_path = String::from("ui"); + } else { + prfx_path = prfx; + } + let mut path: String; + if user.user_id.is_empty() { + path = format!("{}/profiles/{}/{}/defs.yaml",reqenv.websrvr().resources_path,&prfx_path,"default"); + } else { + path = format!("{}/profiles/{}/defs.yaml",reqenv.websrvr().resources_path,&prfx_path); + } + if ! std::path::Path::new(&path).exists() { + path = format!("{}/profiles/{}/defs.yaml",reqenv.websrvr().resources_path,&prfx_path); + } + let content = Profile::load_fs_content(path.into()); + // let lang = opts.lang.unwrap_or_else(|| String::from("es")); + // let section = opts.section.unwrap_or_else(|| String::from("")); + // let lang_items = LangItems::new("langs/ta",&lang,"yaml"); + // let result = lang_items.get_items_str(§ion); + let res = Profile::to_yaml(content); // String::from(""); + serde_json::to_string(&res).unwrap_or_else(|_| String::from("")) +} // warp::generic::Either<(std::string::String,), (std::string::String,)> pub async fn langs( opts: KloudQueryLangFilters, @@ -105,7 +130,7 @@ pub async fn defs( let reqenv = ReqEnv::new(db.app, db.auth, header, method, "/defs", "defs", &prfx); // let allow_origin = reqenv.websrvr().allow_origin; match reqenv.user_authentication().await { - Ok(auth) => { + Ok(_auth) => { // dbg!("auth: {}",&auth); // println!("User: {} | {}",&user.user_id,&user.token); // if let Some(lang) = reqtasks.params().get("lang") { @@ -115,18 +140,7 @@ pub async fn defs( // } // log::debug!("LANG: {} - {}",language, lang); // dbg!("LANG: {} - {}",language, lang); - - let mut path = format!("{}/profiles/{}/{}/defs.yaml",reqenv.websrvr().resources_path,&prfx,&auth.user_id); - if ! std::path::Path::new(&path).exists() { - path = format!("{}/profiles/{}/defs.yaml",reqenv.websrvr().resources_path,&prfx); - } - let content = Profile::load_fs_content(path.into()); - // let lang = opts.lang.unwrap_or_else(|| String::from("es")); - // let section = opts.section.unwrap_or_else(|| String::from("")); - // let lang_items = LangItems::new("langs/ta",&lang,"yaml"); - // let result = lang_items.get_items_str(§ion); - let res = Profile::to_yaml(content); // String::from(""); - let result = serde_json::to_string(&res).unwrap_or_else(|_| String::from("")); + let result = get_defs_from_req(&reqenv, prfx.to_owned(), &reqenv.get_user().await).await; Ok(warp::http::Response::builder() .body(result.to_string()) .into_response()) diff --git a/src/handlers/h_home.rs b/src/handlers/h_home.rs index 2336a54..a04a8c6 100644 --- a/src/handlers/h_home.rs +++ b/src/handlers/h_home.rs @@ -15,10 +15,12 @@ use app_auth::{UserCtx}; // }; use clds::defs::{KloudCheckHome,CloudCheckItem}; use clds::status::{get_statusinfo_fileslist,load_statusinfo}; -use clds::clouds::defs::{Cloud,SrvcsHostInfOut,AppOut,AppsrvcInfo,AppsrvcInfOut, TskSrvcOut, TsksrvcInfo, TsksrvcInfOut}; +use clds::clouds::defs::{Cloud,SrvcsHostInfOut,AppOut,AppsrvcInfo,AppsrvcInfOut, TskSrvcOut, TsksrvcInfo, TsksrvcInfOut,InfoStatus}; +use clds::clouds::on_req::{get_cache_data}; +// use app_env::appenv::{Rol,Policy}; + use crate::defs::{KldCheck, MapCheckInfo, DataDBs}; // ,CollsData}; -use clds::clouds::on_clouds::{get_cloud_check,get_apps_check}; -use app_env::appenv::{Rol,Policy}; +use crate::handlers::h_defs::{get_defs_from_req}; pub fn tsks_info_on_role(role: String, tsksrvcs: Vec) -> Vec { tsksrvcs.into_iter().map(|tsk| { @@ -93,21 +95,35 @@ pub fn collect_clouds_check_items(role: &str, items: &Vec ) -> B }); m_items } -pub async fn get_tsks_apps_check(reqenv: &ReqEnv,_auth: UserCtx, role: &str) -> String { - let result_apps = get_apps_check(&reqenv).await; - // println!("Result apps: {}",&result_apps); - let res_apps: Vec = serde_json::from_str(&result_apps).unwrap_or_else(|e| { - println!("Error serde apps json: {}",e); - Vec::new() - }); - let result_cloud = get_cloud_check(&reqenv).await; - // println!("Result cloud: {}",&result_cloud); - let res_cloud: Vec = serde_json::from_str(&result_cloud).unwrap_or_else(|e| { - println!("Error serde apps json: {}",e); +pub async fn get_info_status(reqenv: &ReqEnv,kld: &KloudCheckHome) -> Vec { + let kld_statusinfo_files: Vec = get_statusinfo_fileslist(&reqenv.config().state_path,&kld.name).await.unwrap_or_else(|e|{ + println!("Error on infos {}: {}",&kld.name,e); + Vec::new() + }); + load_statusinfo(&format!("{}/{}",&reqenv.config().state_path,&kld.name), kld_statusinfo_files).await +} +pub async fn get_tsks_apps_check(reqenv: &ReqEnv,_auth: UserCtx, role: &str, prfx: String) -> String { + let result_data: String; + let res_liveness: Vec; + let result_status = get_cache_data("status",&reqenv).await; + if result_status.is_empty() { + let result_liveness = get_cache_data("liveness",&reqenv).await; + result_data = get_cache_data("apps",&reqenv).await; + res_liveness = serde_json::from_str(&result_liveness).unwrap_or_else(|e| { + println!("Error serde liveness json: {}",e); + Vec::new() + }); + } else { + result_data = result_status; + res_liveness = Vec::new(); + } + let res_data: Vec = serde_json::from_str(&result_data).unwrap_or_else(|e| { + println!("Error serde status json: {}",e); Vec::new() }); + let mut kld_check: Vec = Vec::new(); - res_apps.iter().for_each(|kld| { + for kld in res_data { // let mut list_groups = String::from(""); let mut grp_check_hash: HashMap = HashMap::new(); kld.groups.iter().for_each(|grp| { @@ -117,31 +133,26 @@ pub async fn get_tsks_apps_check(reqenv: &ReqEnv,_auth: UserCtx, role: &str) -> // list_groups.push_str(&grp.name); grp_check_hash.insert(format!("{}",grp.name),collect_clouds_check_items(&role,&grp.items)); }); + kld_check.push(KldCheck { name: kld.name.to_owned(), - cloud: HashMap::new(), + liveness: HashMap::new(), apps: grp_check_hash, - infos: Vec::new(), + infos: get_info_status(reqenv, &kld).await, }); - }); - for kld in res_cloud { + }; + for kld in res_liveness { let mut grp_check_hash: HashMap = HashMap::new(); kld.groups.iter().for_each(|grp| { grp_check_hash.insert(format!("{}",grp.name),collect_clouds_check_items(&role,&grp.items)); }); - let kld_statusinfo_files: Vec = get_statusinfo_fileslist(&reqenv.config().state_path,&kld.name).await.unwrap_or_else(|e|{ - println!("Error on infos {}: {}",&kld.name,e); - Vec::new() - }); // dbg!(&kld_statusinfo_files); - let infos = load_statusinfo(&format!("{}/{}",&reqenv.config().state_path,&kld.name), kld_statusinfo_files).await; - //dbg!(&infos); if kld_check.len() == 0 { kld_check.push(KldCheck { name: kld.name.to_owned(), apps: HashMap::new(), - cloud: grp_check_hash, - infos, + liveness: grp_check_hash, + infos: get_info_status(reqenv, &kld).await, }); } else { let mut kldix = 0; @@ -150,16 +161,16 @@ pub async fn get_tsks_apps_check(reqenv: &ReqEnv,_auth: UserCtx, role: &str) -> kldix = idx; } }); - kld_check[kldix].cloud = grp_check_hash.to_owned(); - kld_check[kldix].infos = infos.to_owned(); + kld_check[kldix].liveness = grp_check_hash.to_owned(); } }; - // let result = format!("{}:{}{}:{}{}",r#"{"group_apps""#,&res_apps_json,r#","group_cloud""#,&res_cloud_json,r#"}"#); - let result=serde_json::to_string(&kld_check).unwrap_or_else(|e|{ + let check_json=serde_json::to_string(&kld_check).unwrap_or_else(|e|{ println!("Error serde from value: {}",e); String::from("") }); - result.to_owned() + let defs_json = get_defs_from_req(&reqenv, prfx.to_owned(), &reqenv.get_user().await).await; + format!("{}:{}{}:{}{}",r#"{"check""#,&check_json,r#","defs""#,&defs_json,r#"}"#).to_owned() + // serde_json::to_string(self).unwrap_or_else(|_| String::from("")).replace("\n","") } pub async fn home( header: HeaderMap, @@ -188,7 +199,7 @@ pub async fn home( // dbg!("LANG: {} - {}",language, lang); let role = reqenv.req.user_role().await; // reqenv.is_admin() - let result = get_tsks_apps_check(&reqenv,auth,&role).await; + let result = get_tsks_apps_check(&reqenv,auth,&role,prfx.to_owned()).await; // let mut path = format!("{}/profiles/{}/{}/defs.yaml",reqenv.websrvr().resources_path,&prfx,&auth.user_id); // if ! std::path::Path::new(&path).exists() { // path = format!("{}/profiles/{}/defs.yaml",reqenv.websrvr().resources_path,&prfx); @@ -211,7 +222,7 @@ pub async fn home( Err(_e) => { // let result = format!("Error: no credentials found"); // println!("{}",e); - let result = get_tsks_apps_check(&reqenv,UserCtx::default(),"").await; + let result = get_tsks_apps_check(&reqenv,UserCtx::default(),"",prfx.to_owned()).await; Ok(warp::http::Response::builder() .body(result.to_string()) .into_response()) diff --git a/src/handlers/new_h_home.rs b/src/handlers/new_h_home.rs new file mode 100644 index 0000000..daf52b8 --- /dev/null +++ b/src/handlers/new_h_home.rs @@ -0,0 +1,229 @@ +use std::collections::{HashMap,BTreeMap}; +use warp::{ + // http::{StatusCode}, + http::{method::Method, HeaderMap, HeaderValue}, + Reply, Rejection, +}; +use reqenv::ReqEnv; +// use app_env::profile::Profile; +use app_auth::{UserCtx}; + +// use kloud::{ +// defs::{ +// KloudQueryConfigFilters, +// }, +// }; +use clds::defs::{KloudCheckHome,CloudCheckItem}; +use clds::status::{get_statusinfo_fileslist,load_statusinfo}; +use clds::clouds::defs::{Cloud,SrvcsHostInfOut,AppOut,AppsrvcInfo,AppsrvcInfOut, TskSrvcOut, TsksrvcInfo, TsksrvcInfOut}; +use crate::defs::{KldCheck, MapCheckInfo, DataDBs}; // ,CollsData}; +use clds::clouds::on_clouds::{get_cloud_check,get_apps_check}; +use app_env::appenv::{Rol,Policy}; + +pub fn tsks_info_on_role(role: String, tsksrvcs: Vec) -> Vec { + tsksrvcs.into_iter().map(|tsk| { + let srvc = match role.as_str() { + "admin" => + TskSrvcOut { + name: tsk.srvc.name.to_owned(), + path: tsk.srvc.path.to_owned(), + req: tsk.srvc.req.to_owned(), + target: tsk.srvc.target.to_owned(), + liveness: tsk.srvc.liveness.to_owned(), + critical: tsk.srvc.critical.to_owned(), + }, + _ => + TskSrvcOut { + name: tsk.srvc.name.to_owned(), + path: String::from(""), + req: String::from(""), + target: tsk.srvc.target.to_owned(), + liveness: String::from(""), + critical: tsk.srvc.critical.to_owned(), + }, + }; + TsksrvcInfOut { + name: tsk.name.to_owned(), + info: tsk.info.to_owned(), + srvc + } + }).collect() +} +pub fn app_info_on_role(role: String, appsrvcs: Vec) -> Vec { + appsrvcs.into_iter().map(|app| { + let srvc = match role.as_str() { + "admin" => + AppOut { + name: app.srvc.name.to_owned(), + path: app.srvc.path.to_owned(), + req: app.srvc.req.to_owned(), + target: app.srvc.target.to_owned(), + liveness: app.srvc.liveness.to_owned(), + critical: app.srvc.critical.to_owned(), + }, + _ => + AppOut { + name: app.srvc.name.to_owned(), + path: String::from(""), + req: String::from(""), + target: app.srvc.target.to_owned(), + liveness: String::from(""), + critical: app.srvc.critical.to_owned(), + }, + }; + AppsrvcInfOut { + name: app.name.to_owned(), + info: app.info.to_owned(), + srvc + } + }).collect() +} +pub fn collect_clouds_check_items(target: String,role: &str, items: &Vec ) -> BTreeMap> { + let mut m_items = BTreeMap::new(); + items.iter().for_each(|itm| { + let liveness: Vec; + liveness = itm.liveness.to_owned().into_iter().map(|it| + match target.as_str() { + "apps" => + SrvcsHostInfOut { + hostname: format!("{}",&it.hostname), + tsksrvcs: Vec::new(), + appsrvcs: app_info_on_role(role.to_owned(), it.appsrvcs), + }, + _ => + SrvcsHostInfOut { + hostname: format!("{}",&it.hostname), + tsksrvcs: tsks_info_on_role(role.to_owned(), it.tsksrvcs), + appsrvcs: Vec::new(), + } + } + ).collect(); + m_items.insert(format!("{}",&itm.name),liveness); + }); + m_items +} +pub async fn get_tsks_apps_check(reqenv: &ReqEnv,_auth: UserCtx, role: &str) -> String { + let result_apps = get_apps_check(&reqenv).await; + // println!("Result apps: {}",&result_apps); + let res_apps: Vec = serde_json::from_str(&result_apps).unwrap_or_else(|e| { + println!("Error serde apps json: {}",e); + Vec::new() + }); + let result_cloud = get_cloud_check(&reqenv).await; + // println!("Result cloud: {}",&result_cloud); + let res_cloud: Vec = serde_json::from_str(&result_cloud).unwrap_or_else(|e| { + println!("Error serde apps json: {}",e); + Vec::new() + }); + let mut kld_check: Vec = Vec::new(); + res_apps.iter().for_each(|kld| { + // let mut list_groups = String::from(""); + let mut grp_check_hash: HashMap = HashMap::new(); + kld.groups.iter().for_each(|grp| { + // if !list_groups.is_empty() { + // list_groups.push(','); + // } + // list_groups.push_str(&grp.name); + grp_check_hash.insert(format!("{}",grp.name),collect_clouds_check_items(String::from("apps"),&role,&grp.items)); + }); + kld_check.push(KldCheck { + name: kld.name.to_owned(), + cloud: HashMap::new(), + apps: grp_check_hash, + infos: Vec::new(), + }); + }); + for kld in res_cloud { + let mut grp_check_hash: HashMap = HashMap::new(); + kld.groups.iter().for_each(|grp| { + grp_check_hash.insert(format!("{}",grp.name),collect_clouds_check_items(String::from("tsks"),&role,&grp.items)); + }); + let kld_statusinfo_files: Vec = get_statusinfo_fileslist(&reqenv.config().state_path,&kld.name).await.unwrap_or_else(|e|{ + println!("Error on infos {}: {}",&kld.name,e); + Vec::new() + }); + // dbg!(&kld_statusinfo_files); + let infos = load_statusinfo(&format!("{}/{}",&reqenv.config().state_path,&kld.name), kld_statusinfo_files).await; + //dbg!(&infos); + if kld_check.len() == 0 { + kld_check.push(KldCheck { + name: kld.name.to_owned(), + apps: HashMap::new(), + cloud: grp_check_hash, + infos, + }); + } else { + let mut kldix = 0; + kld_check.iter().enumerate().for_each(|(idx,k)| { + if k.name == kld.name { + kldix = idx; + } + }); + kld_check[kldix].cloud = grp_check_hash.to_owned(); + kld_check[kldix].infos = infos.to_owned(); + } + }; + // let result = format!("{}:{}{}:{}{}",r#"{"group_apps""#,&res_apps_json,r#","group_cloud""#,&res_cloud_json,r#"}"#); + let result=serde_json::to_string(&kld_check).unwrap_or_else(|e|{ + println!("Error serde from value: {}",e); + String::from("") + }); + result.to_owned() +} +pub async fn home( + header: HeaderMap, + method: Method, +// user: UserCtx, + db: DataDBs, + _cloud: Cloud, + prfx: String, +// headers: warp::http::HeaderMap +// path: warp::path::FullPath, headers: warp::http::HeaderMap +) -> Result { + let reqenv = ReqEnv::new(db.app, db.auth, header, method, "/home", "home", &prfx); + // let allow_origin = reqenv.websrvr().allow_origin; + // let opts = KloudQueryConfigFilters::default(); + // let source = format!("{}/{}","ma",""); + match reqenv.user_authentication().await { + Ok(auth) => { + // dbg!("auth: {}",&auth); + // println!("User: {} | {}",&user.user_id,&user.token); + // if let Some(lang) = reqtasks.params().get("lang") { + // res.render_json_text(&get_lang_items_str("langs",req_lang,"yaml")) + // } else { + // res.render_json_text(&get_lang_items_str("langs",&reqtasks.lang(),"yaml")) + // } + // log::debug!("LANG: {} - {}",language, lang); + // dbg!("LANG: {} - {}",language, lang); + let role = reqenv.req.user_role().await; + // reqenv.is_admin() + let result = get_tsks_apps_check(&reqenv,auth,&role).await; + // let mut path = format!("{}/profiles/{}/{}/defs.yaml",reqenv.websrvr().resources_path,&prfx,&auth.user_id); + // if ! std::path::Path::new(&path).exists() { + // path = format!("{}/profiles/{}/defs.yaml",reqenv.websrvr().resources_path,&prfx); + // } + // let content = Profile::load_fs_content(path.into()); + + // let lang = opts.lang.unwrap_or_else(|| String::from("es")); + // let section = opts.section.unwrap_or_else(|| String::from("")); + // let lang_items = LangItems::new("langs/ta",&lang,"yaml"); + // let result = lang_items.get_items_str(§ion); + + // let res = Profile::to_yaml(content); // String::from(""); + // let result = serde_json::to_string(&res).unwrap_or_else(|_| String::from("")); + + + Ok(warp::http::Response::builder() + .body(result.to_string()) + .into_response()) + }, + Err(_e) => { + // let result = format!("Error: no credentials found"); + // println!("{}",e); + let result = get_tsks_apps_check(&reqenv,UserCtx::default(),"").await; + Ok(warp::http::Response::builder() + .body(result.to_string()) + .into_response()) + } + } +} diff --git a/src/main.rs b/src/main.rs index a26be26..9a9d6a4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,7 +24,13 @@ use crate::defs::{DataDBs,CollsData,load_cloud_env}; use clds::clouds::defs::{ Cloud, }; -use clds::clouds::on_clouds::{env_cloud,make_cloud_cache,run_clouds_check,run_apps_check}; +use clds::clouds::utils::{ + env_cloud, +}; +use clds::clouds::on_req::{ + run_cache_data, + make_cloud_cache, +}; use reqenv::ReqEnv; // static WEBSERVER: AtomicUsize = AtomicUsize::new(0); @@ -222,95 +228,49 @@ async fn set_reqenv(app_env: &AppEnv,verbose: isize) -> ReqEnv { "/config", "config", "kloud" ) } -pub async fn run_cache_clouds() -> Result<()> { +pub async fn run_on_clouds(target_task: &str) -> Result<()> { let (arg_cfg_path,arg_env_path) = get_args(); let now = chrono::Utc::now().timestamp(); let verbose = envmnt::get_isize("DEBUG", 0); if verbose > 0 { - println!("Cache service on Clouds: run {} __________ {} {} ",&now,&arg_cfg_path,&arg_env_path); + println!("{} service on Clouds: run {} __________ {} {} ",&target_task,&now,&arg_cfg_path,&arg_env_path); } let (cloud, app_env) = match get_app_env(arg_cfg_path,verbose).await { Ok((c,e)) => (c,e), Err(e) => { - println!("Cache service on Clouds: done {} __________ ",&now); + println!("{} service on Clouds: errors {} __________ ",&target_task,&now); return Err(e); }, }; let reqenv = set_reqenv(&app_env,verbose).await; - let res = make_cloud_cache(&reqenv,&cloud).await; - if verbose > 0 { - println!("Cache service on Clouds: done {} __________ ",&now); - } - res -} -pub async fn run_check_clouds() -> Result<()> { - let (arg_cfg_path,arg_env_path) = get_args(); - let now = chrono::Utc::now().timestamp(); - let verbose = envmnt::get_isize("DEBUG", 0); - if verbose > 0 { - println!("Check Cloud services: run {} __________ {} {} ",&now,&arg_cfg_path,&arg_env_path); - } - let (cloud, app_env) = match get_app_env(arg_cfg_path,verbose).await { - Ok((c,e)) => (c,e), - Err(e) => { - println!("Check Cloud services: done {} __________ ",&now); - return Err(e); - }, + let res = match target_task { + "monitor" => { + let monitor_rules = MonitorRules::load( + &app_env.config.monitor_rules_path, + &app_env.config.monitor_rules_file, + &app_env.config.monitor_rules_format + ); + if monitor_rules.rules.len() == 0 { + eprintln!("No monitor rules found"); + return Ok(()); + } + monitor_rules.run(cloud,app_env).await + }, + "apps" => + run_cache_data(&target_task,&reqenv,&cloud,&target_task,"*").await, + "liveness" => + run_cache_data(&target_task,&reqenv,&cloud,"monitor,liveness","*").await, + "status" => + run_cache_data(&target_task,&reqenv,&cloud,"monitor,status","*").await, + "cache" => + make_cloud_cache(&reqenv,&cloud).await, + _ => { + eprintln!("Error scheduling task {}",&target_task); + Ok(()) + } }; - let reqenv = set_reqenv(&app_env,verbose).await; - let res = run_clouds_check(&reqenv,&cloud).await; if verbose > 0 { - println!("Check Cloud services: done {} __________ ",&now); - } - res -} -pub async fn run_check_apps() -> Result<()> { - let (arg_cfg_path,arg_env_path) = get_args(); - let now = chrono::Utc::now().timestamp(); - let verbose = envmnt::get_isize("DEBUG", 0); - if verbose > 0 { - println!("Check Apps services: run {} __________ {} {} ",&now,&arg_cfg_path,&arg_env_path); - } - let (cloud, app_env) = match get_app_env(arg_cfg_path,verbose).await { - Ok((c,e)) => (c,e), - Err(e) => { - println!("Check Apps services: done {} __________ ",&now); - return Err(e); - }, - }; - let reqenv = set_reqenv(&app_env,verbose).await; - let res = run_apps_check(&reqenv,&cloud).await; - if verbose > 0 { - println!("Check Apps services: done {} __________ ",&now); - } - res -} -pub async fn run_clouds_monitor() -> Result<()> { - let (arg_cfg_path,arg_env_path) = get_args(); - let now = chrono::Utc::now().timestamp(); - let verbose = envmnt::get_isize("DEBUG", 0); - if verbose > 0 { - println!("Monitor Cloud: run {} __________ {} {} ",&now,arg_cfg_path,&arg_env_path); - } - let (cloud, app_env) = match get_app_env(arg_cfg_path,verbose).await { - Ok((c,e)) => (c,e), - Err(e) => { - println!("Monitor Cloud done {} __________ ",&now); - return Err(e); - }, - }; - let monitor_rules = MonitorRules::load( - &app_env.config.monitor_rules_path, - &app_env.config.monitor_rules_file, - &app_env.config.monitor_rules_format - ); - if monitor_rules.rules.len() == 0 { - eprintln!("No monitor rules found"); - return Ok(()); - } - let res = monitor_rules.run(cloud,app_env).await; - if verbose > 0 { - println!("Monitor Cloud done {} __________ ",&now); + println!("{} service on Clouds: done {} __________ ",&target_task,&now); } res } @@ -352,25 +312,18 @@ pub async fn main() -> BxDynResult<()> { //std::io::Result<()> { } let config = Config::new(config_content,debug); let app_data_conn = AppDataConn::new("Zterton".to_string(),config.datastores_settings.to_owned(),"").await; - if !app_data_conn.check_connections(config.datastores_settings.to_owned()).await { - println!("Error checking app data store connections"); - } + if config.datastores_settings.len() > 0 { + if !app_data_conn.check_connections(config.datastores_settings.to_owned()).await { + println!("Error checking app data store connections"); + } + } if config.run_schedtasks { - for it in &config.schedtasks { - if ! it.on_start { - continue; + config.schedtasks.clone().iter().for_each(|it| { + if it.on_start { + let name:&'static str = Box::leak(format!("{}",&it.name).into_boxed_str()); + tokio::spawn(async move {run_on_clouds(name).await}); } - match it.name.as_str() { - "monitor" => tokio::spawn(async {run_clouds_monitor().await}), - "check" => tokio::spawn(async {run_check_clouds().await}), - "cache" => tokio::spawn(async {run_cache_clouds().await}), - "apps" => tokio::spawn(async {run_check_apps().await}), - _ => { - eprintln!("Error task {} not defined",&it.name); - continue; - }, - }; - } + }); } if config.run_websrvrs { for (pos,it) in config.websrvrs.iter().enumerate() { @@ -382,45 +335,18 @@ pub async fn main() -> BxDynResult<()> { //std::io::Result<()> { } } if config.run_schedtasks { - for it in config.schedtasks { + for it in config.schedtasks.clone() { if it.schedule.is_empty() { eprintln!("Task {} no schedule defined",&it.name); continue; } - let res = match it.name.as_str() { - "monitor" => - sched.add(Job::new(&it.schedule.to_owned(), move |uuid, _l| { - if debug > 0 { - println!("Schedule {} {}: {}",&it.name,&it.schedule,uuid); - } - tokio::spawn(async {run_clouds_monitor().await}); - })?), - "check" => - sched.add(Job::new(&it.schedule.to_owned(), move |uuid, _l| { - if debug > 0 { - println!("Schedule {} {}: {}",&it.name,&it.schedule,uuid); - } - tokio::spawn(async {run_check_clouds().await}); - })?), - "apps" => - sched.add(Job::new(&it.schedule.to_owned(), move |uuid, _l| { - if debug > 0 { - println!("Schedule {} {}: {}",&it.name,&it.schedule,uuid); - } - tokio::spawn(async {run_check_apps().await}); - })?), - "cache" => - sched.add(Job::new(&it.schedule.to_owned(), move |uuid, _l| { - if debug > 0 { - println!("Schedule {} {}: {}",&it.name,&it.schedule,uuid); - } - tokio::spawn(async {run_cache_clouds().await}); - })?), - _ => { - eprintln!("Error task {} not defined",&it.name); - continue; - }, - }; + let res = sched.add(Job::new(&it.schedule.to_owned(), move |uuid, _l| { + if debug > 0 { + println!("Schedule {} {}: {}",&it.name,&it.schedule,uuid); + } + let name:&'static str = Box::leak(format!("{}",&it.name).into_boxed_str()); + tokio::spawn(async move {run_on_clouds(name).await}); + })?); match res { Ok(_) => { continue; }, Err(e) => {