mirror of
https://github.com/zaphar/runwhen.git
synced 2025-07-22 20:39:49 -04:00
Cleanups.
* The RwLock was overkill we only needed a lock. * Add some documentation. * Remove some debug println! statements.
This commit is contained in:
parent
4513694f25
commit
ff1fa0262a
@ -23,7 +23,6 @@ pub enum WatchEventType {
|
||||
|
||||
impl From<DebouncedEvent> for WatchEventType {
|
||||
fn from(e: DebouncedEvent) -> WatchEventType {
|
||||
println!("Found event: {:?}", e);
|
||||
match e {
|
||||
DebouncedEvent::Chmod(_) => WatchEventType::Touched,
|
||||
DebouncedEvent::Create(_) => WatchEventType::Touched,
|
||||
|
36
src/file.rs
36
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<RwLock<bool>>, cmd: String, poll: Duration) {
|
||||
fn spawn_runner_thread(lock: Arc<Mutex<bool>>, 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<RwLock<bool>>, method: WatchEventType, file: &str) -> Result<(), CommandError> {
|
||||
fn wait_for_fs_events(lock: Arc<Mutex<bool>>, 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<RwLock<bool>>, 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)
|
||||
}
|
||||
|
@ -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<Box<Process>> = 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 {
|
||||
|
Loading…
x
Reference in New Issue
Block a user