Skip to content

Commit

Permalink
Add(watcher): change for PR
Browse files Browse the repository at this point in the history
  • Loading branch information
amribm committed Sep 13, 2024
1 parent 1802ece commit ed86d4a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
4 changes: 2 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,10 +488,10 @@ pub async fn load_configs(
) -> Result<Config, ExitCode> {
let config_paths = config::process_paths(config_paths).ok_or(exitcode::CONFIG)?;

if watcher_conf.is_some() {
if let Some(watcher_conf) = watcher_conf {
// Start listening for config changes immediately.
config::watcher::spawn_thread(
watcher_conf.unwrap(),
watcher_conf,
signal_handler.clone_tx(),
config_paths.iter().map(Into::into),
None,
Expand Down
45 changes: 31 additions & 14 deletions src/config/watcher.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use std::{path::PathBuf, time::Duration};
use std::{
path::{Path, PathBuf},
time::Duration,
};
use std::{
sync::mpsc::{channel, Receiver},
thread,
};

use notify::{
recommended_watcher, EventKind, PollWatcher, RecommendedWatcher, RecursiveMode,
Watcher as NotifyWatcher,
};
use notify::{recommended_watcher, EventKind, RecursiveMode};

use crate::Error;

Expand All @@ -31,25 +31,42 @@ pub enum WatcherConfig {

enum Watcher {
/// recommended watcher for os, usually inotify for linux based systems
RecommendedWatcher(RecommendedWatcher),
RecommendedWatcher(notify::RecommendedWatcher),
/// poll based watcher. for watching files from NFS.
PollWatcher(PollWatcher),
PollWatcher(notify::PollWatcher),
}

impl Watcher {
fn add_paths(&mut self, config_paths: &[PathBuf]) -> Result<(), Error> {
let watcher: &mut dyn NotifyWatcher = match self {
&mut Watcher::RecommendedWatcher(ref mut w) => w,
&mut Watcher::PollWatcher(ref mut w) => w,
};

for path in config_paths {
watcher.watch(path, RecursiveMode::Recursive)?;
self.watch(path, RecursiveMode::Recursive)?;
}
Ok(())
}

fn watch(&mut self, path: &Path, recursive_mode: RecursiveMode) -> Result<(), Error> {
match self {
&mut Watcher::RecommendedWatcher(ref mut watcher) => {
watcher.watch(path, recursive_mode)?
}
&mut Watcher::PollWatcher(ref mut watcher) => watcher.watch(path, recursive_mode)?,
}
Ok(())
}
}

// impl From<notify::RecommendedWatcher> for Watcher {
// fn from(recommended_watcher: notify::RecommendedWatcher) -> Self {
// Watcher::RecommendedWatcher(recommended_watcher)
// }
// }

// impl From<notify::PollWatcher> for Watcher {
// fn from(poll_watcher: notify::PollWatcher) -> Self {
// Watcher::PollWatcher(poll_watcher)
// }
// }

/// Sends a ReloadFromDisk on config_path changes.
/// Accumulates file changes until no change for given duration has occurred.
/// Has best effort guarantee of detecting all file changes from the end of
Expand Down Expand Up @@ -139,7 +156,7 @@ fn create_watcher(
WatcherConfig::PollWatcher(interval) => {
let config =
notify::Config::default().with_poll_interval(Duration::from_secs(*interval));
let poll_watcher = PollWatcher::new(sender, config)?;
let poll_watcher = notify::PollWatcher::new(sender, config)?;
let mut watcher = Watcher::PollWatcher(poll_watcher);
watcher.add_paths(config_paths)?;
Ok((watcher, receiver))
Expand Down

0 comments on commit ed86d4a

Please sign in to comment.