From 7fa2def77442a44941f83a0c2e0a70622926bd74 Mon Sep 17 00:00:00 2001 From: Filipe Rodrigues Date: Sun, 12 Feb 2023 08:25:47 +0000 Subject: [PATCH] Added timeout argument for watcher. --- src/args.rs | 6 +++++- src/main.rs | 7 ++++++- src/watcher.rs | 25 ++++++++++++++----------- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/args.rs b/src/args.rs index 596e41a..af02edc 100644 --- a/src/args.rs +++ b/src/args.rs @@ -12,7 +12,7 @@ use std::path::PathBuf; /// /// Then you may request `zbuild` to create a certain file and it will parse /// all rules in order to find a way to build the output -#[derive(PartialEq, Eq, Clone, Debug)] +#[derive(Debug)] #[derive(clap::Parser)] #[clap(author, version, about)] pub struct Args { @@ -47,6 +47,10 @@ pub struct Args { #[clap(long = "watch", short = 'w')] pub watch: bool, + /// Watcher file event debouncer timeout + #[clap(long = "watcher-debouncer-timeout-ms")] + pub watch_debouncer_timeout_ms: Option, + /// Logs output to a file. /// /// You can use `RUST_LOG_FILE` to set filtering options diff --git a/src/main.rs b/src/main.rs index 995c44d..c8e5a80 100644 --- a/src/main.rs +++ b/src/main.rs @@ -174,7 +174,12 @@ async fn main() -> Result<(), anyhow::Error> { // Then create the watcher, if we're watching let watcher = args .watch - .then(|| Watcher::new(builder.subscribe_events())) + .then(|| { + Watcher::new( + builder.subscribe_events(), + args.watch_debouncer_timeout_ms.unwrap_or(100.0), + ) + }) .transpose()?; // Finally build all targets and start watching diff --git a/src/watcher.rs b/src/watcher.rs index ed52671..0ffdcda 100644 --- a/src/watcher.rs +++ b/src/watcher.rs @@ -46,11 +46,16 @@ pub struct Watcher<'s> { impl<'s> Watcher<'s> { /// Creates a new watcher - pub fn new(builder_event_rx: async_broadcast::Receiver>) -> Result { + pub fn new( + builder_event_rx: async_broadcast::Receiver>, + watch_debouncer_timeout_ms: f64, + ) -> Result { // Create the watcher let (fs_event_tx, fs_event_rx) = tokio::sync::mpsc::channel(16); - let watcher = - notify_debouncer_mini::new_debouncer(Duration::from_secs(1), None, move |fs_events| match fs_events { + let watcher = notify_debouncer_mini::new_debouncer( + Duration::from_secs_f64(watch_debouncer_timeout_ms / 1000.0), + None, + move |fs_events| match fs_events { Ok(fs_events) => for fs_event in fs_events { tracing::trace!(?fs_event, "Watcher fs event"); @@ -61,9 +66,10 @@ impl<'s> Watcher<'s> { for err in errs { tracing::warn!("Error while watching: {:?}", anyhow::Error::from(err)); }, - }) - .context("Unable to create file watcher") - .map_err(AppError::Other)?; + }, + ) + .context("Unable to create file watcher") + .map_err(AppError::Other)?; Ok(Self { watcher, @@ -144,7 +150,7 @@ impl<'s> Watcher<'s> { Some(rev_dep) => rev_dep.clone(), None => return, }; - tracing::info!("Changed: {:?}", rev_dep.target); + tracing::debug!("Changed: {:?}", rev_dep.target); tracing::trace!(?rev_dep, "Reverse dependencies"); // Note: We clone the parents so we don't hold onto the rev dep lock for too long @@ -182,10 +188,7 @@ impl<'s> Watcher<'s> { // get rebuilt. dep_parents .iter() - .map(|target| { - tracing::info!("Rechecking: {target:?}"); - crate::build_target(builder, target, rules, ignore_missing) - }) + .map(|target| crate::build_target(builder, target, rules, ignore_missing)) .collect::>() .collect::<()>() .await;