From b4e1774993c17197752b548b57a219e8d1cf48db Mon Sep 17 00:00:00 2001 From: Joakim Hulthe Date: Sat, 2 Oct 2021 17:44:08 +0200 Subject: [PATCH] daemon: Fix poll after finished --- cli/src/daemon.rs | 67 +++++++++++++++++++++++++++-------------------- 1 file changed, 39 insertions(+), 28 deletions(-) diff --git a/cli/src/daemon.rs b/cli/src/daemon.rs index aedf366..cc3b051 100644 --- a/cli/src/daemon.rs +++ b/cli/src/daemon.rs @@ -22,25 +22,27 @@ pub fn run(opt: DaemonOpt) -> Result<(), Error> { let rt = Runtime::new()?; rt.block_on(async { - let (tx, rx) = mpsc::channel(); - info!("creating output file"); fs::write(&opt.file, b"").await?; + // setup INotify watcher + let (tx, rx) = mpsc::channel(); let mut watcher = INotifyWatcher::new_raw(tx).unwrap(); watcher .watch(&opt.file, RecursiveMode::NonRecursive) .expect("failed to watch file"); + let mut inotify = proxy_channel(rx); + // perform first category update soft_fail(update_category_list(opt, cookie).await); let mut last_update = Instant::now(); - let mut wfe: JoinHandle<_> = spawn(wait_for_event(opt, cookie)); - - let mut rx = proxy_channel(rx); + // start WFE long-poll + let mut wfe_handle: Option> = Some(spawn(wait_for_event(opt, cookie))); loop { - let event = match rx.recv().await { + // wait for next inotify event + let event = match inotify.recv().await { Some(event) => event, None => return Err(Error::ChannelClosed), }; @@ -48,33 +50,42 @@ pub fn run(opt: DaemonOpt) -> Result<(), Error> { debug!("event op {:?}", event.op); match event.op { - Ok(Op::CHMOD) => { /* YEESSS */ } + Ok(Op::CHMOD) => { /* we only care about this event */ } _ => continue, } debug!("CHMOD event detected"); - let update_categories; - let restart_wfe; + // poll wfe and decide whether we want to restart it, and whether we want to update the + // categories + let restart_wfe: bool; + let update_categories: Option; + if let Some(mut wfe) = wfe_handle.take() { + match poll!(&mut wfe) { + Poll::Ready(Ok(Ok(_wfe))) => { + restart_wfe = true; + update_categories = Some(true); + } + Poll::Ready(Ok(Err(e))) => { + error!("error waiting for event (falling back to timing): {}", e); + restart_wfe = false; + update_categories = None; + } + Poll::Ready(Err(_)) => { + restart_wfe = false; + update_categories = None; + } + Poll::Pending => { + restart_wfe = false; + update_categories = Some(false); - match poll!(&mut wfe) { - Poll::Ready(Ok(Ok(_wfe))) => { - restart_wfe = true; - update_categories = Some(true); - } - Poll::Ready(Ok(Err(e))) => { - error!("error waiting for event (falling back to timing): {}", e); - restart_wfe = false; - update_categories = None; - } - Poll::Ready(Err(_)) => { - restart_wfe = false; - update_categories = None; - } - Poll::Pending => { - restart_wfe = false; - update_categories = Some(false); + // wfe task is not complete, we will continue waiting for it next loop + wfe_handle = Some(wfe) + } } + } else { + restart_wfe = false; + update_categories = None; } let elapsed = last_update.elapsed(); @@ -94,13 +105,13 @@ pub fn run(opt: DaemonOpt) -> Result<(), Error> { } if update_categories || restart_wfe { - wfe = spawn(wait_for_event(opt, cookie)); + wfe_handle = Some(spawn(wait_for_event(opt, cookie))); } } }) } -#[derive(Deserialize)] +#[derive(Clone, Debug, Deserialize)] struct WaitForEvent { #[allow(dead_code)] timeout: bool,