watchexec/lib/examples/fs.rs

49 lines
1.1 KiB
Rust
Raw Normal View History

2021-08-19 10:31:29 +02:00
use std::time::Duration;
2021-08-16 15:15:17 +02:00
2021-08-16 15:37:01 +02:00
use tokio::{
sync::{mpsc, watch},
time::sleep,
};
2021-08-19 10:31:29 +02:00
use watchexec::{event::Event, fs};
2021-08-16 15:15:17 +02:00
2021-08-16 15:37:01 +02:00
// Run with: `env RUST_LOG=debug cargo run --example fs`,
// then touch some files within the first 15 seconds, and afterwards.
2021-08-16 15:15:17 +02:00
#[tokio::main]
2021-08-16 15:37:01 +02:00
async fn main() -> color_eyre::eyre::Result<()> {
2021-08-16 15:15:17 +02:00
tracing_subscriber::fmt::init();
2021-08-16 15:37:01 +02:00
color_eyre::install()?;
2021-08-16 15:15:17 +02:00
2021-08-17 11:41:13 +02:00
let (ev_s, mut ev_r) = mpsc::channel::<Event>(1024);
2021-08-16 15:15:17 +02:00
let (er_s, mut er_r) = mpsc::channel(64);
let (wd_s, wd_r) = watch::channel(fs::WorkingData::default());
let mut wkd = fs::WorkingData::default();
wkd.pathset = vec![".".into()];
2021-08-16 15:37:01 +02:00
wd_s.send(wkd.clone())?;
2021-08-16 15:15:17 +02:00
tokio::spawn(async move {
while let Some(e) = ev_r.recv().await {
2021-08-16 15:37:01 +02:00
tracing::info!("event: {:?}", e);
2021-08-16 15:15:17 +02:00
}
});
tokio::spawn(async move {
while let Some(e) = er_r.recv().await {
2021-08-16 15:37:01 +02:00
tracing::error!("error: {}", e);
2021-08-16 15:15:17 +02:00
}
});
2021-08-16 15:37:01 +02:00
let wd_sh = tokio::spawn(async move {
sleep(Duration::from_secs(15)).await;
wkd.pathset = Vec::new();
tracing::info!("turning off fs watcher without stopping it");
wd_s.send(wkd).unwrap();
wd_s
});
2021-08-16 15:15:17 +02:00
fs::worker(wd_r, er_s, ev_s).await?;
2021-08-16 15:37:01 +02:00
wd_sh.await?;
2021-08-16 15:15:17 +02:00
Ok(())
}