From 0009efc322ee4a1badfdb367e50ed3a290d3d914 Mon Sep 17 00:00:00 2001 From: Paul Lecuq Date: Wed, 2 Feb 2022 00:35:56 +0100 Subject: [PATCH] another working and better example for hotreload with async --- src/config/cli.yml | 2 +- src/config/mod.rs | 22 ++++++++++------------ src/padcontrol/mod.rs | 6 +++--- 3 files changed, 14 insertions(+), 16 deletions(-) diff --git a/src/config/cli.yml b/src/config/cli.yml index 4096f08..6d6bf8b 100644 --- a/src/config/cli.yml +++ b/src/config/cli.yml @@ -14,4 +14,4 @@ args: short: m value_name: MODE help: Handles mode - takes_value: true \ No newline at end of file + takes_value: true diff --git a/src/config/mod.rs b/src/config/mod.rs index 9484477..27f0d22 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -8,7 +8,6 @@ use serde_json::Value as JsonValue; use std::fs::File; use std::io::Read; use std::string::String; -use std::sync::{Arc, Mutex}; use std::time::Duration; use tokio::time::sleep; @@ -44,7 +43,7 @@ impl Context { }; println!("Loading {configfile} file ...", configfile = ctx.configfile); - ctx.cfg.load(&ctx.configfile).await; + ctx.cfg.load(&ctx.configfile.to_string()).await; println!( "Adding inotify watch on {configfile} file ...", @@ -72,19 +71,19 @@ impl Context { }*/ } -pub async fn hotreload(ctx: Context, ctxsender: &tokio::sync::mpsc::Sender) { +pub async fn hotreload(ctx: &mut Context, ctxsender: &tokio::sync::mpsc::Sender) { println!("function hotreload, {:?}", ctxsender); - let mut sendctx = ctx.clone(); loop { - let events = match ctx.inotify.read_events() { + let events = match ctx.to_owned().inotify.read_events() { Ok(ev) => ev, Err(_) => vec![], }; if events.len() > 0 { - sendctx.cfg.load(&sendctx.configfile).await; + ctx.cfg.load(&ctx.configfile).await; + println!("{cfg:?}", cfg = ctx.cfg); } 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) { Ok(f) => f, Err(err) => { @@ -121,8 +120,7 @@ impl<'a> Config { } }; let mut contents = String::new(); - let fread = file.read_to_string(&mut contents).unwrap(); - println!("{} {} {}", configfile, contents, fread); + file.read_to_string(&mut contents).unwrap(); let parse: Result = serde_json::from_str(contents.as_str()); *self = match parse { Ok(cfg) => { @@ -163,7 +161,7 @@ impl<'a> Config { self.save(&configfile).await; } - async fn save(&self, configfile: &str) { + async fn save(&self, configfile: &String) { let file: File; let filemeta = std::fs::metadata(configfile); let fileexists = match filemeta { @@ -173,7 +171,7 @@ impl<'a> Config { if !fileexists { file = File::create(configfile).unwrap(); serde_json::to_writer_pretty(file, &self).unwrap(); - sleep(Duration::from_secs(1)).await; + sleep(Duration::from_millis(1)).await; } } } diff --git a/src/padcontrol/mod.rs b/src/padcontrol/mod.rs index f136345..0a6067b 100644 --- a/src/padcontrol/mod.rs +++ b/src/padcontrol/mod.rs @@ -20,17 +20,17 @@ fn log(num: i64) { pub async fn run<'a>() { log(1); - let ctx = Context::new().await; + let mut ctx = Box::new(Context::new().await); log(2); let (tx1, mut rx1) = mpsc::channel(1); log(3); 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); log(4); } -async fn test<'a>(ctxreceiver: &'a mut tokio::sync::mpsc::Receiver) { +async fn test<'a>(ctxreceiver: &mut tokio::sync::mpsc::Receiver) { while let Some(val) = ctxreceiver.recv().await { println!("Fetching zabbix problems, {:?}", val.cfg); let zabbix_data = get_zabbix_problems(&val.cfg).await.unwrap();