ba-auto-daily/ba_cron_run.sh

46 lines
1.9 KiB
Bash

#!/usr/bin/env bash
# Cron entry point for ba_dailies.sh presets. Pure process-locking/logging
# plumbing -- no game-automation logic, same "thin wrapper" spirit as
# ba_dailies.sh itself (see CLAUDE.md's Bash policy).
#
# The shared flock lock exists because "daily" fires twice a day (3:30 and
# 4:30) and "q4h" fires every 4 hours -- without a lock, two overlapping
# invocations would both drive xdotool/scrot against the same game window
# at once, which is unsafe. -n (non-blocking): if a previous run is still
# in progress, this invocation is skipped entirely rather than queued, so
# a scheduled fire time can never silently run late.
#
# Logs to ~/ba_logs/<preset>.log -- one file per preset, so "daily" and
# "q4h" never interleave. Every line is one of three explicitly-tagged
# outcomes (OK / FAILED / SKIPPED) specifically so a real failure can be
# found with a single command, e.g.:
# grep -E 'FAILED|SKIPPED' ~/ba_logs/*.log
# A bare exit code alone can't do this: flock and a genuinely crashed task
# both exit 1, so lock acquisition is checked as its own explicit step
# (via an fd-based flock, not the "flock -n LOCKFILE COMMAND" form used
# originally) rather than folded into the wrapped command's own exit code.
set -uo pipefail
PRESET="${1:?usage: ba_cron_run.sh <preset>}"
LOG_DIR="$HOME/ba_logs"
LOG_FILE="$LOG_DIR/$PRESET.log"
LOCK_FILE="$LOG_DIR/ba_dailies.lock"
mkdir -p "$LOG_DIR"
{
exec 9>"$LOCK_FILE"
if ! flock -n 9; then
echo "=== $(date -Iseconds) SKIPPED $PRESET -- previous run still in progress ==="
exit 0
fi
echo "=== $(date -Iseconds) starting $PRESET ==="
"$HOME/ba_dailies.sh" "$PRESET"
status=$?
if [ "$status" -eq 0 ]; then
echo "=== $(date -Iseconds) finished $PRESET OK (exit 0) ==="
else
echo "=== $(date -Iseconds) finished $PRESET FAILED (exit $status) ==="
fi
} >> "$LOG_FILE" 2>&1