Files
Varaverk/Arrs_Stack/arr_cache_prefill.sh
T
Gmer4Lfe c7ecf99c3f 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.
2026-07-16 15:34:56 -04:00

105 lines
4.2 KiB
Bash
Executable File

#!/bin/bash
# ==============================================================================================
# ================================= Arr Cache Prefill ===========================================
# ==============================================================================================
#
# PURPOSE
# ─────────────────────────────────────────────────────────────────────────────
# Populates the shared tracked-data cache (see arr_get_tracked_data() in common.sh) for
# Lidarr, Sonarr, and Radarr once at array start, before any other script needs it. Without
# this, each arr's cache stays cold from boot until whichever script happens to touch that
# arr first writes through — which could be hours, depending on the daily schedule. One-shot:
# runs and exits. Originally Lidarr-only (lidarr_cache_prefill.sh, 2026-07-16), generalized
# the same day to cover all three arrs once the cache mechanism itself was generalized.
#
# ==============================================================================================
# OPERATIONAL MODEL
# ==============================================================================================
#
# Each arr's container may still be starting when this fires (array just started) — retries
# reaching that arr's API for up to ARR_PREFILL_WAIT_MINUTES before giving up on it and moving
# to the next. Not fatal if one never comes up in time; that arr's cache just stays cold until
# the next script writes through naturally, exactly as it would without this script existing.
# An arr not configured on this host (e.g. Lidarr is HOST1-only) is skipped cleanly.
#
# ==============================================================================================
# CONFIGURATION
# ==============================================================================================
#
# host*.conf
# HOST1_LIDARR_URL / HOST1_LIDARR_API_KEY
# HOST*_SONARR_URL / HOST*_SONARR_API_KEY
# HOST*_RADARR_URL / HOST*_RADARR_API_KEY
# All aliased by detect_hosts()
#
# master.conf
# ARR_PREFILL_WAIT_MINUTES — how long to retry reaching each arr before giving up on it (default 10)
#
# ==============================================================================================
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
source "$SCRIPT_DIR/../load_config.sh"
parse_args "$@"
if [[ "$EUID" -ne 0 ]]; then
error "Must be run as root"
exit 1
fi
if ! command -v jq >/dev/null 2>&1; then
warn "jq not found — skipping arr cache prefill"
exit 0
fi
acquire_lock
detect_hosts
WAIT_MINUTES="${ARR_PREFILL_WAIT_MINUTES:-10}"
# Args: arr_type, url, api_key, api_version
_prefill_one() {
local arr_type="$1" url="$2" api_key="$3" api_version="$4"
if [[ -z "$url" ]] || [[ -z "$api_key" ]]; then
info "${arr_type^} not configured on $MY_ID ($LOCAL_SERVER_NAME) — nothing to prefill"
return 0
fi
local waited=0
until check_api "$url" "${arr_type^}" 5 >/dev/null 2>&1; do
if [[ "$waited" -ge $(( WAIT_MINUTES * 60 )) ]]; then
warn "${arr_type^} not reachable after ${WAIT_MINUTES}m — leaving cache cold, next script will write through"
return 0
fi
sleep 15
(( waited += 15 ))
done
local endpoint="${ARR_LIBRARY_ENDPOINT[$arr_type]:-}"
if [[ -z "$endpoint" ]]; then
warn "No library endpoint known for ${arr_type} — skipping"
return 0
fi
local items
items=$(arr_api "$url" "$api_key" "$api_version" "$endpoint" "${arr_type^}") || {
warn "Could not fetch ${arr_type} library for cache prefill — leaving cache cold"
return 0
}
if arr_cache_write "$arr_type" "$items"; then
log "$ICON_DONE ${arr_type^} cache prefilled ($(echo "$items" | jq 'length') items)"
else
warn "Failed to write ${arr_type} cache prefill"
fi
}
_prefill_one "lidarr" "${LIDARR_URL:-}" "${LIDARR_API_KEY:-}" "v1"
_prefill_one "sonarr" "${SONARR_URL:-}" "${SONARR_API_KEY:-}" "v3"
_prefill_one "radarr" "${RADARR_URL:-}" "${RADARR_API_KEY:-}" "v3"
exit 0