Generalize tracked-data cache from Lidarr-only to all three arrs

Shared cache/rescan-duration logic in common.sh now takes an arr_type
param instead of being Lidarr-specific, so Sonarr and Radarr cleanup
scripts get the same cache-first fetch + rescan-aware retry Lidarr had.
Avoids redundant full-library API calls across scripts run back to back,
and stops false failures when a fetch lands mid-rescan.
This commit is contained in:
Gmer4Lfe
2026-07-16 15:34:56 -04:00
parent 62bb166f9a
commit c7ecf99c3f
10 changed files with 430 additions and 236 deletions
+11 -9
View File
@@ -15,7 +15,7 @@
# 22% of normal during an active RescanFolders), which used to trigger this script's own
# hard abort every time it overlapped with a real rescan. Now it checks for an active
# rescan-type command first — if one's running, it waits (calibrated to that command's
# own historical duration via lidarr_get_rescan_duration(), up to 3 strikes) and re-fetches
# own historical duration via arr_get_rescan_duration(), up to 3 strikes) and re-fetches
# rather than either stacking a duplicate scan or crying wolf on a normal, if slow, state.
# Only escalates to the scary abort-and-notify when the count is genuinely low AND nothing
# is actively rescanning.
@@ -260,14 +260,14 @@ unset _cp
# repeated runs each firing their own DownloadedAlbumsScan piled up in Lidarr's command
# queue behind each other rather than replacing/coalescing, contributing to a multi-hour
# backlog. If something's already scanning, just wait for that one instead.
_already_active=$(lidarr_active_rescan_command "$LIDARR_URL" "$LIDARR_API_KEY" "v1")
_already_active=$(arr_active_rescan_command "lidarr" "$LIDARR_URL" "$LIDARR_API_KEY" "v1")
if [[ -n "$_already_active" ]]; then
info "$_already_active already in progress — waiting for it instead of starting a new scan"
_wait=$(( $(lidarr_get_rescan_duration "$_already_active" "${LIDARR_IMPORT_SCAN_TIMEOUT:-600}") ))
_wait=$(( $(arr_get_rescan_duration "lidarr" "$_already_active" "${LIDARR_IMPORT_SCAN_TIMEOUT:-600}") ))
_polled=0
while [[ "$_polled" -lt "$_wait" ]]; do
[[ -z "$(lidarr_active_rescan_command "$LIDARR_URL" "$LIDARR_API_KEY" "v1")" ]] && break
[[ -z "$(arr_active_rescan_command "lidarr" "$LIDARR_URL" "$LIDARR_API_KEY" "v1")" ]] && break
sleep 15
(( _polled += 15 ))
[[ $(( _polled % 60 )) -eq 0 ]] && log " Still waiting on $_already_active... (${_polled}s elapsed)"
@@ -275,11 +275,11 @@ if [[ -n "$_already_active" ]]; then
elif [[ -n "$LIDARR_CONTAINER_ROOT" ]]; then
info "Triggering DownloadedAlbumsScan on: $LIDARR_CONTAINER_ROOT"
SCAN_PAYLOAD="{\"name\": \"DownloadedAlbumsScan\", \"path\": \"$LIDARR_CONTAINER_ROOT\"}"
trigger_and_await_command "$LIDARR_URL" "$LIDARR_API_KEY" "v1" "$SCAN_PAYLOAD" "${LIDARR_IMPORT_SCAN_TIMEOUT:-600}"
trigger_and_await_command "$LIDARR_URL" "$LIDARR_API_KEY" "v1" "$SCAN_PAYLOAD" "${LIDARR_IMPORT_SCAN_TIMEOUT:-600}" "lidarr"
else
info "No path map match — triggering DownloadedAlbumsScan (all root folders)"
SCAN_PAYLOAD='{"name": "DownloadedAlbumsScan"}'
trigger_and_await_command "$LIDARR_URL" "$LIDARR_API_KEY" "v1" "$SCAN_PAYLOAD" "${LIDARR_IMPORT_SCAN_TIMEOUT:-600}"
trigger_and_await_command "$LIDARR_URL" "$LIDARR_API_KEY" "v1" "$SCAN_PAYLOAD" "${LIDARR_IMPORT_SCAN_TIMEOUT:-600}" "lidarr"
fi
# ==============================================================================================
@@ -306,6 +306,8 @@ ARTIST_RESPONSE=$(arr_api "$LIDARR_URL" "$LIDARR_API_KEY" "v1" "artist" "Lidarr"
"Lidarr Cleanup" "warning"
exit 1
}
# Write-through — free cache refresh from a fetch this script already needed.
arr_cache_write "lidarr" "$ARTIST_RESPONSE"
ARTIST_IDS=$(echo "$ARTIST_RESPONSE" | jq -r '.[].id' 2>/dev/null)
ARTIST_COUNT=$(echo "$ARTIST_IDS" | grep -c "[0-9]" 2>/dev/null || echo 0)
@@ -379,10 +381,10 @@ if [[ "$_last_known" -gt 0 ]]; then
_pct=$(awk "BEGIN {printf \"%d\", ($TRACKED_COUNT / $_last_known) * 100}")
[[ "$_pct" -ge "${LIDARR_MIN_TRACKED_PCT:-50}" ]] && break
_active_cmd=$(lidarr_active_rescan_command "$LIDARR_URL" "$LIDARR_API_KEY" "v1")
_active_cmd=$(arr_active_rescan_command "lidarr" "$LIDARR_URL" "$LIDARR_API_KEY" "v1")
[[ -z "$_active_cmd" ]] && break # low count, nothing rescanning — genuine, don't retry
_wait=$(( $(lidarr_get_rescan_duration "$_active_cmd" 300) / 2 ))
_wait=$(( $(arr_get_rescan_duration "lidarr" "$_active_cmd" 300) / 2 ))
[[ "$_wait" -lt 30 ]] && _wait=30
warn "Tracked count ${_pct}% of last run, but $_active_cmd active — waiting ${_wait}s (strike ${_strike}/3)"
sleep "$_wait"
@@ -391,7 +393,7 @@ if [[ "$_last_known" -gt 0 ]]; then
done
if [[ "$_strike" -gt 3 ]]; then
_active_cmd=$(lidarr_active_rescan_command "$LIDARR_URL" "$LIDARR_API_KEY" "v1")
_active_cmd=$(arr_active_rescan_command "lidarr" "$LIDARR_URL" "$LIDARR_API_KEY" "v1")
if [[ -n "$_active_cmd" ]]; then
warn "Lidarr still busy ($_active_cmd) after 3 strikes — deferring to next scheduled run"
exit 0