Clarify fs worker usage
This commit is contained in:
parent
822148da03
commit
7053360187
|
@ -2055,6 +2055,7 @@ name = "watchexec"
|
|||
version = "1.17.1"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"color-eyre",
|
||||
"dunce",
|
||||
"miette",
|
||||
"notify 5.0.0-pre.11",
|
||||
|
|
|
@ -27,5 +27,6 @@ version = "1.10.0"
|
|||
features = ["full"]
|
||||
|
||||
[dev-dependencies]
|
||||
color-eyre = "0.5.11"
|
||||
tracing-subscriber = "0.2.19"
|
||||
|
||||
|
|
|
@ -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(())
|
||||
}
|
||||
|
|
|
@ -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(
|
||||
|
|
Loading…
Reference in New Issue