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()?; let rt = Runtime::new()?;
rt.block_on(async { rt.block_on(async {
let (tx, rx) = mpsc::channel();
info!("creating output file"); info!("creating output file");
fs::write(&opt.file, b"").await?; fs::write(&opt.file, b"").await?;
// setup INotify watcher
let (tx, rx) = mpsc::channel();
let mut watcher = INotifyWatcher::new_raw(tx).unwrap(); let mut watcher = INotifyWatcher::new_raw(tx).unwrap();
watcher watcher
.watch(&opt.file, RecursiveMode::NonRecursive) .watch(&opt.file, RecursiveMode::NonRecursive)
.expect("failed to watch file"); .expect("failed to watch file");
let mut inotify = proxy_channel(rx);
// perform first category update
soft_fail(update_category_list(opt, cookie).await); soft_fail(update_category_list(opt, cookie).await);
let mut last_update = Instant::now(); let mut last_update = Instant::now();
let mut wfe: JoinHandle<_> = spawn(wait_for_event(opt, cookie)); // start WFE long-poll
let mut wfe_handle: Option<JoinHandle<_>> = Some(spawn(wait_for_event(opt, cookie)));
let mut rx = proxy_channel(rx);
loop { loop {
let event = match rx.recv().await { // wait for next inotify event
let event = match inotify.recv().await {
Some(event) => event, Some(event) => event,
None => return Err(Error::ChannelClosed), None => return Err(Error::ChannelClosed),
}; };
@ -48,33 +50,42 @@ pub fn run(opt: DaemonOpt) -> Result<(), Error> {
debug!("event op {:?}", event.op); debug!("event op {:?}", event.op);
match event.op { match event.op {
Ok(Op::CHMOD) => { /* YEESSS */ } Ok(Op::CHMOD) => { /* we only care about this event */ }
_ => continue, _ => continue,
} }
debug!("CHMOD event detected"); debug!("CHMOD event detected");
let update_categories; // poll wfe and decide whether we want to restart it, and whether we want to update the
let restart_wfe; // 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;
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) { // wfe task is not complete, we will continue waiting for it next loop
Poll::Ready(Ok(Ok(_wfe))) => { wfe_handle = Some(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);
} }
} else {
restart_wfe = false;
update_categories = None;
} }
let elapsed = last_update.elapsed(); let elapsed = last_update.elapsed();
@ -94,13 +105,13 @@ pub fn run(opt: DaemonOpt) -> Result<(), Error> {
} }
if update_categories || restart_wfe { 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 { struct WaitForEvent {
#[allow(dead_code)] #[allow(dead_code)]
timeout: bool, timeout: bool,