daemon: Fix poll after finished

This commit is contained in:
2021-10-02 17:44:08 +02:00
parent 8cf21681fa
commit b4e1774993

View File

@ -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<JoinHandle<_>> = 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,15 +50,17 @@ 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<bool>;
if let Some(mut wfe) = wfe_handle.take() {
match poll!(&mut wfe) {
Poll::Ready(Ok(Ok(_wfe))) => {
restart_wfe = true;
@ -74,8 +78,15 @@ pub fn run(opt: DaemonOpt) -> Result<(), Error> {
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();
debug!(
@ -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,