Compare commits
No commits in common. "806d17328c2b6dc7bdcaff025a69a9e1ada25c26" and "5c19c27136b8636290bb22274a211c9ecefca2d2" have entirely different histories.
806d17328c
...
5c19c27136
|
@ -123,7 +123,7 @@ checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "kr"
|
name = "kr"
|
||||||
version = "1.0.3"
|
version = "1.0.2"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"clap",
|
"clap",
|
||||||
]
|
]
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "kr"
|
name = "kr"
|
||||||
version = "1.0.3"
|
version = "1.0.2"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
63
src/main.rs
63
src/main.rs
|
@ -35,29 +35,24 @@ struct Retry {
|
||||||
fn main() {
|
fn main() {
|
||||||
let arguments = Arguments::parse();
|
let arguments = Arguments::parse();
|
||||||
|
|
||||||
let mut max_retries = 4;
|
let mut retry: Retry = Retry {
|
||||||
let mut timespan = SECONDS_IN_A_MINUTE;
|
|
||||||
let mut restart_name = String::from("minute");
|
|
||||||
|
|
||||||
if arguments.per_minute > 0 {
|
|
||||||
max_retries = arguments.per_minute;
|
|
||||||
} else if arguments.per_hour > 0 {
|
|
||||||
max_retries = arguments.per_hour;
|
|
||||||
timespan = SECONDS_IN_A_HOUR;
|
|
||||||
restart_name = String::from("hour");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
let retry: Retry = Retry {
|
|
||||||
max_retries,
|
|
||||||
timespan,
|
|
||||||
restart_name,
|
|
||||||
command: arguments.command,
|
command: arguments.command,
|
||||||
history: Vec::new(),
|
history: Vec::new(),
|
||||||
|
max_retries: 4,
|
||||||
|
timespan: SECONDS_IN_A_MINUTE,
|
||||||
restart_delay: arguments.delay,
|
restart_delay: arguments.delay,
|
||||||
|
restart_name: "minute".to_string(),
|
||||||
};
|
};
|
||||||
|
|
||||||
run_command(retry)
|
if arguments.per_minute > 0 {
|
||||||
|
retry.max_retries = arguments.per_minute;
|
||||||
|
} else if arguments.per_hour > 0 {
|
||||||
|
retry.max_retries = arguments.per_hour;
|
||||||
|
retry.timespan = SECONDS_IN_A_HOUR;
|
||||||
|
retry.restart_name = "hour".to_string();
|
||||||
|
}
|
||||||
|
|
||||||
|
run_command(&mut retry)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_now() -> u64 {
|
fn get_now() -> u64 {
|
||||||
|
@ -67,29 +62,26 @@ fn get_now() -> u64 {
|
||||||
.as_secs();
|
.as_secs();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn push_history(mut history: Vec<u64>, timespan: u16) -> Vec<u64> {
|
fn push_history(retry: &mut Retry) {
|
||||||
history.push(get_now() + u64::from(timespan));
|
retry.history.push(get_now() + u64::from(retry.timespan));
|
||||||
|
|
||||||
return history.to_owned()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_history(mut history: Vec<u64>) -> Vec<u64> {
|
fn update_history(retry: &mut Retry) {
|
||||||
let now = get_now();
|
let now = get_now();
|
||||||
let clear_times: Vec<u64> = history
|
let clear_times: Vec<u64> = retry
|
||||||
|
.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 {
|
||||||
history.retain(|&h| h != time);
|
retry.history.retain(|&h| h != time);
|
||||||
}
|
}
|
||||||
|
|
||||||
return history.to_owned();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn check_history(history: &Vec<u64>, max_retries: u8) -> bool {
|
fn check_history(retry: &Retry) -> bool {
|
||||||
return history.len().lt(&usize::from(max_retries))
|
return retry.history.len().lt(&usize::from(retry.max_retries))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn spawn_process(retry: &Retry) -> Result<Child, std::io::Error> {
|
fn spawn_process(retry: &Retry) -> Result<Child, std::io::Error> {
|
||||||
|
@ -109,8 +101,8 @@ fn spawn_process(retry: &Retry) -> Result<Child, std::io::Error> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run_command(mut retry: Retry) {
|
fn run_command(retry: &mut 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() {
|
||||||
|
@ -125,10 +117,9 @@ fn run_command(mut retry: 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);
|
||||||
let pushed_history = push_history(retry.history, retry.timespan);
|
push_history(retry);
|
||||||
let updated_history = update_history(pushed_history);
|
update_history(retry);
|
||||||
if check_history(&updated_history, retry.max_retries) {
|
if check_history(retry) {
|
||||||
retry.history = updated_history;
|
|
||||||
println!("Restarting...");
|
println!("Restarting...");
|
||||||
restart(retry);
|
restart(retry);
|
||||||
} else {
|
} else {
|
||||||
|
@ -137,7 +128,7 @@ fn run_command(mut retry: Retry) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn restart(retry: Retry) {
|
fn restart(retry: &mut 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);
|
||||||
|
|
Loading…
Reference in New Issue