another working and better example for hotreload with async

This commit is contained in:
Paul 2022-02-02 00:35:56 +01:00
parent b5484c2bcd
commit 0009efc322
3 changed files with 14 additions and 16 deletions

View File

@ -8,7 +8,6 @@ use serde_json::Value as JsonValue;
use std::fs::File; use std::fs::File;
use std::io::Read; use std::io::Read;
use std::string::String; use std::string::String;
use std::sync::{Arc, Mutex};
use std::time::Duration; use std::time::Duration;
use tokio::time::sleep; use tokio::time::sleep;
@ -44,7 +43,7 @@ impl Context {
}; };
println!("Loading {configfile} file ...", configfile = ctx.configfile); println!("Loading {configfile} file ...", configfile = ctx.configfile);
ctx.cfg.load(&ctx.configfile).await; ctx.cfg.load(&ctx.configfile.to_string()).await;
println!( println!(
"Adding inotify watch on {configfile} file ...", "Adding inotify watch on {configfile} file ...",
@ -72,19 +71,19 @@ impl Context {
}*/ }*/
} }
pub async fn hotreload(ctx: Context, ctxsender: &tokio::sync::mpsc::Sender<Context>) { pub async fn hotreload(ctx: &mut Context, ctxsender: &tokio::sync::mpsc::Sender<Context>) {
println!("function hotreload, {:?}", ctxsender); println!("function hotreload, {:?}", ctxsender);
let mut sendctx = ctx.clone();
loop { loop {
let events = match ctx.inotify.read_events() { let events = match ctx.to_owned().inotify.read_events() {
Ok(ev) => ev, Ok(ev) => ev,
Err(_) => vec![], Err(_) => vec![],
}; };
if events.len() > 0 { if events.len() > 0 {
sendctx.cfg.load(&sendctx.configfile).await; ctx.cfg.load(&ctx.configfile).await;
println!("{cfg:?}", cfg = ctx.cfg);
} }
println!("Sending {:?}", ctx); println!("Sending {:?}", ctx);
ctxsender.send(sendctx.clone()).await; ctxsender.send(ctx.to_owned()).await;
} }
} }
@ -113,7 +112,7 @@ impl<'a> Config {
} }
} }
async fn load(&mut self, configfile: &str) { async fn load(&mut self, configfile: &String) {
let mut file = match File::open(configfile) { let mut file = match File::open(configfile) {
Ok(f) => f, Ok(f) => f,
Err(err) => { Err(err) => {
@ -121,8 +120,7 @@ impl<'a> Config {
} }
}; };
let mut contents = String::new(); let mut contents = String::new();
let fread = file.read_to_string(&mut contents).unwrap(); file.read_to_string(&mut contents).unwrap();
println!("{} {} {}", configfile, contents, fread);
let parse: Result<JsonValue, JsonError> = serde_json::from_str(contents.as_str()); let parse: Result<JsonValue, JsonError> = serde_json::from_str(contents.as_str());
*self = match parse { *self = match parse {
Ok(cfg) => { Ok(cfg) => {
@ -163,7 +161,7 @@ impl<'a> Config {
self.save(&configfile).await; self.save(&configfile).await;
} }
async fn save(&self, configfile: &str) { async fn save(&self, configfile: &String) {
let file: File; let file: File;
let filemeta = std::fs::metadata(configfile); let filemeta = std::fs::metadata(configfile);
let fileexists = match filemeta { let fileexists = match filemeta {
@ -173,7 +171,7 @@ impl<'a> Config {
if !fileexists { if !fileexists {
file = File::create(configfile).unwrap(); file = File::create(configfile).unwrap();
serde_json::to_writer_pretty(file, &self).unwrap(); serde_json::to_writer_pretty(file, &self).unwrap();
sleep(Duration::from_secs(1)).await; sleep(Duration::from_millis(1)).await;
} }
} }
} }

View File

@ -20,17 +20,17 @@ fn log(num: i64) {
pub async fn run<'a>() { pub async fn run<'a>() {
log(1); log(1);
let ctx = Context::new().await; let mut ctx = Box::new(Context::new().await);
log(2); log(2);
let (tx1, mut rx1) = mpsc::channel(1); let (tx1, mut rx1) = mpsc::channel(1);
log(3); log(3);
let t1 = tokio::task::spawn(async move { test(&mut rx1).await }); let t1 = tokio::task::spawn(async move { test(&mut rx1).await });
let t2 = tokio::task::spawn(async move { hotreload(ctx.clone(), &tx1).await }); let t2 = tokio::task::spawn(async move { hotreload(&mut ctx, &tx1).await });
tokio::join!(t1, t2); tokio::join!(t1, t2);
log(4); log(4);
} }
async fn test<'a>(ctxreceiver: &'a mut tokio::sync::mpsc::Receiver<Context>) { async fn test<'a>(ctxreceiver: &mut tokio::sync::mpsc::Receiver<Context>) {
while let Some(val) = ctxreceiver.recv().await { while let Some(val) = ctxreceiver.recv().await {
println!("Fetching zabbix problems, {:?}", val.cfg); println!("Fetching zabbix problems, {:?}", val.cfg);
let zabbix_data = get_zabbix_problems(&val.cfg).await.unwrap(); let zabbix_data = get_zabbix_problems(&val.cfg).await.unwrap();