diff --git a/Arrs_Stack/arrs_failed_stalled_recovery.sh b/Arrs_Stack/arrs_failed_stalled_recovery.sh index 9d2aeb9..0d7dc58 100755 --- a/Arrs_Stack/arrs_failed_stalled_recovery.sh +++ b/Arrs_Stack/arrs_failed_stalled_recovery.sh @@ -212,14 +212,41 @@ item_is_old_enough() { [[ "$age_seconds" -ge "$AGE_THRESHOLD_SECONDS" ]] } -# Query the arr queue API and return all records +# Query the arr queue API and return all records, paginated. +# A single page=1&pageSize=200 request silently misses everything past record 200 — +# on a busy Sonarr instance the queue can run into the thousands (e.g. a large +# missing-episode search campaign), which pushed every importBlocked/warning item +# past page 1 and made this whole script blind to them despite matching correctly. # Args: url, api_key, api_version get_queue_data() { local url="$1" api_key="$2" api_version="$3" - curl -sf --max-time 15 \ - -H "X-Api-Key: $api_key" \ - "${url}/api/${api_version}/queue?page=1&pageSize=200&includeUnknownSeriesItems=true&includeUnknownArtistItems=true" \ - 2>/dev/null + local page=1 page_size=250 max_pages=50 + local page_data page_count + # Accumulate pages as files rather than growing a shell variable — on a large + # queue (thousands of records) passing the combined JSON through --argjson + # blows past ARG_MAX ("Argument list too long"). jq -s reads files instead. + local tmp_dir + tmp_dir=$(mktemp -d) + trap 'rm -rf "$tmp_dir"' RETURN + + while [[ "$page" -le "$max_pages" ]]; do + page_data=$(curl -sf --max-time 15 \ + -H "X-Api-Key: $api_key" \ + "${url}/api/${api_version}/queue?page=${page}&pageSize=${page_size}&includeUnknownSeriesItems=true&includeUnknownArtistItems=true" \ + 2>/dev/null) + [[ -z "$page_data" ]] && break + + page_count=$(echo "$page_data" | jq '.records // [] | length' 2>/dev/null) + [[ -z "$page_count" || "$page_count" -eq 0 ]] && break + + echo "$page_data" | jq -c '.records // []' > "$tmp_dir/page_${page}.json" + + [[ "$page_count" -lt "$page_size" ]] && break + (( page++ )) + done + + jq -c -s '{totalRecords: ([.[][]] | length), records: [.[][]]}' "$tmp_dir"/page_*.json 2>/dev/null \ + || echo '{"totalRecords":0,"records":[]}' } # Blocklist and remove a queue item