diff --git a/tools/music-audit/stageb-runner.sh b/tools/music-audit/stageb-runner.sh new file mode 100755 index 0000000..0147548 --- /dev/null +++ b/tools/music-audit/stageb-runner.sh @@ -0,0 +1,68 @@ +#!/usr/bin/env bash +# Run Stage B (album-level beets imports) over a list of folders, one at a time. +# +# Serialised on purpose: beets writes a single SQLite library, and two concurrent +# imports collide on it. Each folder gets its own log, and the runner waits for +# any import already in flight before starting the next. +# +# ./stageb-runner.sh # the default queue +# ./stageb-runner.sh "dir1" "dir2" # an explicit queue + +set -uo pipefail + +BEET=/opt/beets-venv/bin/beet +CONF=/etc/beets/config-stageb.yaml +LIB=/mnt/MediaDisk/Music/MyLibrary +LOGDIR=/var/log +QUEUE_MARK=/var/lib/beets/stageb-done.txt + +DEFAULT_QUEUE=( + "$LIB/Johnny Cash Discography" + "$LIB/Jimi Hendrix" + "$LIB/Queen Discography @ 320Kbps [Aufseher]" + "$LIB/Van Halen - Studio Discography 1978 - 2012 [FLAC] [h33t] - Kitlope" + "$LIB/Nine Inch Nails - Discography (1989 - 2008) [FLAC] [h33t] - Kitlope" + "$LIB/Styx [Discography]" + "$LIB/Leonard Cohen - Studio Discography (1967 - 2012) [Mp3 320]" + "$LIB/DLMusic" + "$LIB/Flac Zips" +) + +if [ "$#" -gt 0 ]; then + QUEUE=("$@") +else + QUEUE=("${DEFAULT_QUEUE[@]}") +fi + +mkdir -p "$(dirname "$QUEUE_MARK")" +touch "$QUEUE_MARK" + +log() { echo "$(date -u +%H:%M:%SZ) $*"; } + +for dir in "${QUEUE[@]}"; do + [ -d "$dir" ] || { log "SKIP (missing): $dir"; continue; } + if grep -qxF "$dir" "$QUEUE_MARK"; then + log "SKIP (already done): $dir" + continue + fi + + # Never run two imports at once. + while pgrep -f "[b]eet.*import" >/dev/null; do + log "waiting for the running import to finish before $dir" + sleep 30 + done + + slug=$(basename "$dir" | tr -c 'A-Za-z0-9' '-' | cut -c1-48) + logfile="$LOGDIR/beets-stageB-$slug.log" + log "importing: $dir" + "$BEET" -c "$CONF" import -q "$dir" > "$logfile" 2>&1 + rc=$? + skipped=$(grep -c "Skipping" "$logfile" 2>/dev/null || echo 0) + items=$(grep -cE "Importing|Applied" "$logfile" 2>/dev/null || echo 0) + log "done: $dir exit=$rc applied_markers=$items albums_skipped=$skipped log=$logfile" + if [ "$rc" -eq 0 ]; then + echo "$dir" >> "$QUEUE_MARK" + fi +done + +log "stage B queue finished"