Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ humantime = "2.1"
libc = "0.2"
signal-hook = "0.3"
signal-hook-tokio = { version = "0.3", features = ["futures-v0_3"] }
tokio = { version = "1", features = ["macros", "process", "rt-multi-thread"] }
tokio = { version = "1", features = ["macros", "process", "rt-multi-thread", "time"] }
15 changes: 12 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ use clap::Parser;
use futures::stream::StreamExt;
use signal_hook::consts::signal;
use signal_hook_tokio::Signals;
use std::thread::sleep;
use std::time::Duration;
use tokio::process::Command;
use tokio::{process::Command, time::sleep};

/// A utility for retrying failed console commands
#[derive(Parser)]
Expand Down Expand Up @@ -111,7 +110,17 @@ async fn run(args: Args) -> i32 {
num_attempts,
humantime::Duration::from(delay)
));
sleep(delay);

let backoff_sleep = sleep(delay);
tokio::pin!(backoff_sleep);

tokio::select! {
Some(_signal) = signals.next() => {
return last_exit_code;
}
_ = &mut backoff_sleep => {}
}

delay *= args.delay_multiplier;
}
}
Expand Down
32 changes: 32 additions & 0 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,38 @@ assertEqual \
"child received TERM signal" \
"$(tail -n 1 ./output)"

#
# Given: The child exit code is non-zero,
# and we are sleeping before the next attempt
# When: A stop signal is received
# Then: The sleep is interrupted
#
givenScript "exit 1"
${binPath} --attempts 2 --delay 5s -- ./script >output 2>&1 &
processId=$!

# Wait until we enter sleep by detecting the 'retrying in' output
for i in $(seq 1 100); do
if grep -q 'retrying in' ./output; then
break
fi
sleep 0.05
done

beforeTs=$(date +%s)
kill -s TERM ${processId}
wait ${processId} 2>/dev/null
afterTs=$(date +%s)
elapsed=$((afterTs-beforeTs))

interrupted=false
[ ${elapsed} -lt 2 ] && interrupted=true

assertEqual \
"Sleep between attempts is interrupted when a stop signal is received" \
"true" \
"${interrupted}"

rm ./script ./output >/dev/null 2>&1

[ ${failCount} = 0 ] && exit 0 || exit 1
Loading