This commit is contained in:
CanbiZ 2025-05-08 14:06:17 +02:00
parent 12796db067
commit a7ef32b29d
2 changed files with 27 additions and 18 deletions

View File

@ -124,9 +124,14 @@ run_curl() {
curl_handler() { curl_handler() {
local args=() local args=()
local url="" local url=""
local max_retries=0 delay=2 attempt=1 local max_retries=3 # Setting a default value
local exit_code has_output_file=false local delay=2
local attempt=1
local exit_code
local has_output_file=false
local result=""
# Parse arguments
for arg in "$@"; do for arg in "$@"; do
if [[ "$arg" != -* && -z "$url" ]]; then if [[ "$arg" != -* && -z "$url" ]]; then
url="$arg" url="$arg"
@ -136,40 +141,39 @@ curl_handler() {
done done
if [[ -z "$url" ]]; then if [[ -z "$url" ]]; then
msg_error "no valid url or option entered for curl_handler" msg_error "No valid URL or option entered for curl_handler"
exit 1 return 1
fi fi
$STD msg_info "Fetching: $url" msg_info "Fetching: $url"
while :; do while [[ $attempt -le $max_retries ]]; do
if $has_output_file; then if $has_output_file; then
$STD run_curl "${args[@]}" run_curl "${args[@]}"
exit_code=$? exit_code=$?
else else
$STD result=$(run_curl "${args[@]}") result=$(run_curl "${args[@]}")
exit_code=$? exit_code=$?
fi fi
if [[ $exit_code -eq 0 ]]; then if [[ $exit_code -eq 0 ]]; then
stop_spinner
msg_ok "Fetched: $url" msg_ok "Fetched: $url"
$has_output_file || printf '%s' "$result" $has_output_file || printf '%s' "$result"
return 0 return 0
fi fi
if ((attempt >= max_retries)); then if ((attempt >= max_retries)); then
stop_spinner # Read error log if it exists
if [ -s /tmp/curl_error.log ]; then if [ -s /tmp/curl_error.log ]; then
local curl_stderr local curl_stderr
curl_stderr=$(</tmp/curl_error.log) curl_stderr=$(</tmp/curl_error.log)
rm -f /tmp/curl_error.log rm -f /tmp/curl_error.log
fi fi
__curl_err_handler "$exit_code" "$url" "$curl_stderr" __curl_err_handler "$exit_code" "$url" "${curl_stderr:-}"
exit 1 # hard exit if exit_code is not 0 return 1 # Return error instead of exit to allow script to continue
fi fi
$STD printf "\r\033[K${INFO}${YW}Retry $attempt/$max_retries in ${delay}s...${CL}" >&2 printf "\r\033[K${INFO}${YW}Retry $attempt/$max_retries in ${delay}s...${CL}" >&2
sleep "$delay" sleep "$delay"
((attempt++)) ((attempt++))
done done

View File

@ -334,16 +334,21 @@ get_valid_nextid() {
} }
cleanup_vmid() { cleanup_vmid() {
if qm status $VMID &>/dev/null; then if [[ -z "${VMID:-}" ]]; then
qm stop $VMID &>/dev/null return
qm destroy $VMID &>/dev/null fi
if qm status "$VMID" &>/dev/null; then
qm stop "$VMID" &>/dev/null
qm destroy "$VMID" &>/dev/null
fi fi
} }
cleanup() { cleanup() {
popd >/dev/null if [[ "$(dirs -p | wc -l)" -gt 1 ]]; then
popd >/dev/null || true
fi
post_update_to_api "done" "none" post_update_to_api "done" "none"
rm -rf $TEMP_DIR [[ -n "${TEMP_DIR:-}" && -d "$TEMP_DIR" ]] && rm -rf "$TEMP_DIR"
} }
check_root() { check_root() {