Skip to content

feat: use polling to watch files #365

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 8 additions & 29 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ gix = { version = "0.72", default-features = false, features = ["index", "exclud
glob = "0.3"
iq = { version = "0.4", features = ["template"] }
lazy-regex = "3.4.1"
notify = "7.0"
notify = "8.0"
rodio = { version = "0.20", optional = true, default-features = false, features = ["mp3"] }
rustc-hash = "2"
serde = { version = "1.0", features = ["derive"] }
Expand Down
4 changes: 4 additions & 0 deletions src/cli/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ pub struct Args {
#[clap(last = true)]
/// Arguments given to the job
pub additional_job_args: Vec<String>,

/// Change the recommended file watcher into a polling watcher by setting its polling interval (in seconds)
#[clap(long, value_name = "seconds")]
pub poll: Option<u64>,
}

impl Args {
Expand Down
6 changes: 4 additions & 2 deletions src/tui/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ pub fn run(
action_rx.clone(),
message.take(),
headless,
args,
)?;
match do_after {
DoAfterMission::NextJob(job_ref) => {
Expand Down Expand Up @@ -115,6 +116,7 @@ fn run_mission(
action_rx: Receiver<Action>,
message: Option<Message>,
headless: bool,
args: &Args
) -> Result<DoAfterMission> {
let keybindings = mission.settings.keybindings.clone();
let grace_period = mission.job.grace_period();
Expand All @@ -123,10 +125,10 @@ fn run_mission(

// build the watcher detecting and transmitting mission file changes
let ignorer = time!(Info, mission.ignorer());
let mission_watcher = Watcher::new(&mission.paths_to_watch, ignorer)?;
let mission_watcher = Watcher::new(&mission.paths_to_watch, ignorer, args.poll)?;

// create the watcher for config file changes
let config_watcher = Watcher::new(&mission.settings.config_files, IgnorerSet::default())?;
let config_watcher = Watcher::new(&mission.settings.config_files, IgnorerSet::default(), args.poll)?;

// create the executor, mission, and state
let mut executor = MissionExecutor::new(&mission)?;
Expand Down
47 changes: 28 additions & 19 deletions src/watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,40 @@ use {
crate::*,
anyhow::Result,
notify::{
RecommendedWatcher,
RecursiveMode,
Watcher as NotifyWatcher,
event::{
AccessKind,
AccessMode,
DataChange,
EventKind,
ModifyKind,
},
AccessKind, AccessMode, DataChange, EventKind, MetadataKind, ModifyKind
}, RecursiveMode, Watcher as NotifyWatcher
},
std::path::PathBuf,
std::{path::PathBuf, time::Duration},
termimad::crossbeam::channel::{
Receiver,
bounded,
bounded, Receiver
},
};

/// A file watcher, providing a channel to receive notifications
pub struct Watcher {
pub receiver: Receiver<()>,
_notify_watcher: RecommendedWatcher,
_notify_watcher: Box<dyn NotifyWatcher>,
}

impl Watcher {
pub fn new(
paths_to_watch: &[PathBuf],
mut ignorer: IgnorerSet,
polling: Option<u64>,
) -> Result<Self> {
info!("watcher on {:#?}", paths_to_watch);
let (sender, receiver) = bounded(0);
let mut notify_watcher =
notify::recommended_watcher(move |res: notify::Result<notify::Event>| match res {
let event_handler = move |res: notify::Result<notify::Event>| match res {
Ok(we) => {
match we.kind {
EventKind::Modify(ModifyKind::Metadata(_)) => {
debug!("ignoring metadata change");
return; // useless event
EventKind::Modify(ModifyKind::Metadata(kind)) => {
if kind == MetadataKind::WriteTime && polling.is_some() {
// the only event triggered usable when polling on WSL2
} else {
debug!("ignoring metadata change: {:?}", kind);
return; // useless event
}
}
EventKind::Modify(ModifyKind::Data(DataChange::Any)) => {
debug!("ignoring 'any' data change");
Expand Down Expand Up @@ -73,7 +69,20 @@ impl Watcher {
}
}
Err(e) => warn!("watch error: {:?}", e),
})?;
};
let use_polling = polling.is_some();
let notify_watcher: Result<Box<dyn NotifyWatcher>, notify::Error> = if use_polling {
let config = notify::Config::default()
.with_poll_interval(Duration::from_secs(polling.unwrap_or_default()))
.with_compare_contents(true);
notify::PollWatcher::new(event_handler, config)
.map(|w| Box::new(w) as Box<dyn NotifyWatcher>)
} else {
notify::recommended_watcher(event_handler)
.map(|w| Box::new(w) as Box<dyn NotifyWatcher>)
};
let mut notify_watcher = notify_watcher?;

for path in paths_to_watch {
if !path.exists() {
warn!("watch path doesn't exist: {:?}", path);
Expand Down