Clarify fs worker usage

This commit is contained in:
Félix Saparelli 2021-08-17 01:37:01 +12:00
parent 822148da03
commit 7053360187
No known key found for this signature in database
GPG Key ID: B948C4BAE44FC474
4 changed files with 26 additions and 7 deletions

1
Cargo.lock generated
View File

@ -2055,6 +2055,7 @@ name = "watchexec"
version = "1.17.1"
dependencies = [
"chrono",
"color-eyre",
"dunce",
"miette",
"notify 5.0.0-pre.11",

View File

@ -27,5 +27,6 @@ version = "1.10.0"
features = ["full"]
[dev-dependencies]
color-eyre = "0.5.11"
tracing-subscriber = "0.2.19"

View File

@ -1,11 +1,17 @@
use std::error::Error;
use std::time::Duration;
use tokio::sync::{mpsc, watch};
use tokio::{
sync::{mpsc, watch},
time::sleep,
};
use watchexec::fs;
// Run with: `env RUST_LOG=debug cargo run --example fs`,
// then touch some files within the first 15 seconds, and afterwards.
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
async fn main() -> color_eyre::eyre::Result<()> {
tracing_subscriber::fmt::init();
color_eyre::install()?;
let (ev_s, mut ev_r) = mpsc::channel(1024);
let (er_s, mut er_r) = mpsc::channel(64);
@ -13,21 +19,30 @@ async fn main() -> Result<(), Box<dyn Error>> {
let mut wkd = fs::WorkingData::default();
wkd.pathset = vec![".".into()];
wd_s.send(wkd)?;
wd_s.send(wkd.clone())?;
tokio::spawn(async move {
while let Some(e) = ev_r.recv().await {
println!("event: {:?}", e);
tracing::info!("event: {:?}", e);
}
});
tokio::spawn(async move {
while let Some(e) = er_r.recv().await {
println!("error: {}", e);
tracing::error!("error: {}", e);
}
});
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
});
fs::worker(wd_r, er_s, ev_s).await?;
wd_sh.await?;
Ok(())
}

View File

@ -57,7 +57,8 @@ pub struct WorkingData {
/// While you can run several, you should only have one.
///
/// This only does a bare minimum of setup; to actually start the work, you need to set a non-empty pathset on the
/// [`WorkingData`] with the [`watch`] channel, and send a notification.
/// [`WorkingData`] with the [`watch`] channel, and send a notification. Take care _not_ to drop the watch sender:
/// this will cause the worker to stop gracefully, which may not be what was expected.
///
/// # Examples
///
@ -183,6 +184,7 @@ pub async fn worker(
}
}
debug!("ending file watcher");
Ok(())
}
fn process_event(