Refactor 'Retry' instance creation and modify 'run_command' function

Changed the approach for creating 'Retry' instance in main() to avoid mutation behavior by setting 'max_retries', 'timespan' and 'restart_name' initially. The 'per_minute' and 'per_hour' arguments now directly modify these values. Additionally, the 'run_command' function and related functions were refactored to pass 'Retry' instance by value, not reference and to handle 'retry' history separately for each command execution. These changes were made to improve code readability and maintainability.
This commit is contained in:
Ian Wijma 2023-12-02 21:52:09 +11:00
parent 5c19c27136
commit ef1e2c56c0
1 changed files with 36 additions and 27 deletions

View File

@ -35,24 +35,29 @@ struct Retry {
fn main() { fn main() {
let arguments = Arguments::parse(); let arguments = Arguments::parse();
let mut retry: Retry = Retry { let mut max_retries = 4;
command: arguments.command, let mut timespan = SECONDS_IN_A_MINUTE;
history: Vec::new(), let mut restart_name = String::from("minute");
max_retries: 4,
timespan: SECONDS_IN_A_MINUTE,
restart_delay: arguments.delay,
restart_name: "minute".to_string(),
};
if arguments.per_minute > 0 { if arguments.per_minute > 0 {
retry.max_retries = arguments.per_minute; max_retries = arguments.per_minute;
} else if arguments.per_hour > 0 { } else if arguments.per_hour > 0 {
retry.max_retries = arguments.per_hour; max_retries = arguments.per_hour;
retry.timespan = SECONDS_IN_A_HOUR; timespan = SECONDS_IN_A_HOUR;
retry.restart_name = "hour".to_string(); restart_name = String::from("hour");
} }
run_command(&mut retry)
let retry: Retry = Retry {
max_retries,
timespan,
restart_name,
command: arguments.command,
history: Vec::new(),
restart_delay: arguments.delay,
};
run_command(retry)
} }
fn get_now() -> u64 { fn get_now() -> u64 {
@ -62,26 +67,29 @@ fn get_now() -> u64 {
.as_secs(); .as_secs();
} }
fn push_history(retry: &mut Retry) { fn push_history(mut history: Vec<u64>, timespan: u16) -> Vec<u64> {
retry.history.push(get_now() + u64::from(retry.timespan)); history.push(get_now() + u64::from(timespan));
return history.to_owned()
} }
fn update_history(retry: &mut Retry) { fn update_history(mut history: Vec<u64>) -> Vec<u64> {
let now = get_now(); let now = get_now();
let clear_times: Vec<u64> = retry let clear_times: Vec<u64> = history
.history
.iter() .iter()
.filter(|&time| time <= &now) .filter(|&time| time <= &now)
.map(|&time| time) .map(|&time| time)
.collect(); .collect();
for time in clear_times { for time in clear_times {
retry.history.retain(|&h| h != time); history.retain(|&h| h != time);
}
} }
fn check_history(retry: &Retry) -> bool { return history.to_owned();
return retry.history.len().lt(&usize::from(retry.max_retries)) }
fn check_history(history: &Vec<u64>, max_retries: u8) -> bool {
return history.len().lt(&usize::from(max_retries))
} }
fn spawn_process(retry: &Retry) -> Result<Child, std::io::Error> { fn spawn_process(retry: &Retry) -> Result<Child, std::io::Error> {
@ -101,8 +109,8 @@ fn spawn_process(retry: &Retry) -> Result<Child, std::io::Error> {
} }
} }
fn run_command(retry: &mut Retry) { fn run_command(mut retry: Retry) {
let mut process = spawn_process(retry) let mut process = spawn_process(&retry)
.expect("Process failed on startup"); .expect("Process failed on startup");
let exit_code = match process.wait() { let exit_code = match process.wait() {
@ -117,9 +125,10 @@ fn run_command(retry: &mut Retry) {
println!("Exit code: {}", exit_code); println!("Exit code: {}", exit_code);
} else { } else {
println!("[CRASH] exit code: {}", exit_code); println!("[CRASH] exit code: {}", exit_code);
push_history(retry); let pushed_history = push_history(retry.history, retry.timespan);
update_history(retry); let updated_history = update_history(pushed_history);
if check_history(retry) { if check_history(&updated_history, retry.max_retries) {
retry.history = updated_history;
println!("Restarting..."); println!("Restarting...");
restart(retry); restart(retry);
} else { } else {
@ -128,7 +137,7 @@ fn run_command(retry: &mut Retry) {
} }
} }
fn restart(retry: &mut Retry) { fn restart(retry: Retry) {
sleep(Duration::from_secs(u64::from(retry.restart_delay))); sleep(Duration::from_secs(u64::from(retry.restart_delay)));
run_command(retry); run_command(retry);