working example for hotreload with async
This commit is contained in:
parent
473883f6dc
commit
b5484c2bcd
@ -8,6 +8,9 @@ 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;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum ReloadFrequency {
|
||||
@ -69,16 +72,19 @@ impl Context {
|
||||
}*/
|
||||
}
|
||||
|
||||
pub async fn hotreload(rwctx: &tokio::sync::RwLock<Context>) {
|
||||
let mut ctx = rwctx.write().await;
|
||||
let configfile = ctx.configfile.clone();
|
||||
let events = match ctx.inotify.read_events() {
|
||||
Ok(ev) => ev,
|
||||
Err(_) => vec![],
|
||||
};
|
||||
if events.len() > 0 {
|
||||
println!("Reloading {cfg}", cfg = ctx.configfile);
|
||||
ctx.cfg.load(configfile.as_str()).await;
|
||||
pub async fn hotreload(ctx: Context, ctxsender: &tokio::sync::mpsc::Sender<Context>) {
|
||||
println!("function hotreload, {:?}", ctxsender);
|
||||
let mut sendctx = ctx.clone();
|
||||
loop {
|
||||
let events = match ctx.inotify.read_events() {
|
||||
Ok(ev) => ev,
|
||||
Err(_) => vec![],
|
||||
};
|
||||
if events.len() > 0 {
|
||||
sendctx.cfg.load(&sendctx.configfile).await;
|
||||
}
|
||||
println!("Sending {:?}", ctx);
|
||||
ctxsender.send(sendctx.clone()).await;
|
||||
}
|
||||
}
|
||||
|
||||
@ -115,7 +121,8 @@ impl<'a> Config {
|
||||
}
|
||||
};
|
||||
let mut contents = String::new();
|
||||
file.read_to_string(&mut contents).unwrap();
|
||||
let fread = file.read_to_string(&mut contents).unwrap();
|
||||
println!("{} {} {}", configfile, contents, fread);
|
||||
let parse: Result<JsonValue, JsonError> = serde_json::from_str(contents.as_str());
|
||||
*self = match parse {
|
||||
Ok(cfg) => {
|
||||
@ -163,11 +170,10 @@ impl<'a> Config {
|
||||
Ok(_) => true,
|
||||
Err(_) => false,
|
||||
};
|
||||
if fileexists {
|
||||
if !fileexists {
|
||||
file = File::create(configfile).unwrap();
|
||||
} else {
|
||||
file = File::open(configfile).unwrap();
|
||||
serde_json::to_writer_pretty(file, &self).unwrap();
|
||||
sleep(Duration::from_secs(1)).await;
|
||||
}
|
||||
serde_json::to_writer_pretty(file, &self).unwrap();
|
||||
}
|
||||
}
|
||||
|
@ -2,12 +2,9 @@ use crate::config::{hotreload, Context};
|
||||
use crate::zabbix::api::*;
|
||||
use launchy::Color;
|
||||
use launchy::{self, Canvas, CanvasLayout, CanvasLayoutPoller, MsgPollingWrapper, Pad};
|
||||
//use std::process::exit;
|
||||
//use std::sync::mpsc::{channel, Receiver, Sender};
|
||||
use std::sync::Arc;
|
||||
use std::thread::sleep;
|
||||
use std::time::Duration;
|
||||
use tokio::sync::RwLock;
|
||||
use tokio::sync::mpsc;
|
||||
use tokio::time::sleep;
|
||||
|
||||
const MATRIX_WIDTH: i32 = 8;
|
||||
const MATRIX_SIZE: i32 = MATRIX_WIDTH * MATRIX_WIDTH;
|
||||
@ -15,34 +12,32 @@ const REQUEST_WAITFOR: i32 = 5;
|
||||
const ZERO_COLOR: f32 = 0.;
|
||||
const ONE_COLOR: f32 = 1.;
|
||||
|
||||
pub async fn run() {
|
||||
println!("test1 🦀");
|
||||
let ctx = Context::new().await;
|
||||
println!("test2 🦀");
|
||||
let e = Arc::new(RwLock::new(ctx));
|
||||
let mutedctx = e.clone();
|
||||
println!("test3 🦀{}", e);
|
||||
loop {
|
||||
println!("test4 🦀");
|
||||
tokio::spawn(async move { test(&mutedctx) });
|
||||
println!("test5 🦀");
|
||||
/*tokio::select! {
|
||||
_ = test(&mutedctx) => {
|
||||
println!("fn test 🦀 {:?}", mutedctx);
|
||||
}
|
||||
_ = hotreload(&mutedctx) => {
|
||||
println!("fn hotreload 🦀 {:?}", mutedctx);
|
||||
}
|
||||
};*/
|
||||
}
|
||||
const POLLER_ITER: u64 = 17;
|
||||
|
||||
fn log(num: i64) {
|
||||
return println!("test{} 🦀", num);
|
||||
}
|
||||
|
||||
async fn test(roctx: &'static tokio::sync::RwLock<Context>) {
|
||||
let ctx = roctx.read().await;
|
||||
println!("{:?}", ctx);
|
||||
let zabbix_data = get_zabbix_problems(&ctx.cfg).await.unwrap();
|
||||
println!("{} {:?}", zabbix_data, ctx.cfg);
|
||||
sleep(Duration::from_secs(ctx.cfg.refresh.unwrap()));
|
||||
pub async fn run<'a>() {
|
||||
log(1);
|
||||
let ctx = 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 });
|
||||
tokio::join!(t1, t2);
|
||||
log(4);
|
||||
}
|
||||
|
||||
async fn test<'a>(ctxreceiver: &'a mut tokio::sync::mpsc::Receiver<Context>) {
|
||||
while let Some(val) = ctxreceiver.recv().await {
|
||||
println!("Fetching zabbix problems, {:?}", val.cfg);
|
||||
let zabbix_data = get_zabbix_problems(&val.cfg).await.unwrap();
|
||||
println!("{}", zabbix_data["result"][0]);
|
||||
println!("sleeping during {}", val.cfg.refresh.unwrap());
|
||||
sleep(Duration::from_secs(val.cfg.refresh.unwrap())).await;
|
||||
}
|
||||
}
|
||||
|
||||
fn initpad() -> (CanvasLayout<'static>, CanvasLayoutPoller) {
|
||||
@ -63,7 +58,10 @@ fn initpad() -> (CanvasLayout<'static>, CanvasLayoutPoller) {
|
||||
|
||||
fn input(canvas: &mut CanvasLayout, poller: &mut CanvasLayoutPoller) {
|
||||
for color in (0u64..).map(|f| Color::red_green_color(f as f32 / 60.0 / 2.5)) {
|
||||
for msg in poller.iter_for_millis(17).filter(|msg| msg.is_press()) {
|
||||
for msg in poller
|
||||
.iter_for_millis(POLLER_ITER)
|
||||
.filter(|msg| msg.is_press())
|
||||
{
|
||||
canvas[msg.pad()] = color;
|
||||
println!("{msg:?}", msg = msg.pad())
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user