mc-dashboard/scripts/run-task.sh
hurkicorgi cf467b26c7 Expanded scheduled tasks + keyboard shortcuts
- scripts/run-task.sh: dispatcher for say / backup / snapshot-prune with
  safe arg handling (message stripped of CR/LF, integer-clamped keep
  count). Logs to ~/logs/mc-dashboard/tasks.log (falls back to /tmp).
- New /api/schedule/tasks GET/POST/DELETE route: stores tasks as
  crontab lines with `# mc-task:<base64(json)>` marker so the UI can
  round-trip them. Strict server-side validation:
    - Cron expression regex (5 fields, * / N / N-N / N,N / */N)
    - say message: 1–120 chars, no newlines/backticks/shell quotes
    - snapshot-prune keep: integer 1–50
    - task id: 16-hex only
  Single-quote-escaped message in the generated shell command.
- ScheduledTasks UI under ServerControls (alongside the existing single
  ScheduledRestart): pick type (Announce / Backup / Prune snapshots),
  preset schedule (daily at HH:MM or every N hours), adds with one
  click. Tasks list shows human-readable schedule + "next in Xh" hint
  computed client-side. Hover-reveal Remove action.
- Admin keyboard shortcuts: when not typing,
    r — refetch the active tab's query keys (toast feedback)
    / — focus the first input/contenteditable in the active panel
    ? — toast the shortcuts cheat sheet
  Chord-free, mirrors existing ⌘K palette and Esc handlers.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-13 05:57:39 -06:00

66 lines
1.7 KiB
Bash
Executable file

#!/bin/bash
# Dispatcher for dashboard-scheduled tasks.
# Usage: run-task.sh <type> [...params]
# say <message>
# backup
# snapshot-prune <keepCount>
set -u
TYPE="${1:-}"
shift || true
LOG="${MC_DASHBOARD_LOG:-/home/minecraft/logs/mc-dashboard/tasks.log}"
mkdir -p "$(dirname "$LOG")" 2>/dev/null || true
if [ ! -w "$(dirname "$LOG")" ]; then LOG="/tmp/mc-dashboard-tasks.log"; fi
log() {
echo "[$(date -Iseconds)] $*" >>"$LOG" 2>/dev/null || true
}
rcon_say() {
local msg="$1"
RCON_MSG="$msg" node -e '
const { Rcon } = require("/home/minecraft/dashboard/node_modules/rcon-client");
(async () => {
try {
const rcon = await Rcon.connect({ host: "127.0.0.1", port: 25575, password: "23991818cc169249f181436f2a29a013" });
await rcon.send("say " + process.env.RCON_MSG);
rcon.end();
} catch (e) {
process.stderr.write(String(e));
process.exit(1);
}
})();
'
}
case "$TYPE" in
say)
MSG="${*:-}"
if [ -z "$MSG" ]; then log "say: empty message"; exit 1; fi
MSG="$(printf '%s' "$MSG" | tr -d '\r\n')"
rcon_say "$MSG"
log "say: $MSG"
;;
backup)
bash /home/minecraft/dashboard/scripts/backup-world.sh >>"$LOG" 2>&1
log "backup: done ($?)"
;;
snapshot-prune)
KEEP="${1:-5}"
if ! [[ "$KEEP" =~ ^[0-9]+$ ]] || [ "$KEEP" -lt 1 ] || [ "$KEEP" -gt 50 ]; then
KEEP=5
fi
DIR="/home/minecraft/server/snapshots"
if [ -d "$DIR" ]; then
ls -1dt "$DIR"/*/ 2>/dev/null | tail -n +$((KEEP + 1)) | while read -r d; do
[ -n "$d" ] && rm -rf -- "$d"
done
fi
log "snapshot-prune: kept $KEEP"
;;
*)
log "unknown task type: $TYPE"
exit 1
;;
esac