From ff1fa0262a675ce10f02b983732cf2c867cb003c Mon Sep 17 00:00:00 2001 From: Jeremy Wall Date: Sun, 29 Jan 2017 17:39:14 -0600 Subject: [PATCH] Cleanups. * The RwLock was overkill we only needed a lock. * Add some documentation. * Remove some debug println! statements. --- src/events.rs | 1 - src/file.rs | 36 +++++++++++++++--------------------- src/main.rs | 9 +++++---- 3 files changed, 20 insertions(+), 26 deletions(-) diff --git a/src/events.rs b/src/events.rs index 3988344..128f540 100644 --- a/src/events.rs +++ b/src/events.rs @@ -23,7 +23,6 @@ pub enum WatchEventType { impl From for WatchEventType { fn from(e: DebouncedEvent) -> WatchEventType { - println!("Found event: {:?}", e); match e { DebouncedEvent::Chmod(_) => WatchEventType::Touched, DebouncedEvent::Create(_) => WatchEventType::Touched, diff --git a/src/file.rs b/src/file.rs index 860fb8f..a01a761 100644 --- a/src/file.rs +++ b/src/file.rs @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. use std::thread; -use std::sync::{Arc,RwLock}; +use std::sync::{Arc,Mutex}; use std::time::Duration; use std::path::Path; use std::sync::mpsc::channel; @@ -37,33 +37,27 @@ impl<'a> FileProcess<'a> { } } -fn spawn_runner_thread(lock: Arc>, cmd: String, poll: Duration) { +fn spawn_runner_thread(lock: Arc>, cmd: String, poll: Duration) { thread::spawn(move || { loop { // Wait our requisit number of seconds thread::sleep(poll); // Default to not running the command. - let should_run = { // get a new scope for our read lock. - // Check our evt drop off. - *(lock.read().unwrap()) - }; // drop our read lock - if should_run { - { // Set a new scope for our write lock. - let mut signal = lock.write().unwrap(); - // set signal to false so we won't trigger on the - // next loop iteration unless we recieved more events. - *signal = false; - // Run our command! - if let Err(err) = run_cmd(&cmd) { - println!("{:?}", err) - } - } // drop our write lock. + let mut signal = lock.lock().unwrap(); + if *signal { + // set signal to false so we won't trigger on the + // next loop iteration unless we recieved more events. + *signal = false; + // Run our command! + if let Err(err) = run_cmd(&cmd) { + println!("{:?}", err) + } } } }); } -fn wait_for_fs_events(lock: Arc>, method: WatchEventType, file: &str) -> Result<(), CommandError> { +fn wait_for_fs_events(lock: Arc>, method: WatchEventType, file: &str) -> Result<(), CommandError> { // Notify requires a channel for communication. let (tx, rx) = channel(); let mut watcher = try!(watcher(tx, Duration::from_secs(1))); @@ -88,12 +82,12 @@ fn wait_for_fs_events(lock: Arc>, method: WatchEventType, file: &st }, WatchEventType::Touched => { if method == WatchEventType::Touched { - let mut signal = lock.write().unwrap(); + let mut signal = lock.lock().unwrap(); *signal = true; } }, WatchEventType::Changed => { - let mut signal = lock.write().unwrap(); + let mut signal = lock.lock().unwrap(); *signal = true; } } @@ -109,7 +103,7 @@ impl<'a> Process for FileProcess<'a> { } // TODO(jeremy): Is this sufficent or do we want to ignore // any events that come in while the command is running? - let lock = Arc::new(RwLock::new(false)); + let lock = Arc::new(Mutex::new(false)); spawn_runner_thread(lock.clone(), self.cmd.to_string(), self.poll); wait_for_fs_events(lock, self.method.clone(), self.file) } diff --git a/src/main.rs b/src/main.rs index 1a8435c..46a3e2a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -11,6 +11,7 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. +//! runwhen - A utility that runs commands on user defined triggers. #[macro_use] extern crate clap; extern crate humantime; @@ -64,7 +65,7 @@ fn do_flags<'a>() -> clap::ArgMatches<'a> { fn main() { let app = do_flags(); // Unwrap because this flag is required. - let cmd = app.value_of("cmd").unwrap(); + let cmd = app.value_of("cmd").expect("cmd flag is required"); let mut process: Option> = None; if let Some(matches) = app.subcommand_matches("watch") { // Unwrap because this flag is required. @@ -74,11 +75,11 @@ fn main() { method = WatchEventType::Touched; } let poll = matches.value_of("poll").unwrap_or("5s"); - let dur = humantime::parse_duration(poll).unwrap(); + let dur = humantime::parse_duration(poll).expect("Invalid poll value."); process = Some(Box::new(FileProcess::new(cmd, file, method, dur))); } else if let Some(matches) = app.subcommand_matches("timer") { // Unwrap because this flag is required. - let dur = humantime::parse_duration(matches.value_of("duration").unwrap()); + let dur = humantime::parse_duration(matches.value_of("duration").expect("duration flag is required")); match dur { Ok(duration) =>{ let max_repeat = if let Some(val) = matches.value_of("repeat") { @@ -102,7 +103,7 @@ fn main() { } } else if let Some(matches) = app.subcommand_matches("success") { // unwrap because this is required. - let ifcmd = matches.value_of("ifcmd").unwrap(); + let ifcmd = matches.value_of("ifcmd").expect("ifcmd flag is required"); let dur = humantime::parse_duration( matches.value_of("poll").unwrap_or("5s")); process = match dur {