-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds non-block watchers for long running tasks (#66)
- Loading branch information
1 parent
a6c710a
commit ffdc34a
Showing
13 changed files
with
431 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
- name: run long task 2 | ||
run: "bash examples/longtask.sh long 2" | ||
change: ["src/**", "tests/integration/workdir/**"] | ||
|
||
- name: run long task 3 | ||
run: "bash examples/longtask.sh short 3" | ||
change: ["src/**", "tests/integration/workdir/**"] | ||
|
||
- name: run long task 4 | ||
run: "bash examples/longtask.sh short 4" | ||
change: ["src/**", "tests/integration/workdir/**"] | ||
run_on_init: true | ||
|
||
- name: run long task 5 | ||
run: "bash examples/longtask.sh short 5" | ||
change: ["src/**", "tests/integration/workdir/**"] | ||
run_on_init: true | ||
|
||
- name: run long task 6 | ||
run: ["bash examples/longtask.sh list 3", "bash examples/longtask.sh list 4"] | ||
change: ["src/**", "tests/integration/workdir/**"] | ||
run_on_init: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/usr/bin/env bash | ||
|
||
# Simulate a long task and count time | ||
count=0 | ||
echo "Started task $1 $2" | ||
while true; do | ||
echo "Long task running... $count" | ||
count=$((count+1)) | ||
sleep 3 | ||
|
||
if [ $count -eq "$2" ]; then | ||
echo "Task $1 finished" | ||
break | ||
fi | ||
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
extern crate notify; | ||
|
||
use std::sync::mpsc::channel; | ||
use std::sync::mpsc::TryRecvError; | ||
|
||
use self::notify::{DebouncedEvent, RecommendedWatcher, RecursiveMode, Watcher}; | ||
use std::thread; | ||
use std::time::Duration; | ||
|
||
use cli::Command; | ||
use stdout; | ||
use watches::Watches; | ||
use workers; | ||
|
||
/// # `WatchNonBlockCommand` | ||
/// | ||
/// Starts watcher to listen the change events configured | ||
/// in watch.yaml, upon change it cancell all previous tasks, including the running | ||
/// task and initiate a new set of tasks. | ||
/// | ||
pub struct WatchNonBlockCommand { | ||
watches: Watches, | ||
verbose: bool, | ||
} | ||
|
||
impl WatchNonBlockCommand { | ||
pub fn new(watches: Watches, verbose: bool) -> Self { | ||
if verbose { | ||
stdout::verbose(&format!("watches {:?}", watches)); | ||
} | ||
|
||
WatchNonBlockCommand { watches, verbose } | ||
} | ||
} | ||
|
||
impl Command for WatchNonBlockCommand { | ||
fn execute(&self) -> Result<(), String> { | ||
if self.verbose { | ||
stdout::verbose(&format!("Verbose mode enabled.")); | ||
}; | ||
|
||
let (tx, rx) = channel(); | ||
let mut watcher: RecommendedWatcher = | ||
Watcher::new(tx, Duration::from_secs(2)).expect("Unable to create watcher"); | ||
|
||
if let Err(err) = watcher.watch(".", RecursiveMode::Recursive) { | ||
return Err(format!("Unable to watch current directory {:?}", err)); | ||
} | ||
|
||
let worker = workers::Worker::new(self.verbose); | ||
|
||
if let Some(rules) = self.watches.run_on_init() { | ||
stdout::info(&format!("Running on init commands.")); | ||
if let Err(err) = worker.schedule(rules.clone()) { | ||
stdout::error(&format!("failed to initiate next run: {:?}", err)); | ||
} | ||
} | ||
|
||
stdout::info(&format!("Watching...")); | ||
loop { | ||
match rx.try_recv() { | ||
Ok(event) => { | ||
if let DebouncedEvent::Create(path) = event { | ||
let path_str = path.into_os_string().into_string().unwrap(); | ||
|
||
if self.verbose { | ||
stdout::verbose(&format!("Changed file: {}", path_str)); | ||
}; | ||
|
||
if let Some(rules) = self.watches.watch(&*path_str) { | ||
if self.verbose { | ||
stdout::verbose(&format!("Triggered by change in: {}", path_str)); | ||
}; | ||
|
||
if let Err(err) = worker.cancel_running_tasks() { | ||
stdout::error(&format!( | ||
"failed to cancel current running tasks: {:?}", | ||
err | ||
)); | ||
} | ||
|
||
if let Err(err) = worker.schedule(rules.clone()) { | ||
stdout::error(&format!("failed to initiate next run: {:?}", err)); | ||
} | ||
} | ||
} | ||
} | ||
|
||
Err(err) => { | ||
if err != TryRecvError::Empty { | ||
stdout::error(&format!("failed to receive event: {:?}", err)); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,4 +5,5 @@ pub mod cmd; | |
pub mod rules; | ||
pub mod stdout; | ||
pub mod watches; | ||
pub mod workers; | ||
pub mod yaml; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.