movetdarr v5: lease lock on local filesystem #4

Merged
sascha merged 3 commits from fix/movetdarr-nfs-lock into main 2026-09-07 11:38:42 +02:00
Showing only changes of commit 32d68201fd - Show all commits

View file

@ -1,9 +1,9 @@
#!/bin/bash #!/bin/bash
# movetdarr.sh v4 — SABnzbd -> FileFlows with durable n8n handoff lease # movetdarr.sh v5 — SABnzbd -> FileFlows with durable n8n handoff lease
# Categories: serien4k->Sonarr UHD, serien->Sonarr FHD, # Categories: serien4k->Sonarr UHD, serien->Sonarr FHD,
# video4k->Radarr UHD, video->Radarr FHD. # video4k->Radarr UHD, video->Radarr FHD.
# v4: flock-Serialisierung des Lease-Blocks gegen n8n-staticData-Race # v5: BusyBox-kompatibler FD-Lock auf lokalem /config-Dateisystem.
# bei parallelen SAB-Aufträgen (Last-Writer-Wins verschluckt Jobs). # Der alte Lock unter /usenet lag auf NFS und konnte dauerhaft fehlschlagen.
DIR="$1" DIR="$1"
NZB_NAME="$2" NZB_NAME="$2"
@ -17,7 +17,7 @@ SOURCE_ROOT="${SOURCE_ROOT:-/usenet/complete}"
DEST_ROOT="${DEST_ROOT:-/tdarr/complete}" DEST_ROOT="${DEST_ROOT:-/tdarr/complete}"
POLL_SECONDS="${POLL_SECONDS:-60}" POLL_SECONDS="${POLL_SECONDS:-60}"
MAX_POLLS="${MAX_POLLS:-1440}" # 24 hours MAX_POLLS="${MAX_POLLS:-1440}" # 24 hours
LEASE_LOCK="${LEASE_LOCK:-/usenet/scripts/.movetdarr.lease.lock}" LEASE_LOCK="${LEASE_LOCK:-/config/.movetdarr.lease.lock}"
log() { echo "$(date '+%d.%m.%Y %H:%M:%S'): $1" >> "$LOGFILE"; } log() { echo "$(date '+%d.%m.%Y %H:%M:%S'): $1" >> "$LOGFILE"; }
@ -42,7 +42,7 @@ notify_failure() {
handoff "$payload" >/dev/null 2>&1 || true handoff "$payload" >/dev/null 2>&1 || true
} }
log "Start v4 - Kategorie=$CATEGORY Status=$STATUS Dir=$DIR" log "Start v5 - Kategorie=$CATEGORY Status=$STATUS Dir=$DIR"
if [ "$STATUS" != "0" ]; then if [ "$STATUS" != "0" ]; then
log "Download fehlgeschlagen, ueberspringe" log "Download fehlgeschlagen, ueberspringe"
@ -145,21 +145,32 @@ register_lease() {
} }
if command -v flock >/dev/null 2>&1; then if command -v flock >/dev/null 2>&1; then
# BusyBox flock hat kein -w (timeout) → -n (non-blocking) mit Retry-Loop # BusyBox flock hat kein -w: FD öffnen und -n mit Retry-Loop verwenden.
# Der FD bleibt bis nach start -> mv -> moved offen und wird vor dem
# lang laufenden Status-Polling ausdrücklich entsperrt.
exec 9>"$LEASE_LOCK" || {
log "FEHLER: Lease-Lock-Datei konnte nicht geoeffnet werden - kein Move"
exit 1
}
LEASE_ACQUIRED=false LEASE_ACQUIRED=false
for i in $(seq 1 900); do for i in $(seq 1 900); do
if flock -n "$LEASE_LOCK" -c true 2>/dev/null; then if flock -n 9 2>/dev/null; then
LEASE_ACQUIRED=true LEASE_ACQUIRED=true
break break
fi fi
sleep 1 sleep 1
done done
if [ "$LEASE_ACQUIRED" != "true" ]; then if [ "$LEASE_ACQUIRED" != "true" ]; then
log "WARNUNG: Lease-Lock nach 15 Minuten nicht erhalten; fahre ohne Lock fort" log "FEHLER: Lease-Lock nach 15 Minuten nicht erhalten - kein Move"
exec 9>&-
exit 1
fi fi
register_lease register_lease
flock -u 9
exec 9>&-
else else
register_lease log "FEHLER: flock fehlt - kein sicherer Handoff und kein Move"
exit 1
fi fi
# Ab hier: Status-Polling ohne Lock (lang laufend, stoert keine andere Registrierung). # Ab hier: Status-Polling ohne Lock (lang laufend, stoert keine andere Registrierung).