remove unneeded function

This commit is contained in:
Florian Stecker 2023-01-01 10:51:37 +01:00
parent 4a5d5a464a
commit ec4ee605f4

View File

@ -43,67 +43,6 @@ pub struct Cli {
verbose: u8,
}
/// runs the command if one of the following conditions is satisfied:
/// - `new_mail` is true
/// - the command hasn't been run yet
/// - `cli.interval` is set and the last run was at least `cli.interval` minus 1 second ago
///
/// The return value is the duration to wait until command should be run again
/// (assuming no new mail arrives in the meantime). This is `cli.interval` if
/// the command was run, and `cli.interval` minus the time elapsed since the last run
/// in case the command was not run.
/// If `cli.interval` is `None`, the return value will be `None`.
pub fn run_command_if_needed(cli: &Cli, lastrun_mutex: &Mutex<Option<SystemTime>>, new_mail: bool) -> Option<Duration> {
// locks the mutex until the end of the function, also preventing that the command is run concurrently
let mut last = lastrun_mutex.lock().unwrap();
let run;
if new_mail {
run = true;
} else {
match *last {
None => {
run = true;
},
Some(last_inner) => {
match cli.interval {
None => {
run = false;
},
Some(int) => {
let min_duration = Duration::from_secs(int - 1);
let duration = last_inner.elapsed().unwrap_or(Duration::ZERO);
if duration > min_duration {
run = true;
} else {
run = false;
}
}
}
}
}
}
if run {
println!("Running command ...");
Command::new(cli.command.as_os_str())
.output()
.expect("command execution failed");
println!("Command finished.");
*last = Some(SystemTime::now());
}
// subtract the time already elapsed since last run
return cli.interval.map(|int| {
Duration::from_secs(int).saturating_sub(
last
.expect("command should have run at least once")
.elapsed()
.unwrap_or(Duration::ZERO))
});
}
#[derive(Debug)]
struct Status {
connected: bool,