Last active
August 20, 2022 08:00
-
-
Save nathants/7b38efe827fc12fd7a5b9a5401ddd242 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# The MIT License (MIT) | |
# Copyright (c) 2022-present Nathan Todd-Stone | |
# https://en.wikipedia.org/wiki/MIT_License#License_terms | |
# define the retry fn | |
retry() { | |
e=0 | |
if [[ $- =~ e ]]; then | |
e=1 | |
fi | |
set +e | |
max_tries=10 | |
sleep_seconds=3 | |
for i in $(seq 1 $max_tries); do | |
("$@") | |
if [ $? = 0 ]; then | |
break | |
else | |
echo retrying: "$@" 1>&2 | |
sleep $sleep_seconds 1>&2 | |
fi | |
if [ $i = $max_tries ]; then | |
echo all retries failed 1>&2 | |
exit 1 | |
fi | |
done | |
if [ e = 1 ]; then | |
set -e | |
fi | |
} | |
# export it so its available in subprocesses | |
export -f retry | |
fn() { | |
set -eou pipefail | |
# false # to trigger retries | |
echo process $1 | |
} | |
# export it so its available in subprocesses | |
export -f fn | |
inputs=" | |
a | |
b | |
c | |
" | |
# run the fn in parallel over the inputs, retrying failures | |
echo "$inputs" | xargs -n1 -P$(nproc) -I{} bash -c "retry fn {}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment