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
+76 -25
View File
@@ -10,6 +10,14 @@
# untracked that is old enough to be past the import window. Triggers an Emby
# library clean after each deletion run so ghost entries disappear immediately.
#
# The tracked-count floor check (Safety Layer 6) is rescan-aware: if Radarr's own
# RescanMovie/DownloadedMoviesScan is active (independently of this script's own
# lighter ProcessMonitoredDownloads pre-flight), a genuinely low mid-scan count gets
# waited out (calibrated to that command's historical duration via
# arr_get_rescan_duration(), up to 3 strikes) and re-fetched rather than triggering a
# false-alarm abort. Mirrors the same fix built for lidarr_cleanup.sh 2026-07-16 after
# a whole-library rescan there made trackFileCount read 22% of normal mid-scan.
#
# ==============================================================================================
# OPERATIONAL MODEL
# ==============================================================================================
@@ -251,7 +259,7 @@ info "Scan targets (${#SCAN_ROOTS[@]}): ${SCAN_ROOTS[*]}"
info "Triggering ProcessMonitoredDownloads pre-flight"
SCAN_PAYLOAD='{"name": "ProcessMonitoredDownloads"}'
trigger_and_await_command "$RADARR_URL" "$RADARR_API_KEY" "v3" "$SCAN_PAYLOAD" "${RADARR_IMPORT_SCAN_TIMEOUT:-600}"
trigger_and_await_command "$RADARR_URL" "$RADARR_API_KEY" "v3" "$SCAN_PAYLOAD" "${RADARR_IMPORT_SCAN_TIMEOUT:-600}" "radarr"
# ==============================================================================================
# ━━━ Fetch Radarr Tracked Files ━━━
@@ -277,6 +285,8 @@ MOVIES_RESPONSE=$(arr_api "$RADARR_URL" "$RADARR_API_KEY" "v3" "movie" "Radarr")
"Radarr Cleanup" "warning"
exit 1
}
# Write-through — free cache refresh from a fetch this script already needed.
arr_cache_write "radarr" "$MOVIES_RESPONSE"
MOVIE_IDS=$(echo "$MOVIES_RESPONSE" | jq -r '.[].id' 2>/dev/null)
MOVIE_COUNT=$(echo "$MOVIE_IDS" | grep -c "." 2>/dev/null || echo 0)
@@ -294,32 +304,41 @@ info "Found $MOVIE_COUNT movies — fetching movie files..."
TRACKED_FILE="$TMP_DIR/tracked_paths.txt"
> "$TRACKED_FILE"
MOVIE_INDEX=0
while IFS= read -r movie_id; do
[[ -z "$movie_id" ]] && continue
(( MOVIE_INDEX++ ))
[[ $(( MOVIE_INDEX % 100 )) -eq 0 ]] && \
log "Fetching files: $MOVIE_INDEX/$MOVIE_COUNT movies..."
MOVIE_FILES=$(arr_api "$RADARR_URL" "$RADARR_API_KEY" "v3" "moviefile?movieId=${movie_id}" "Radarr" 2>/dev/null)
if [[ -n "$MOVIE_FILES" ]]; then
while IFS= read -r api_path; do
[[ -z "$api_path" ]] && continue
translate_path "$api_path" >> "$TRACKED_FILE"
done < <(echo "$MOVIE_FILES" | jq -r '.[].path // .path' 2>/dev/null)
fi
done <<< "$MOVIE_IDS"
# Fetches every movie's file path fresh into TRACKED_FILE/TRACKED_MAP/TRACKED_COUNT. Pulled
# into a function so the rescan-aware retry below can re-fetch after waiting without
# duplicating this whole loop inline.
_fetch_tracked_files() {
> "$TRACKED_FILE"
local _movie_index=0
while IFS= read -r movie_id; do
[[ -z "$movie_id" ]] && continue
(( _movie_index++ ))
[[ $(( _movie_index % 100 )) -eq 0 ]] && \
log "Fetching files: $_movie_index/$MOVIE_COUNT movies..."
MOVIE_FILES=$(arr_api "$RADARR_URL" "$RADARR_API_KEY" "v3" "moviefile?movieId=${movie_id}" "Radarr" 2>/dev/null)
if [[ -n "$MOVIE_FILES" ]]; then
while IFS= read -r api_path; do
[[ -z "$api_path" ]] && continue
translate_path "$api_path" >> "$TRACKED_FILE"
done < <(echo "$MOVIE_FILES" | jq -r '.[].path // .path' 2>/dev/null)
fi
done <<< "$MOVIE_IDS"
sort -u "$TRACKED_FILE" -o "$TRACKED_FILE"
sort -u "$TRACKED_FILE" -o "$TRACKED_FILE"
# Build in-memory lookup map — O(1) per lookup vs O(n) grep per file
# Eliminates the main performance bottleneck for large libraries
declare -A TRACKED_MAP
while IFS= read -r _tracked_path; do
[[ -n "$_tracked_path" ]] && TRACKED_MAP["$_tracked_path"]=1
done < "$TRACKED_FILE"
unset _tracked_path
# Build in-memory lookup map — O(1) per lookup vs O(n) grep per file
# Eliminates the main performance bottleneck for large libraries
unset TRACKED_MAP
declare -gA TRACKED_MAP
while IFS= read -r _tracked_path; do
[[ -n "$_tracked_path" ]] && TRACKED_MAP["$_tracked_path"]=1
done < "$TRACKED_FILE"
unset _tracked_path
TRACKED_COUNT=$(wc -l < "$TRACKED_FILE")
}
_fetch_tracked_files
info "Built in-memory lookup map: ${#TRACKED_MAP[@]} tracked paths"
TRACKED_COUNT=$(wc -l < "$TRACKED_FILE")
# Safety Layer 5 — tracked count > 0
if [[ "$TRACKED_COUNT" -eq 0 ]]; then
@@ -331,7 +350,39 @@ fi
info "$MOVIE_COUNT movies | $TRACKED_COUNT tracked movie files"
# Safety Layer 6 — percentage drop vs last known count
# Safety Layer 6 — percentage drop vs last known count, with rescan-aware retry.
# ProcessMonitoredDownloads (this script's own pre-flight) is a different, lighter operation
# than a full library rescan — but Radarr's own RescanMovie/DownloadedMoviesScan can be
# triggered independently and would cause the exact same mid-scan count dip confirmed on
# Lidarr 2026-07-16. Wait it out (calibrated to that command's own historical duration)
# before treating a drop as genuine.
_last_known=$(cat "$RADARR_TRACKED_COUNT_FILE" 2>/dev/null || echo 0)
if [[ "$_last_known" -gt 0 ]]; then
_strike=1
while [[ "$_strike" -le 3 ]]; do
_pct=$(awk "BEGIN {printf \"%d\", ($TRACKED_COUNT / $_last_known) * 100}")
[[ "$_pct" -ge "${RADARR_MIN_TRACKED_PCT:-50}" ]] && break
_active_cmd=$(arr_active_rescan_command "radarr" "$RADARR_URL" "$RADARR_API_KEY" "v3")
[[ -z "$_active_cmd" ]] && break # low count, nothing rescanning — genuine, don't retry
_wait=$(( $(arr_get_rescan_duration "radarr" "$_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"
_fetch_tracked_files
(( _strike++ ))
done
if [[ "$_strike" -gt 3 ]]; then
_active_cmd=$(arr_active_rescan_command "radarr" "$RADARR_URL" "$RADARR_API_KEY" "v3")
if [[ -n "$_active_cmd" ]]; then
warn "Radarr still busy ($_active_cmd) after 3 strikes — deferring to next scheduled run"
exit 0
fi
fi
fi
check_tracked_count_floor "$TRACKED_COUNT" "$RADARR_TRACKED_COUNT_FILE" "$RADARR_MIN_TRACKED_PCT" "Radarr Cleanup"
# ==============================================================================================