Skip to content

Commit

Permalink
feat: allow disabling the cooldown
Browse files Browse the repository at this point in the history
  • Loading branch information
ctron committed Sep 29, 2023
1 parent 11f879e commit dbdbe8c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
6 changes: 5 additions & 1 deletion src/config/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,11 @@ pub struct ConfigOptsWatch {
#[arg(short, long, value_name = "path")]
pub ignore: Option<Vec<PathBuf>>,
/// Using polling mode for detecting changes
#[arg(short, long)]
#[arg(long)]
pub poll: bool,
/// Allow disabling the cooldown
#[arg(long)]
pub ignore_cooldown: bool,
}

/// Config options for the serve system.
Expand Down Expand Up @@ -331,6 +334,7 @@ impl ConfigOpts {
watch: cli.watch,
ignore: cli.ignore,
poll: cli.poll,
ignore_cooldown: cli.ignore_cooldown,
};
let cfg = ConfigOpts {
build: None,
Expand Down
5 changes: 4 additions & 1 deletion src/config/rt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,10 @@ pub struct RtcWatch {
pub paths: Vec<PathBuf>,
/// Paths to ignore.
pub ignored_paths: Vec<PathBuf>,
/// Use polling mode for detecting chnages
/// Use polling mode for detecting changes
pub poll: bool,
/// Allow disabling the cooldown
pub ignore_cooldown: bool,
}

impl RtcWatch {
Expand Down Expand Up @@ -237,6 +239,7 @@ impl RtcWatch {
paths,
ignored_paths,
poll: opts.poll,
ignore_cooldown: opts.ignore_cooldown,
})
}
}
Expand Down
27 changes: 16 additions & 11 deletions src/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ pub struct WatchSystem {
/// build cooldown period ensures that no FS events are processed until at least a duration
/// of `WATCHER_COOLDOWN` has elapsed since the last build.
last_build_finished: Instant,
/// The cooldown for the watcher. [`None`] disables the cooldown.
watcher_cooldown: Option<Duration>,
}

impl WatchSystem {
Expand Down Expand Up @@ -88,6 +90,7 @@ impl WatchSystem {
shutdown: BroadcastStream::new(shutdown.subscribe()),
build_done_tx,
last_build_finished: Instant::now(),
watcher_cooldown: (!cfg.ignore_cooldown).then(|| WATCHER_COOLDOWN),
})
}

Expand All @@ -113,17 +116,19 @@ impl WatchSystem {

#[tracing::instrument(level = "trace", skip(self, event))]
async fn handle_watch_event(&mut self, event: DebouncedEvent) {
// There are various OS syscalls which can trigger FS changes, even though semantically no
// changes were made. A notorious example which has plagued the trunk watcher
// implementation is `std::fs::copy`, which will trigger watcher changes indicating
// that file contents have been modified.
//
// Given the difficult nature of this issue, we opt for using a cooldown period. Any changes
// events processed within the cooldown period following a build will be ignored.
if Instant::now().duration_since(self.last_build_finished) <= WATCHER_COOLDOWN {
// Purge any other events in the queue.
while let Ok(_event) = self.watch_rx.try_recv() {}
return;
if let Some(cooldown) = self.watcher_cooldown {
// There are various OS syscalls which can trigger FS changes, even though semantically no
// changes were made. A notorious example which has plagued the trunk watcher
// implementation is `std::fs::copy`, which will trigger watcher changes indicating
// that file contents have been modified.
//
// Given the difficult nature of this issue, we opt for using a cooldown period. Any changes
// events processed within the cooldown period following a build will be ignored.
if Instant::now().duration_since(self.last_build_finished) <= cooldown {
// Purge any other events in the queue.
while let Ok(_event) = self.watch_rx.try_recv() {}
return;
}
}

// Check each path in the event for a match.
Expand Down

0 comments on commit dbdbe8c

Please sign in to comment.