This repository has no description
0

Configure Feed

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

services.flix: stop transmission and alert on Matrix when disk is low

When /var/download drops below 100 GiB free the guard now stops all
transmission torrents (it has no native low-disk pause like sabnzbd's
download_free) and posts a Matrix message to !MRjkvAaFPsjdYCpeiZ:lassul.us
via the archiver bot's access token. Torrents are restarted and a
recovery message sent once space climbs back above the threshold. State
is tracked in StateDirectory so alerts fire only on transitions, not
every run.

Assisted-by: Claude Code (claude-opus-4-8)

+38 -13
+38 -13
2configs/services/flix/default.nix
··· 143 143 # pause downloads when /var/download runs low, so a full disk can no longer 144 144 # hard-wedge sabnzbd with an unrecoverable "Disk full! Forcing Pause". 145 145 # sabnzbd pauses itself once free disk drops below download_free/complete_free 146 - # (auto-resuming above); this guard keeps those set to THRESH GiB and, since 147 - # transmission has no equivalent, throttles its downloads below the threshold. 146 + # (auto-resuming above); this guard keeps those set to THRESH GiB. Transmission 147 + # has no equivalent, so below the threshold the guard stops all its torrents 148 + # (restarting them on recovery) and posts a Matrix alert on each transition. 148 149 systemd.services.download-space-guard = { 149 150 wantedBy = [ "multi-user.target" ]; 150 151 after = [ "sabnzbd.service" ]; ··· 154 155 pkgs.coreutils 155 156 pkgs.gnugrep 156 157 pkgs.gawk 158 + pkgs.jq 157 159 pkgs.transmission_4 158 160 ]; 159 161 script = '' 160 162 set -efu 161 163 DIR=/var/download 162 164 THRESH=100 165 + # !MRjkvAaFPsjdYCpeiZ:lassul.us, URL-encoded for the API path 166 + ROOM=%21MRjkvAaFPsjdYCpeiZ%3Alassul.us 167 + STATE="$STATE_DIRECTORY/state" 163 168 164 169 avail=$(df -Pk "$DIR" | awk 'NR==2 {print int($4 / 1024 / 1024)}') 165 170 ··· 167 172 key=$(grep -E '^api_key' "$INI" | head -1 | cut -d= -f2 | tr -d ' ') 168 173 sab() { 169 174 curl -fsS --max-time 15 "http://127.0.0.1:8080/api?output=json&apikey=$key&$1" >/dev/null || : 175 + } 176 + tr_remote() { 177 + transmission-remote 128.0.0.1:9091 "$@" >/dev/null || : 178 + } 179 + notify() { 180 + token=$(cat "$CREDENTIALS_DIRECTORY/matrix-token") 181 + body=$(jq -nc --arg b "$1" '{msgtype: "m.text", body: $b}') 182 + curl -fsS --max-time 15 -X POST \ 183 + -H "Authorization: Bearer $token" -H 'Content-Type: application/json' \ 184 + "https://matrix.lassul.us/_matrix/client/v3/rooms/$ROOM/send/m.room.message" \ 185 + --data "$body" >/dev/null || : 170 186 } 171 187 172 188 # sabnzbd pauses continuously once free disk drops below these (and ··· 176 192 || sab "mode=set_config&section=misc&keyword=$k&value=''${THRESH}G" 177 193 done 178 194 179 - # transmission has no built-in low-disk guard: throttle its downloads to a 180 - # trickle below the threshold (seeding keeps running), lift it above. 181 - tr_remote() { 182 - transmission-remote 128.0.0.1:9091 "$@" >/dev/null || : 183 - } 184 - if [ "$avail" -lt "$THRESH" ]; then 185 - echo "free ''${avail}G < ''${THRESH}G: sabnzbd paused by download_free, throttling transmission" 186 - tr_remote -d 1 187 - else 188 - echo "free ''${avail}G >= ''${THRESH}G: downloads unrestricted" 189 - tr_remote -D 195 + prev=ok 196 + if [ -f "$STATE" ]; then prev=$(cat "$STATE"); fi 197 + if [ "$avail" -lt "$THRESH" ]; then cur=low; else cur=ok; fi 198 + 199 + if [ "$cur" = low ]; then 200 + # stop everything each run so newly-grabbed torrents can't fill the disk 201 + tr_remote -t all --stop 202 + if [ "$prev" != low ]; then 203 + echo "free ''${avail}G < ''${THRESH}G: stopped transmission, alerting" 204 + notify "⚠️ yellow: /var/download down to ''${avail} GiB free (< ''${THRESH} GiB). Stopped all transmission torrents; sabnzbd auto-paused via download_free." 205 + fi 206 + elif [ "$prev" = low ]; then 207 + echo "free ''${avail}G >= ''${THRESH}G: resumed transmission, alerting" 208 + tr_remote -t all --start 209 + notify "✅ yellow: /var/download recovered to ''${avail} GiB free. Restarted transmission torrents." 190 210 fi 211 + echo "$cur" > "$STATE" 191 212 ''; 192 213 serviceConfig = { 193 214 Type = "oneshot"; 194 215 User = "root"; 216 + StateDirectory = "download-space-guard"; 217 + LoadCredential = [ 218 + "matrix-token:${config.clan.core.vars.generators.archiver-matrix.files."matrix-access-token".path}" 219 + ]; 195 220 }; 196 221 }; 197 222