apiVersion: batch/v1 kind: CronJob metadata: name: transmission-cleanup namespace: htpc labels: app.kubernetes.io/name: transmission-cleanup app.kubernetes.io/part-of: htpc spec: schedule: "*/15 * * * *" concurrencyPolicy: Forbid successfulJobsHistoryLimit: 1 failedJobsHistoryLimit: 2 jobTemplate: spec: template: metadata: labels: app.kubernetes.io/name: transmission-cleanup app.kubernetes.io/part-of: htpc spec: nodeSelector: kubernetes.io/hostname: vmserver restartPolicy: OnFailure containers: - name: cleanup image: lscr.io/linuxserver/transmission:latest imagePullPolicy: IfNotPresent command: - /bin/bash - -c - | set -euo pipefail RPC_URL="http://transmission.htpc.svc.cluster.local:9091/transmission/rpc" MAX_IDLE_SECONDS=43200 # 12 uur SESSION=$(curl -s "$RPC_URL" | sed -n 's/.*X-Transmission-Session-Id: \([^<]*\).*/\1/p') if [ -z "$SESSION" ]; then echo "Kon geen X-Transmission-Session-Id ophalen." exit 1 fi NOW=$(date +%s) RESPONSE=$(curl -s -H "X-Transmission-Session-Id: $SESSION" \ -d '{"method":"torrent-get","arguments":{"fields":["id","name","activityDate","addedDate","rateDownload","rateUpload","percentDone","status"]}}' \ "$RPC_URL") STALE_JSON=$(echo "$RESPONSE" | jq -c --argjson now "$NOW" --argjson max_idle "$MAX_IDLE_SECONDS" ' [ .arguments.torrents[] | (. as $t | (if $t.activityDate > $t.addedDate then $t.activityDate else $t.addedDate end)) as $last_active | select($last_active > 0 and (($now - $last_active) > $max_idle) and (.rateDownload == 0) and (.rateUpload == 0)) | {id: .id, name: .name, idle_hours: ((($now - $last_active) / 3600 * 10 | floor) / 10)} ] ') COUNT=$(echo "$STALE_JSON" | jq 'length') if [ "$COUNT" -eq 0 ]; then echo "Geen torrents gevonden die langer dan 12 uur inactief zijn." exit 0 fi echo "Verwijderen van $COUNT inactieve torrents (>12u) inclusief lokale data:" echo "$STALE_JSON" | jq -r '.[] | " - [\(.id)] \(.name) (\(.idle_hours)u inactief)"' IDS=$(echo "$STALE_JSON" | jq -c '[.[].id]') REMOVE_PAYLOAD=$(jq -nc --argjson ids "$IDS" '{"method":"torrent-remove","arguments":{"ids":$ids,"delete-local-data":true}}') RESULT=$(curl -s -H "X-Transmission-Session-Id: $SESSION" -d "$REMOVE_PAYLOAD" "$RPC_URL") echo "RPC resultaat: $RESULT"