Skip to content
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

Use PollWatcher to always get filesystem updates #2240

Closed
wants to merge 2 commits into from
Closed
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
62 changes: 46 additions & 16 deletions src/cmd/serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use mdbook::errors::*;
use mdbook::utils;
use mdbook::utils::fs::get_404_output_file;
use mdbook::MDBook;
use notify::PollWatcher;
use notify::RecommendedWatcher;
use std::net::{SocketAddr, ToSocketAddrs};
use std::path::PathBuf;
use tokio::sync::broadcast;
Expand Down Expand Up @@ -43,6 +45,9 @@ pub fn make_subcommand() -> Command {
.help("Port to use for HTTP connections"),
)
.arg_open()
.arg(arg!(--compat "Watch files in compatibility mode.\n\
Use this if your environment doesn't support filesystem events (Windows, Docker, NFS),
or if you encounter issues otherwise"))
}

// Serve command implementation
Expand Down Expand Up @@ -97,23 +102,48 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
}

#[cfg(feature = "watch")]
watch::trigger_on_change(&book, move |paths, book_dir| {
info!("Files changed: {:?}", paths);
info!("Building book...");

// FIXME: This area is really ugly because we need to re-set livereload :(
let result = MDBook::load(book_dir).and_then(|mut b| {
update_config(&mut b);
b.build()
});
let polling = args.get_flag("compat");

if let Err(e) = result {
error!("Unable to load the book");
utils::log_backtrace(&e);
} else {
let _ = tx.send(Message::text("reload"));
}
});
#[cfg(feature = "watch")]
if polling {
debug!("Using PollWatcher backend");
watch::trigger_on_change::<_, PollWatcher>(&book, move |paths, book_dir| {
info!("Files changed: {:?}", paths);
info!("Building book...");

// FIXME: This area is really ugly because we need to re-set livereload :(
let result = MDBook::load(book_dir).and_then(|mut b| {
update_config(&mut b);
b.build()
});

if let Err(e) = result {
error!("Unable to load the book");
utils::log_backtrace(&e);
} else {
let _ = tx.send(Message::text("reload"));
}
});
} else {
debug!("Using RecommendWatcher backend");
watch::trigger_on_change::<_, RecommendedWatcher>(&book, move |paths, book_dir| {
info!("Files changed: {:?}", paths);
info!("Building book...");

// FIXME: This area is really ugly because we need to re-set livereload :(
let result = MDBook::load(book_dir).and_then(|mut b| {
update_config(&mut b);
b.build()
});

if let Err(e) = result {
error!("Unable to load the book");
utils::log_backtrace(&e);
} else {
let _ = tx.send(Message::text("reload"));
}
});
}

let _ = thread_handle.join();

Expand Down
62 changes: 48 additions & 14 deletions src/cmd/watch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ use ignore::gitignore::Gitignore;
use mdbook::errors::Result;
use mdbook::utils;
use mdbook::MDBook;
use notify::PollWatcher;
use notify::RecommendedWatcher;
use notify::Watcher;
use notify_debouncer_mini::Config;
use pathdiff::diff_paths;
use std::path::{Path, PathBuf};
use std::sync::mpsc::channel;
Expand All @@ -17,6 +21,9 @@ pub fn make_subcommand() -> Command {
.arg_dest_dir()
.arg_root_dir()
.arg_open()
.arg(arg!(--compat "Watch files in compatibility mode.\n\
Use this if your environment doesn't support filesystem events (Windows, Docker, NFS),
or if you encounter issues otherwise"))
}

// Watch command implementation
Expand All @@ -41,18 +48,37 @@ pub fn execute(args: &ArgMatches) -> Result<()> {
open(path);
}

trigger_on_change(&book, |paths, book_dir| {
info!("Files changed: {:?}\nBuilding book...\n", paths);
let result = MDBook::load(book_dir).and_then(|mut b| {
update_config(&mut b);
b.build()
});
let polling = args.get_flag("compat");

if let Err(e) = result {
error!("Unable to build the book");
utils::log_backtrace(&e);
}
});
if polling {
debug!("Using PollWatcher backend");
trigger_on_change::<_, PollWatcher>(&book, |paths, book_dir| {
info!("Files changed: {:?}\nBuilding book...\n", paths);
let result = MDBook::load(book_dir).and_then(|mut b| {
update_config(&mut b);
b.build()
});

if let Err(e) = result {
error!("Unable to build the book");
utils::log_backtrace(&e);
}
});
} else {
debug!("Using RecommendWatcher backend");
trigger_on_change::<_, RecommendedWatcher>(&book, |paths, book_dir| {
info!("Files changed: {:?}\nBuilding book...\n", paths);
let result = MDBook::load(book_dir).and_then(|mut b| {
update_config(&mut b);
b.build()
});

if let Err(e) = result {
error!("Unable to build the book");
utils::log_backtrace(&e);
}
});
};

Ok(())
}
Expand Down Expand Up @@ -109,16 +135,24 @@ fn filter_ignored_files(ignore: Gitignore, paths: &[PathBuf]) -> Vec<PathBuf> {
}

/// Calls the closure when a book source file is changed, blocking indefinitely.
pub fn trigger_on_change<F>(book: &MDBook, closure: F)
pub fn trigger_on_change<F, W>(book: &MDBook, closure: F)
where
F: Fn(Vec<PathBuf>, &Path),
W: 'static + Watcher + Send,
{
use notify::RecursiveMode::*;

// Create a channel to receive the events.
let (tx, rx) = channel();

let mut debouncer = match notify_debouncer_mini::new_debouncer(Duration::from_secs(1), tx) {
// Notify backend configuration
let backend_config = notify::Config::default().with_poll_interval(Duration::from_secs(1));
// Debouncer configuration
let debouncer_config = Config::default()
.with_timeout(Duration::from_secs(1))
.with_notify_config(backend_config);

let mut debouncer = match notify_debouncer_mini::new_debouncer_opt::<_, W>(debouncer_config, tx)
{
Ok(d) => d,
Err(e) => {
error!("Error while trying to watch the files:\n\n\t{:?}", e);
Expand Down