mirror of
https://github.com/zaphar/runwhen.git
synced 2025-07-22 20:39:49 -04:00
Better poll handling and respect the watch poll flag
This commit is contained in:
parent
4d33126c69
commit
38b0b6aa59
55
flake.lock
generated
55
flake.lock
generated
@ -31,21 +31,6 @@
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"flake-utils_2": {
|
||||
"locked": {
|
||||
"lastModified": 1637014545,
|
||||
"narHash": "sha256-26IZAc5yzlD9FlDT54io1oqG/bBoyka+FJk5guaX4x4=",
|
||||
"owner": "numtide",
|
||||
"repo": "flake-utils",
|
||||
"rev": "bba5dcc8e0b20ab664967ad83d24d64cb64ec4f4",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "numtide",
|
||||
"repo": "flake-utils",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"naersk": {
|
||||
"inputs": {
|
||||
"nixpkgs": "nixpkgs"
|
||||
@ -78,49 +63,11 @@
|
||||
"type": "indirect"
|
||||
}
|
||||
},
|
||||
"nixpkgs_2": {
|
||||
"locked": {
|
||||
"lastModified": 1650222748,
|
||||
"narHash": "sha256-AHh/goEfG5hlhIMVgGQwACbuv5Wit2ND9vrcB4QthJs=",
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "ba88a5afa6fff7710c17b5423ff9d721386c4164",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"root": {
|
||||
"inputs": {
|
||||
"flake-compat": "flake-compat",
|
||||
"flake-utils": "flake-utils",
|
||||
"naersk": "naersk",
|
||||
"nixpkgs": "nixpkgs_2",
|
||||
"rust-overlay": "rust-overlay"
|
||||
}
|
||||
},
|
||||
"rust-overlay": {
|
||||
"inputs": {
|
||||
"flake-utils": "flake-utils_2",
|
||||
"nixpkgs": [
|
||||
"nixpkgs"
|
||||
]
|
||||
},
|
||||
"locked": {
|
||||
"lastModified": 1650162887,
|
||||
"narHash": "sha256-e23LlN7NQGxrsSWNNAjyvrWlZ3kwFSav9kXbayibKWc=",
|
||||
"owner": "oxalica",
|
||||
"repo": "rust-overlay",
|
||||
"rev": "26b570500cdd7a359526524e9abad341891122a6",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "oxalica",
|
||||
"repo": "rust-overlay",
|
||||
"type": "github"
|
||||
"naersk": "naersk"
|
||||
}
|
||||
}
|
||||
},
|
||||
|
45
src/file.rs
45
src/file.rs
@ -14,7 +14,7 @@
|
||||
use std::path::Path;
|
||||
use std::sync::mpsc::{channel, Receiver, Sender};
|
||||
use std::thread;
|
||||
use std::time::Duration;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use notify::{watcher, RecursiveMode, Watcher};
|
||||
|
||||
@ -67,24 +67,45 @@ fn watch_for_change_events(
|
||||
println!("Spawning command");
|
||||
exec.spawn().expect("Failed to start command");
|
||||
println!("Starting watch loop");
|
||||
let mut poll_time = Instant::now();
|
||||
let mut changed = false;
|
||||
println!("Waiting for first change event");
|
||||
let _ = ch.recv().expect("Channel was closed!!!");
|
||||
loop {
|
||||
// Wait our requisit number of seconds
|
||||
if let Some(poll) = poll {
|
||||
thread::sleep(dbg!(poll));
|
||||
}
|
||||
//if let Err(err) = exec.check() {
|
||||
// println!("Error running command! {}", err);
|
||||
// println!("Continuing");
|
||||
//};
|
||||
// Default to not running the command.
|
||||
if !run_loop_step(&ch, &mut exec) {
|
||||
println!("Failed to start command");
|
||||
println!("We have a poll time {:?}", poll);
|
||||
// If our time has passed and the value has changed then run our step immediately
|
||||
if changed && Instant::now().duration_since(poll_time) >= poll {
|
||||
println!(
|
||||
"Recieved changed event and waited: {:?}",
|
||||
Instant::now().duration_since(poll_time)
|
||||
);
|
||||
if !run_loop_step(&mut exec) {
|
||||
println!("Failed to start command");
|
||||
}
|
||||
changed = false;
|
||||
continue;
|
||||
}
|
||||
println!("Waiting for next change event");
|
||||
let _ = ch.recv().expect("Channel was closed!!!");
|
||||
changed = true;
|
||||
poll_time = Instant::now();
|
||||
continue;
|
||||
} else {
|
||||
println!("We do not have a poll time");
|
||||
println!("Waiting for next change event");
|
||||
let _ = ch.recv().expect("Channel was closed!!!");
|
||||
if !run_loop_step(&mut exec) {
|
||||
println!("Failed to start command");
|
||||
}
|
||||
}
|
||||
poll_time = Instant::now();
|
||||
changed = false;
|
||||
}
|
||||
}
|
||||
|
||||
fn run_loop_step(ch: &Receiver<()>, exec: &mut CancelableProcess) -> bool {
|
||||
let _ = ch.recv().unwrap();
|
||||
fn run_loop_step(exec: &mut CancelableProcess) -> bool {
|
||||
// We always want to check on our process each iteration of the loop.
|
||||
// set signal to false so we won't trigger on the
|
||||
// next loop iteration unless we recieved more events.
|
||||
|
@ -48,7 +48,7 @@ fn do_flags() -> clap::ArgMatches {
|
||||
.takes_value(true).help("File or directory to watch for changes"),
|
||||
)
|
||||
.arg(arg!(--touch).name("filetouch").help("Use file or directory timestamps to monitor for changes."))
|
||||
.arg(arg!(--poll).value_parser(value_parser!(humantime::Duration)).help("Duration of time between polls")))
|
||||
.arg(arg!(--poll).name("poll").takes_value(true).value_parser(value_parser!(humantime::Duration)).help("Duration of time between polls")))
|
||||
.subcommand(
|
||||
clap::Command::new("timer")
|
||||
.about("Run command on a timer")
|
||||
@ -90,6 +90,7 @@ fn main() {
|
||||
Some(d) => Some((*d).into()),
|
||||
None => None,
|
||||
};
|
||||
println!("Enforcing a poll time of {:?}", duration);
|
||||
Box::new(FileProcess::new(cmd, maybe_env, file, method, duration))
|
||||
} else if let Some(matches) = app.subcommand_matches("timer") {
|
||||
// TODO(jwall): This should use cancelable commands.
|
||||
|
Loading…
x
Reference in New Issue
Block a user