Harden gen-djmix, gen-vgm, strip-mb-tags, notify-telegram under set -euo pipefail (R1)

Adds strict mode to four maintenance scripts, with guards so expected non-zero
exits (empty beet queries, a missing config, a curl timeout) log/skip instead
of aborting: gen-djmix bails cleanly if djmix-albums.txt is absent and tolerates
a bad single query; gen-vgm tolerates an empty query; strip-mb-tags quotes its
path arg and best-efforts the trailing beet update; notify-telegram guards curl
so a network error still reaches its explicit "send failed" branch.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
andrew
2026-07-10 10:43:37 -06:00
parent 852b3797d8
commit 41ef26a40b
4 changed files with 23 additions and 10 deletions
+8 -2
View File
@@ -10,7 +10,7 @@
#
# Runs daily from cron. Safe to run manually anytime.
set -u
set -euo pipefail
CONFIG=${ALEMBIC_CONFIG_DIR:-/config}/pipeline/djmix-albums.txt
PLAYLIST_DIR=${MUSIC_DATA_DIR:-/data/music}/playlists
@@ -24,6 +24,11 @@ log() { echo "[$(date -Iseconds)] $*" | tee -a "$LOG"; }
log "=== Regenerating djmix.m3u8 ==="
if [[ ! -f "$CONFIG" ]]; then
log "no djmix-albums.txt at $CONFIG — nothing to generate"
exit 0
fi
TRACKS=$(mktemp)
while IFS= read -r query; do
@@ -40,7 +45,8 @@ while IFS= read -r query; do
_t="${_t%"${_t##*[![:space:]]}"}"
[[ -n "$_t" ]] && _args+=("$_t")
done
beet ls -f '$path' "${_args[@]}" 2>>"$LOG" >> "$TRACKS"
# `|| true`: a malformed single query shouldn't abort the whole regen.
beet ls -f '$path' "${_args[@]}" 2>>"$LOG" >> "$TRACKS" || true
done < "$CONFIG"
# Deduplicate and sort
+4 -2
View File
@@ -4,7 +4,7 @@
#
# Navidrome picks this up as a regular playlist. Safe to run manually anytime.
set -u
set -euo pipefail
PLAYLIST_DIR=${MUSIC_DATA_DIR:-/data/music}/playlists
OUT="${PLAYLIST_DIR}/vgm.m3u8"
@@ -17,7 +17,9 @@ log() { echo "[$(date -Iseconds)] $*" | tee -a "$LOG"; }
log "=== Regenerating vgm.m3u8 ==="
SORTED=$(beet ls -f '$path' 'genres:Game' 2>>"$LOG" | sort -u)
# `|| true`: an empty or failed query just means no tracks, handled below as a
# warning. This is a cosmetic playlist regen, not worth aborting the job over.
SORTED=$(beet ls -f '$path' 'genres:Game' 2>>"$LOG" | sort -u || true)
COUNT=$(echo -n "$SORTED" | grep -c '^' || true)
if [[ "$COUNT" -gt 0 ]]; then
+4 -2
View File
@@ -10,7 +10,7 @@
# Telegram messages are capped at 4096 chars. Anything longer is truncated
# with a "...<truncated>" tail. Returns exit 0 on send-OK, non-zero otherwise.
set -u
set -euo pipefail
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
CONFIG="${ALEMBIC_CONFIG_DIR:-/config}/pipeline/telegram/notify.env"
@@ -42,11 +42,13 @@ print(s, end='')
# Send via Bot API. Disable web-page preview and use plain text (no parse_mode)
# so log content with special chars doesn't get interpreted as Markdown.
# `|| true`: a curl timeout/network error must fall through to the explicit
# ok-check below (which reports "send failed" and exits 2), not abort here.
resp=$(curl -s --max-time 15 -X POST \
"https://api.telegram.org/bot${TG_BOT_TOKEN}/sendMessage" \
--data-urlencode "chat_id=${TG_CHAT_ID}" \
--data-urlencode "text=${MSG}" \
--data-urlencode "disable_web_page_preview=true")
--data-urlencode "disable_web_page_preview=true" || true)
ok=$(echo "$resp" | python3 -c "import sys,json;print(json.load(sys.stdin).get('ok',False))" 2>/dev/null || echo False)
if [[ "$ok" != "True" ]]; then
+7 -4
View File
@@ -10,19 +10,22 @@
# The tag list and per-file strip logic live in lib/mb-tags.sh, which is also
# sourced by import-track.sh to strip MB tags on every manual import.
set -u
set -euo pipefail
LOG=${ALEMBIC_CONFIG_DIR:-/config}/logs/mb-strip-$(date +%Y%m%d-%H%M%S).log
mkdir -p "$(dirname "$LOG")"
# shellcheck source=${PIPELINE_DIR:-/app/pipeline}/lib/mb-tags.sh
source ${PIPELINE_DIR:-/app/pipeline}/lib/mb-tags.sh
# strip_mb_tags is the same helper import-track.sh sources and runs under its
# own set -euo pipefail on every manual import, so it is already strict-safe.
source "${PIPELINE_DIR:-/app/pipeline}/lib/mb-tags.sh"
echo "[$(date -Iseconds)] Stripping release-identification tags from FLAC files in ${MUSIC_DATA_DIR:-/data/music}/Library" | tee -a "$LOG"
COUNT=$(strip_mb_tags ${MUSIC_DATA_DIR:-/data/music}/Library "$LOG")
COUNT=$(strip_mb_tags "${MUSIC_DATA_DIR:-/data/music}/Library" "$LOG")
echo "[$(date -Iseconds)] Stripped tags from $COUNT FLAC files" | tee -a "$LOG"
echo "[$(date -Iseconds)] Syncing beets DB..." | tee -a "$LOG"
beet update 2>&1 | tail -5 >> "$LOG"
# Best-effort DB sync; the strip already happened, so don't abort on a sync hiccup.
beet update 2>&1 | tail -5 >> "$LOG" || true
echo "[$(date -Iseconds)] Done. Log: $LOG" | tee -a "$LOG"
echo "[$(date -Iseconds)] Trigger a Navidrome scan now to re-evaluate album grouping" | tee -a "$LOG"