mirror of
https://github.com/zaphar/runwhen.git
synced 2025-07-24 20:59: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 {
|
impl From<DebouncedEvent> for WatchEventType {
|
||||||
fn from(e: DebouncedEvent) -> WatchEventType {
|
fn from(e: DebouncedEvent) -> WatchEventType {
|
||||||
println!("Found event: {:?}", e);
|
|
||||||
match e {
|
match e {
|
||||||
DebouncedEvent::Chmod(_) => WatchEventType::Touched,
|
DebouncedEvent::Chmod(_) => WatchEventType::Touched,
|
||||||
DebouncedEvent::Create(_) => 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
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use std::sync::{Arc,RwLock};
|
use std::sync::{Arc,Mutex};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::sync::mpsc::channel;
|
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 || {
|
thread::spawn(move || {
|
||||||
loop {
|
loop {
|
||||||
// Wait our requisit number of seconds
|
// Wait our requisit number of seconds
|
||||||
thread::sleep(poll);
|
thread::sleep(poll);
|
||||||
// Default to not running the command.
|
// Default to not running the command.
|
||||||
let should_run = { // get a new scope for our read lock.
|
let mut signal = lock.lock().unwrap();
|
||||||
// Check our evt drop off.
|
if *signal {
|
||||||
*(lock.read().unwrap())
|
// set signal to false so we won't trigger on the
|
||||||
}; // drop our read lock
|
// next loop iteration unless we recieved more events.
|
||||||
if should_run {
|
*signal = false;
|
||||||
{ // Set a new scope for our write lock.
|
// Run our command!
|
||||||
let mut signal = lock.write().unwrap();
|
if let Err(err) = run_cmd(&cmd) {
|
||||||
// set signal to false so we won't trigger on the
|
println!("{:?}", err)
|
||||||
// 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.
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
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.
|
// Notify requires a channel for communication.
|
||||||
let (tx, rx) = channel();
|
let (tx, rx) = channel();
|
||||||
let mut watcher = try!(watcher(tx, Duration::from_secs(1)));
|
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 => {
|
WatchEventType::Touched => {
|
||||||
if method == WatchEventType::Touched {
|
if method == WatchEventType::Touched {
|
||||||
let mut signal = lock.write().unwrap();
|
let mut signal = lock.lock().unwrap();
|
||||||
*signal = true;
|
*signal = true;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
WatchEventType::Changed => {
|
WatchEventType::Changed => {
|
||||||
let mut signal = lock.write().unwrap();
|
let mut signal = lock.lock().unwrap();
|
||||||
*signal = true;
|
*signal = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -109,7 +103,7 @@ impl<'a> Process for FileProcess<'a> {
|
|||||||
}
|
}
|
||||||
// TODO(jeremy): Is this sufficent or do we want to ignore
|
// TODO(jeremy): Is this sufficent or do we want to ignore
|
||||||
// any events that come in while the command is running?
|
// 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);
|
spawn_runner_thread(lock.clone(), self.cmd.to_string(), self.poll);
|
||||||
wait_for_fs_events(lock, self.method.clone(), self.file)
|
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.
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
//! runwhen - A utility that runs commands on user defined triggers.
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
extern crate clap;
|
extern crate clap;
|
||||||
extern crate humantime;
|
extern crate humantime;
|
||||||
@ -64,7 +65,7 @@ fn do_flags<'a>() -> clap::ArgMatches<'a> {
|
|||||||
fn main() {
|
fn main() {
|
||||||
let app = do_flags();
|
let app = do_flags();
|
||||||
// Unwrap because this flag is required.
|
// 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;
|
let mut process: Option<Box<Process>> = None;
|
||||||
if let Some(matches) = app.subcommand_matches("watch") {
|
if let Some(matches) = app.subcommand_matches("watch") {
|
||||||
// Unwrap because this flag is required.
|
// Unwrap because this flag is required.
|
||||||
@ -74,11 +75,11 @@ fn main() {
|
|||||||
method = WatchEventType::Touched;
|
method = WatchEventType::Touched;
|
||||||
}
|
}
|
||||||
let poll = matches.value_of("poll").unwrap_or("5s");
|
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)));
|
process = Some(Box::new(FileProcess::new(cmd, file, method, dur)));
|
||||||
} else if let Some(matches) = app.subcommand_matches("timer") {
|
} else if let Some(matches) = app.subcommand_matches("timer") {
|
||||||
// Unwrap because this flag is required.
|
// 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 {
|
match dur {
|
||||||
Ok(duration) =>{
|
Ok(duration) =>{
|
||||||
let max_repeat = if let Some(val) = matches.value_of("repeat") {
|
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") {
|
} else if let Some(matches) = app.subcommand_matches("success") {
|
||||||
// unwrap because this is required.
|
// 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(
|
let dur = humantime::parse_duration(
|
||||||
matches.value_of("poll").unwrap_or("5s"));
|
matches.value_of("poll").unwrap_or("5s"));
|
||||||
process = match dur {
|
process = match dur {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user