This repository has no description
0

Configure Feed

Select the types of activity you want to include in your feed.

services.flix: add transmission garbage collection and fix watcher

- Add transmission-gc timer to remove completed torrents after ratio >= 5.0
or age >= 30 days, deleting files to free disk space
- Enable ratio-limit and seed-queue in transmission settings
- Fix transmission-watcher to check service state before restarting,
preventing restart loops during startup

+102 -2
+102 -2
2configs/services/flix/default.nix
··· 20 20 umask = 18; 21 21 rpc-whitelist-enabled = false; 22 22 rpc-host-whitelist-enabled = false; 23 + # stop seeding after reaching 5.0 ratio 24 + ratio-limit = 5; 25 + ratio-limit-enabled = true; 26 + # limit concurrent seeding to reduce resource usage 27 + seed-queue-enabled = true; 28 + seed-queue-size = 20; 29 + }; 30 + }; 31 + 32 + # garbage collection for old torrents 33 + systemd.services.transmission-gc = { 34 + description = "Remove old completed torrents from Transmission"; 35 + after = [ "transmission.service" ]; 36 + path = [ 37 + pkgs.transmission_4 38 + pkgs.jq 39 + pkgs.coreutils 40 + pkgs.gawk 41 + pkgs.gnugrep 42 + pkgs.findutils 43 + ]; 44 + script = '' 45 + set -efu 46 + TRANSMISSION_HOST="128.0.0.1:9091" 47 + MAX_AGE_DAYS=30 48 + MIN_RATIO=5.0 49 + 50 + # get list of all torrents as JSON 51 + torrents=$(transmission-remote "$TRANSMISSION_HOST" -l 2>/dev/null | tail -n +2 | head -n -1 || true) 52 + 53 + if [ -z "$torrents" ]; then 54 + echo "No torrents found" 55 + exit 0 56 + fi 57 + 58 + # process each torrent 59 + echo "$torrents" | while read -r line; do 60 + id=$(echo "$line" | awk '{print $1}' | tr -d '*') 61 + # skip if not a valid ID 62 + [ -z "$id" ] || [ "$id" = "ID" ] && continue 63 + 64 + # get torrent info 65 + info=$(transmission-remote "$TRANSMISSION_HOST" -t "$id" -i 2>/dev/null || continue) 66 + 67 + # extract ratio and completion status 68 + ratio=$(echo "$info" | grep "Ratio:" | awk '{print $2}') 69 + percent=$(echo "$info" | grep "Percent Done:" | awk '{print $3}' | tr -d '%') 70 + state=$(echo "$info" | grep "State:" | cut -d: -f2- | xargs) 71 + 72 + # skip if not 100% complete 73 + [ "$percent" != "100" ] && continue 74 + 75 + # check if ratio reached 76 + ratio_reached=false 77 + if [ -n "$ratio" ] && [ "$ratio" != "None" ]; then 78 + if awk "BEGIN {exit !($ratio >= $MIN_RATIO)}"; then 79 + ratio_reached=true 80 + fi 81 + fi 82 + 83 + # check age via date added 84 + date_added=$(echo "$info" | grep "Date added:" | cut -d: -f2- | xargs) 85 + if [ -n "$date_added" ]; then 86 + added_epoch=$(date -d "$date_added" +%s 2>/dev/null || echo 0) 87 + now_epoch=$(date +%s) 88 + age_days=$(( (now_epoch - added_epoch) / 86400 )) 89 + else 90 + age_days=0 91 + fi 92 + 93 + # remove if ratio reached OR older than max age 94 + if [ "$ratio_reached" = "true" ]; then 95 + echo "Removing torrent $id (ratio: $ratio >= $MIN_RATIO)" 96 + transmission-remote "$TRANSMISSION_HOST" -t "$id" --remove-and-delete 97 + elif [ "$age_days" -ge "$MAX_AGE_DAYS" ]; then 98 + echo "Removing torrent $id (age: $age_days days >= $MAX_AGE_DAYS)" 99 + transmission-remote "$TRANSMISSION_HOST" -t "$id" --remove-and-delete 100 + fi 101 + done 102 + ''; 103 + serviceConfig = { 104 + Type = "oneshot"; 105 + User = "transmission"; 106 + }; 107 + }; 108 + 109 + systemd.timers.transmission-gc = { 110 + wantedBy = [ "timers.target" ]; 111 + timerConfig = { 112 + OnCalendar = "daily"; 113 + Persistent = true; 114 + RandomizedDelaySec = "1h"; 23 115 }; 24 116 }; 25 117 systemd.services.transmission-watcher = { ··· 31 123 ]; 32 124 script = '' 33 125 set -efu -o pipefail 34 - if ! curl -SsfL http://transmission.r; then 35 - systemctl restart transmission* 126 + # don't restart if transmission is currently starting or stopping 127 + state=$(systemctl show -p ActiveState --value transmission.service) 128 + if [ "$state" = "activating" ] || [ "$state" = "deactivating" ]; then 129 + echo "transmission is $state, skipping" 130 + exit 0 131 + fi 132 + # check if transmission responds (with timeout) 133 + if ! curl -SsfL --max-time 10 http://transmission.r; then 134 + echo "transmission not responding, restarting" 135 + systemctl restart transmission.service 36 136 fi 37 137 ''; 38 138 };