Skip to content

Commit

Permalink
monitor loop
Browse files Browse the repository at this point in the history
  • Loading branch information
slivingston committed Aug 18, 2024
1 parent f839d3a commit 22f6ba2
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
27 changes: 23 additions & 4 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1087,9 +1087,24 @@ fn monitor_subcommand(matches: &clap::ArgMatches) -> Result<(), CliError> {
Err(err) => return CliError::new_std(err, 1),
};

match monitor::run(&local_config, wd_index) {
Ok(()) => Ok(()),
Err(err) => CliError::new_std(err, 1),
if matches.is_present("loop") {
let duration = match matches.value_of("loop").unwrap().parse::<u64>() {
Ok(d) => d,
Err(err) => return CliError::new(err, 1),
};
match monitor::run_loop(
&local_config,
wd_index,
std::time::Duration::from_secs(duration),
) {
Ok(()) => Ok(()),
Err(err) => CliError::new_std(err, 1),
}
} else {
match monitor::run(&local_config, wd_index) {
Ok(()) => Ok(()),
Err(err) => CliError::new_std(err, 1),
}
}
}

Expand Down Expand Up @@ -1302,7 +1317,11 @@ pub fn main() -> Result<(), CliError> {
.about("Detect and handle errors in a deployment")
.arg(Arg::with_name("id_prefix")
.value_name("ID")
.help("id of workspace deployment to monitor")))
.help("id of workspace deployment to monitor"))
.arg(Arg::with_name("loop")
.long("loop")
.value_name("DURATION")
.help("Repeat monitor checks every DURATION seconds")))
;

let matches = app.get_matches();
Expand Down
18 changes: 18 additions & 0 deletions src/monitor.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::process::Command;
use std::thread::sleep;

use crate::api;
use crate::check::Error;
Expand Down Expand Up @@ -48,3 +49,20 @@ pub fn run_dry(local_config: &Config, wd_index: usize) -> Result<(), Box<dyn std
pub fn run(local_config: &Config, wd_index: usize) -> Result<(), Box<dyn std::error::Error>> {
run_opt(local_config, wd_index, true)
}


pub fn run_loop(
local_config: &Config,
wd_index: usize,
duration: std::time::Duration,
) -> Result<(), Box<dyn std::error::Error>> {
let mut result;
loop {
println!("{:?}", duration);
result = run(local_config, wd_index);
if result.is_err() {
return result;
}
sleep(duration);
}
}

0 comments on commit 22f6ba2

Please sign in to comment.