Files
loki/scripts/install-hetzner-crons.sh
T
Mao NakamotoandClaude Opus 4.8 6e8a7ff27a chore(infra): move scheduled jobs off Vercel crons to Hetzner systemd timers
The four janitors (prune-debug-logs, nudge-idle, prune-agent-tokens,
send-digest-emails) were only ever triggered by Vercel cron and silently
stopped running when the Vercel project was paused. They now run on the box
(fleetcrown.orangecat.ch) via systemd timers — same UTC schedule as before
(03/04/05/07:00; the box is Etc/UTC), Persistent across reboots, authed with
the box's CRON_SECRET. Verified live: authed endpoint → 200, unauthed → 401,
test run exit 0.

- scripts/install-hetzner-crons.sh: idempotent installer (fc-cron@.service +
  per-job timers). Re-run after a box rebuild.
- Delete vercel.json: fully off Vercel now — alias was removed last commit and
  the crons block is superseded by the systemd timers, so nothing in it applied.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-15 14:32:19 +02:00

69 lines
2.2 KiB
Bash
Executable File

#!/usr/bin/env bash
# install-hetzner-crons.sh — install the scheduled-job timers on the Hetzner box.
#
# Replaces the old vercel.json `crons` block (dead since the Vercel exit). The
# box (fleetcrown.orangecat.ch, systemd fleetcrown-app on 127.0.0.1:4002) runs
# the same four /api/crons/* janitors via systemd timers. Schedules are in UTC
# to match the original Vercel cron behavior (the box is Etc/UTC).
#
# Idempotent — safe to re-run after a box rebuild or schedule change.
#
# Usage: bash scripts/install-hetzner-crons.sh
set -euo pipefail
HOST="root@${HETZNER_IP:-167.233.22.31}"
ssh -o BatchMode=yes "$HOST" 'bash -s' <<'REMOTE'
set -euo pipefail
# Runner: reads CRON_SECRET from the app .env and calls the local app. The
# secret never leaves the box and isn't duplicated into the unit files.
cat > /opt/fleetcrown/fc-cron.sh <<'SH'
#!/usr/bin/env bash
set -euo pipefail
ENV_FILE=/opt/fleetcrown/app/.env
SECRET="$(grep -m1 '^CRON_SECRET=' "$ENV_FILE" | sed 's/^CRON_SECRET=//; s/^"//; s/"$//' | tr -d '\r')"
NAME="$1"
curl -fsS -m 120 "http://127.0.0.1:4002/api/crons/${NAME}" \
-H "Authorization: Bearer ${SECRET}" \
-o /dev/null -w "fc-cron ${NAME}: HTTP %{http_code}\n"
SH
chmod +x /opt/fleetcrown/fc-cron.sh
cat > /etc/systemd/system/fc-cron@.service <<'SVC'
[Unit]
Description=FleetCrown scheduled job %i
After=network-online.target fleetcrown-app.service
Wants=network-online.target
[Service]
Type=oneshot
ExecStart=/opt/fleetcrown/fc-cron.sh %i
SVC
declare -A SCHED=( [prune-debug-logs]="03:00" [nudge-idle]="04:00" [prune-agent-tokens]="05:00" [send-digest-emails]="07:00" )
for name in "${!SCHED[@]}"; do
cat > "/etc/systemd/system/fc-cron@${name}.timer" <<TIMER
[Unit]
Description=FleetCrown cron timer: ${name}
[Timer]
OnCalendar=*-*-* ${SCHED[$name]}:00
Persistent=true
Unit=fc-cron@${name}.service
[Install]
WantedBy=timers.target
TIMER
done
systemctl daemon-reload
for name in prune-debug-logs nudge-idle prune-agent-tokens send-digest-emails; do
systemctl enable --now "fc-cron@${name}.timer" >/dev/null 2>&1
done
echo "✓ installed FleetCrown cron timers (UTC):"
systemctl list-timers 'fc-cron@*' --all --no-pager | grep -E 'fc-cron|NEXT' || true
REMOTE