From 15779e4ee0f49b780932726ef3e4e7d913d8f1d3 Mon Sep 17 00:00:00 2001 From: "CanbiZ (MickLesk)" <47820557+MickLesk@users.noreply.github.com> Date: Fri, 13 Feb 2026 13:45:31 +0100 Subject: [PATCH] Retry reporting with fallback payloads Enhance post_update_to_api to support a "force" mode and robust retry logic: add a 3rd-arg bypass to duplicate suppression, capture a short error summary, and perform up to three POST attempts (full payload, shortened error payload, minimal payload) with HTTP code checks and small backoffs. Mark POST_UPDATE_DONE on success (or after three attempts) to avoid infinite retries. Also invoke post_update_to_api with the "force" flag from cleanup paths in build.func and error_handler.func so a final status update is attempted after cleanup. --- misc/api.func | 87 ++++++++++++++++++++++++++++++++++++++--- misc/build.func | 4 ++ misc/error_handler.func | 6 +++ 3 files changed, 91 insertions(+), 6 deletions(-) diff --git a/misc/api.func b/misc/api.func index 51e4788bc..baf2ffa73 100644 --- a/misc/api.func +++ b/misc/api.func @@ -592,9 +592,12 @@ post_update_to_api() { # Silent fail - telemetry should never break scripts command -v curl &>/dev/null || return 0 - # Prevent duplicate submissions + # Support "force" mode (3rd arg) to bypass duplicate check for retries after cleanup + local force="${3:-}" POST_UPDATE_DONE=${POST_UPDATE_DONE:-false} - [[ "$POST_UPDATE_DONE" == "true" ]] && return 0 + if [[ "$POST_UPDATE_DONE" == "true" && "$force" != "force" ]]; then + return 0 + fi [[ "${DIAGNOSTICS:-no}" == "no" ]] && return 0 [[ -z "${RANDOM_UUID:-}" ]] && return 0 @@ -632,6 +635,7 @@ post_update_to_api() { esac # For failed/unknown status, resolve exit code and error description + local short_error="" if [[ "$pb_status" == "failed" ]] || [[ "$pb_status" == "unknown" ]]; then if [[ "$raw_exit_code" =~ ^[0-9]+$ ]]; then exit_code="$raw_exit_code" @@ -645,6 +649,7 @@ post_update_to_api() { else error=$(json_escape "$(explain_exit_code "$exit_code")") fi + short_error=$(json_escape "$(explain_exit_code "$exit_code")") error_category=$(categorize_error "$exit_code") [[ -z "$error" ]] && error="Unknown error" fi @@ -661,8 +666,9 @@ post_update_to_api() { pve_version=$(pveversion 2>/dev/null | awk -F'[/ ]' '{print $2}') || true fi - # Full payload including all fields - allows record creation if initial call failed - # The Go service will find the record by random_id and PATCH, or create if not found + local http_code="" + + # ── Attempt 1: Full payload with complete error text ── local JSON_PAYLOAD JSON_PAYLOAD=$( cat </dev/null) || http_code="000" + + if [[ "$http_code" =~ ^2[0-9]{2}$ ]]; then + POST_UPDATE_DONE=true + return 0 + fi + + # ── Attempt 2: Short error text (no full log) ── + sleep 1 + local RETRY_PAYLOAD + RETRY_PAYLOAD=$( + cat </dev/null) || http_code="000" + + if [[ "$http_code" =~ ^2[0-9]{2}$ ]]; then + POST_UPDATE_DONE=true + return 0 + fi + + # ── Attempt 3: Minimal payload (bare minimum to set status) ── + sleep 2 + local MINIMAL_PAYLOAD + MINIMAL_PAYLOAD=$( + cat <&1 || true + -d "$MINIMAL_PAYLOAD" -o /dev/null 2>/dev/null || true + # Tried 3 times - mark as done regardless to prevent infinite loops POST_UPDATE_DONE=true } diff --git a/misc/build.func b/misc/build.func index 74d73ff20..b96e96f24 100644 --- a/misc/build.func +++ b/misc/build.func @@ -4130,6 +4130,10 @@ EOF' echo -e "${BFR}${CM}${GN}Container ${CTID} removed${CL}" fi + # Force one final status update attempt after cleanup + # This ensures status is updated even if the first attempt failed (e.g., HTTP 400) + post_update_to_api "failed" "$install_exit_code" "force" + exit $install_exit_code fi } diff --git a/misc/error_handler.func b/misc/error_handler.func index 2dea84742..afedc696f 100644 --- a/misc/error_handler.func +++ b/misc/error_handler.func @@ -222,6 +222,12 @@ error_handler() { pct destroy "$CTID" &>/dev/null || true echo -e "${GN}✔${CL} Container ${CTID} removed" fi + + # Force one final status update attempt after cleanup + # This ensures status is updated even if the first attempt failed (e.g., HTTP 400) + if declare -f post_update_to_api &>/dev/null; then + post_update_to_api "failed" "$exit_code" "force" + fi fi fi fi