use notify::{RecommendedWatcher, RecursiveMode, Watcher};
use std::path::Path;
use std::time::Duration;
use tokio::sync::mpsc;
pub struct RunsWatcher {
_watcher: RecommendedWatcher,
rx: mpsc::Receiver<()>,
}
impl RunsWatcher {
pub fn new(runs_dir: &Path) -> notify::Result<Self> {
let (tx, rx) = mpsc::channel(1);
let mut watcher =
notify::recommended_watcher(move |result: notify::Result<notify::Event>| {
if result.is_ok() {
let _ = tx.try_send(());
}
})?;
watcher.watch(runs_dir, RecursiveMode::Recursive)?;
Ok(Self {
_watcher: watcher,
rx,
})
}
pub async fn changed(&mut self) {
if self.rx.recv().await.is_none() {
tokio::time::sleep(Duration::from_secs(2)).await;
return;
}
while let Ok(Some(())) =
tokio::time::timeout(Duration::from_millis(40), self.rx.recv()).await
{}
}
}