movetdarr v4: flock-Serialisierung gegen n8n-staticData-Race bei parallelen SAB-Auftraegen
This commit is contained in:
parent
71247af6b6
commit
e8a36502e9
1 changed files with 81 additions and 60 deletions
|
|
@ -1,7 +1,9 @@
|
|||
#!/bin/bash
|
||||
# movetdarr.sh v3 — SABnzbd -> FileFlows with durable n8n handoff lease
|
||||
# movetdarr.sh v4 — SABnzbd -> FileFlows with durable n8n handoff lease
|
||||
# Categories: serien4k->Sonarr UHD, serien->Sonarr FHD,
|
||||
# video4k->Radarr UHD, video->Radarr FHD.
|
||||
# v4: flock-Serialisierung des Lease-Blocks gegen n8n-staticData-Race
|
||||
# bei parallelen SAB-Aufträgen (Last-Writer-Wins verschluckt Jobs).
|
||||
|
||||
DIR="$1"
|
||||
NZB_NAME="$2"
|
||||
|
|
@ -15,6 +17,7 @@ SOURCE_ROOT="${SOURCE_ROOT:-/usenet/complete}"
|
|||
DEST_ROOT="${DEST_ROOT:-/tdarr/complete}"
|
||||
POLL_SECONDS="${POLL_SECONDS:-60}"
|
||||
MAX_POLLS="${MAX_POLLS:-1440}" # 24 hours
|
||||
LEASE_LOCK="${LEASE_LOCK:-/usenet/scripts/.movetdarr.lease.lock}"
|
||||
|
||||
log() { echo "$(date '+%d.%m.%Y %H:%M:%S'): $1" >> "$LOGFILE"; }
|
||||
|
||||
|
|
@ -39,7 +42,7 @@ notify_failure() {
|
|||
handoff "$payload" >/dev/null 2>&1 || true
|
||||
}
|
||||
|
||||
log "Start v3 - Kategorie=$CATEGORY Status=$STATUS Dir=$DIR"
|
||||
log "Start v4 - Kategorie=$CATEGORY Status=$STATUS Dir=$DIR"
|
||||
|
||||
if [ "$STATUS" != "0" ]; then
|
||||
log "Download fehlgeschlagen, ueberspringe"
|
||||
|
|
@ -74,30 +77,33 @@ if [ -z "$EXPECTED_FILES" ] || [ "$EXPECTED_FILES" -lt 1 ] || [ "$EXPECTED_FILES
|
|||
exit 1
|
||||
fi
|
||||
|
||||
# Lease registrieren, solange der SAB-Job und sein Quellpfad noch sichtbar sind.
|
||||
START_PAYLOAD="$(json_payload action start category "$CATEGORY" directory "$DIR" release "$NZB_NAME" cleanName "$CLEAN_NAME" expectedFiles "$EXPECTED_FILES")"
|
||||
START_RESPONSE="$(handoff "$START_PAYLOAD" 2>>"$LOGFILE")" || {
|
||||
# Lease registrieren und Move+Bestaetigung unter exclusivem Lock, damit parallele
|
||||
# SAB-Auftraege sich nicht gegenseitig den n8n-staticData-Job zerstoeren (v4).
|
||||
register_lease() {
|
||||
START_PAYLOAD="$(json_payload action start category "$CATEGORY" directory "$DIR" release "$NZB_NAME" cleanName "$CLEAN_NAME" expectedFiles "$EXPECTED_FILES")"
|
||||
START_RESPONSE="$(handoff "$START_PAYLOAD" 2>>"$LOGFILE")" || {
|
||||
log "FEHLER: n8n-Handoff konnte nicht registriert werden - kein Move"
|
||||
exit 1
|
||||
}
|
||||
JOB_ID="$(printf '%s' "$START_RESPONSE" | json_field jobId 2>>"$LOGFILE")"
|
||||
START_OK="$(printf '%s' "$START_RESPONSE" | json_field ok 2>>"$LOGFILE")"
|
||||
if [ "$START_OK" != "True" ] && [ "$START_OK" != "true" ] || [ -z "$JOB_ID" ]; then
|
||||
}
|
||||
JOB_ID="$(printf '%s' "$START_RESPONSE" | json_field jobId 2>>"$LOGFILE")"
|
||||
START_OK="$(printf '%s' "$START_RESPONSE" | json_field ok 2>>"$LOGFILE")"
|
||||
if [ "$START_OK" != "True" ] && [ "$START_OK" != "true" ] || [ -z "$JOB_ID" ]; then
|
||||
log "FEHLER: ungueltige Handoff-Antwort - kein Move"
|
||||
exit 1
|
||||
fi
|
||||
log "Handoff registriert: Job=$JOB_ID"
|
||||
fi
|
||||
log "Handoff registriert: Job=$JOB_ID"
|
||||
export JOB_ID
|
||||
|
||||
DEST="${DEST_ROOT}/${CATEGORY}"
|
||||
BASENAME="$(basename "$DIR")"
|
||||
TARGET="$DEST/$BASENAME"
|
||||
mkdir -p "$DEST" || {
|
||||
DEST="${DEST_ROOT:?DEST_ROOT muss gesetzt sein}/${CATEGORY}"
|
||||
BASENAME="$(basename "$DIR")"
|
||||
TARGET="$DEST/$BASENAME"
|
||||
mkdir -p "$DEST" || {
|
||||
log "FEHLER: Zielbasis konnte nicht erstellt werden: $DEST"
|
||||
notify_failure "mkdir_failed"
|
||||
exit 1
|
||||
}
|
||||
}
|
||||
|
||||
if [ -d "$TARGET" ]; then
|
||||
if [ -d "$TARGET" ]; then
|
||||
if find "$TARGET" -maxdepth 1 -type f \( -iname '*.mkv' -o -iname '*.mp4' -o -iname '*.avi' \) -print -quit | grep -q .; then
|
||||
log "WARNUNG: Ziel $TARGET enthaelt bereits Video-Dateien - kein Ueberschreiben"
|
||||
notify_failure "target_contains_video"
|
||||
|
|
@ -109,18 +115,18 @@ if [ -d "$TARGET" ]; then
|
|||
notify_failure "stale_target_remove_failed"
|
||||
exit 1
|
||||
}
|
||||
fi
|
||||
fi
|
||||
|
||||
if ! mv -- "$DIR" "$DEST/"; then
|
||||
if ! mv -- "$DIR" "$DEST/"; then
|
||||
log "FEHLER beim Verschieben nach $TARGET (Quelle bleibt: $DIR)"
|
||||
notify_failure "move_failed"
|
||||
exit 1
|
||||
fi
|
||||
log "Erfolgreich verschoben nach $TARGET; SAB-Job bleibt bis Arr-Import aktiv"
|
||||
fi
|
||||
log "Erfolgreich verschoben nach $TARGET; SAB-Job bleibt bis Arr-Import aktiv"
|
||||
|
||||
MOVED_PAYLOAD="$(json_payload action moved jobId "$JOB_ID")"
|
||||
MOVED_OK=false
|
||||
for attempt in 1 2 3 4 5; do
|
||||
MOVED_PAYLOAD="$(json_payload action moved jobId "$JOB_ID")"
|
||||
MOVED_OK=false
|
||||
for attempt in 1 2 3 4 5; do
|
||||
if MOVED_RESPONSE="$(handoff "$MOVED_PAYLOAD" 2>>"$LOGFILE")"; then
|
||||
MOVED_STATE="$(printf '%s' "$MOVED_RESPONSE" | json_field state 2>>"$LOGFILE")"
|
||||
if [ "$MOVED_STATE" = "processing" ] || [ "$MOVED_STATE" = "success" ]; then
|
||||
|
|
@ -130,13 +136,28 @@ for attempt in 1 2 3 4 5; do
|
|||
fi
|
||||
log "Handoff-Move-Bestaetigung Versuch $attempt/5 fehlgeschlagen"
|
||||
sleep 15
|
||||
done
|
||||
if [ "$MOVED_OK" != "true" ]; then
|
||||
done
|
||||
if [ "$MOVED_OK" != "true" ]; then
|
||||
log "FEHLER: Move konnte n8n nicht bestaetigt werden; Datei bleibt fuer Recovery in $TARGET"
|
||||
notify_failure "move_confirmation_failed"
|
||||
exit 1
|
||||
fi
|
||||
}
|
||||
|
||||
if command -v flock >/dev/null 2>&1; then
|
||||
flock -w 900 "$LEASE_LOCK" -c 'set -e' || true # Lock-Datei anlegen
|
||||
exec 9>"$LEASE_LOCK"
|
||||
if ! flock -w 900 9; then
|
||||
log "WARNUNG: Lease-Lock nach 15 Minuten nicht erhalten; fahre ohne Lock fort"
|
||||
register_lease
|
||||
else
|
||||
register_lease
|
||||
fi
|
||||
else
|
||||
register_lease
|
||||
fi
|
||||
|
||||
# Ab hier: Status-Polling ohne Lock (lang laufend, stoert keine andere Registrierung).
|
||||
STATUS_PAYLOAD="$(json_payload action status jobId "$JOB_ID")"
|
||||
for poll in $(seq 1 "$MAX_POLLS"); do
|
||||
if RESPONSE="$(handoff "$STATUS_PAYLOAD" 2>>"$LOGFILE")"; then
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue