Skip to content

Commit 7e9f550

Browse files
authored
[CI] Add retries to avoid failing because of flakey network issues (#281)
* [CI] Add retries to avoid failing because of flakey network issues * Print each retry error if all retries failed
1 parent 992b0cb commit 7e9f550

File tree

1 file changed

+71
-9
lines changed

1 file changed

+71
-9
lines changed

scripts/run_examples.sh

Lines changed: 71 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,34 @@
11
#!/bin/bash
22

3+
# Default retry count
4+
RETRY_COUNT=3
5+
6+
# Parse command line arguments
7+
while [[ $# -gt 0 ]]; do
8+
case $1 in
9+
--no-extra-dep)
10+
NO_EXTRA_DEP=true
11+
shift
12+
;;
13+
--retry-count)
14+
RETRY_COUNT="$2"
15+
shift 2
16+
;;
17+
--help)
18+
echo "Usage: $0 [--no-extra-dep] [--retry-count N]"
19+
echo " --no-extra-dep: Exclude files that require extra dependencies"
20+
echo " --retry-count N: Number of retries for each test (default: 3)"
21+
echo " --help: Show this help message"
22+
exit 0
23+
;;
24+
*)
25+
echo "Unknown option: $1"
26+
echo "Use --help for usage information"
27+
exit 1
28+
;;
29+
esac
30+
done
31+
332
# List of files to exclude
433
exclude_files=(
534
"examples/mistral/chat/chatbot_with_streaming.py"
@@ -13,8 +42,8 @@ exclude_files=(
1342
"examples/mistral/agents/async_conversation_run_mcp_remote.py"
1443
)
1544

16-
# Check if the first argument is "no-extra-dep" then remove all the files that require the extra dependencies
17-
if [ "$1" = "--no-extra-dep" ]; then
45+
# Check if the no-extra-dep flag is set
46+
if [ "$NO_EXTRA_DEP" = true ]; then
1847
# Add more files to the exclude list
1948
exclude_files+=(
2049
"examples/mistral/agents/async_conversation_run_mcp_remote.py"
@@ -25,19 +54,52 @@ fi
2554

2655
failed=0
2756

57+
# Function to run a test with retries
58+
run_test_with_retries() {
59+
local file="$1"
60+
local attempt=1
61+
local error_outputs=()
62+
63+
while [ $attempt -le $RETRY_COUNT ]; do
64+
echo "Running $file (attempt $attempt/$RETRY_COUNT)"
65+
66+
# Run the script and capture both exit status and error output
67+
local current_output=$(python3 "$file" 2>&1)
68+
local exit_code=$?
69+
70+
if [ $exit_code -eq 0 ]; then
71+
echo "Success"
72+
return 0
73+
else
74+
# Store the error output from this attempt
75+
error_outputs+=("Attempt $attempt: $current_output")
76+
77+
if [ $attempt -lt $RETRY_COUNT ]; then
78+
echo "Failed (attempt $attempt/$RETRY_COUNT), retrying..."
79+
sleep 1 # Brief pause before retry
80+
else
81+
echo "Failed after $RETRY_COUNT attempts"
82+
echo "Error outputs from all attempts:"
83+
for error_output in "${error_outputs[@]}"; do
84+
echo "$error_output"
85+
echo "---"
86+
done
87+
return 1
88+
fi
89+
fi
90+
91+
attempt=$((attempt + 1))
92+
done
93+
}
94+
2895
for file in examples/mistral/**/*.py; do
2996
# Check if the file is not in the exclude list
3097
if [ -f "$file" ] && [[ ! " ${exclude_files[@]} " =~ " $file " ]]; then
31-
echo "Running $file"
32-
# Run the script and capture the exit status
33-
if python3 "$file" > /dev/null; then
34-
echo "Success"
35-
else
36-
echo "Failed"
98+
if ! run_test_with_retries "$file"; then
3799
failed=1
38100
fi
39101
else
40-
echo "Skipped $file"
102+
echo "Skipped $file"
41103
fi
42104
done
43105

0 commit comments

Comments
 (0)