mirror of
https://github.com/bitbaum/evig.git
synced 2026-12-04 05:37:00 +01:00
evig-cron@timecard-reminders.service went red on 2026-09-10 and stayed red. Nothing was broken. The box booted at 08:01:48 UTC. `Persistent=true` makes systemd fire every schedule missed while the box was off the moment it comes back, so the 08:00 run caught up at 08:01:58 — the same second evig-app.service started. Next bound :4004 at 08:02:00. curl got connection-refused in between, `set -e` turned curl's exit 7 into run-cron.sh's exit status, and because this is a *daily* timer the unit then sat `failed` until the next morning: an incident page for a two-second race, and one day of reminders silently skipped. `After=evig-app.service` cannot fix this — it orders the timer after the app unit's *start*, never its readiness. So run-cron.sh now waits for the app it is supposed to call: retry ONLY curl's exit 7 (10 attempts, 6s apart, bounded well inside TimeoutStartSec=180), and log each wait so it is not a silent hang. A real HTTP status still fails on the first response — a broken endpoint stays exactly as loud as it was. Two smaller things in the same file: `set -e` would have killed the run before curl if CRON_SECRET were ever unset (the `[ -n ... ] && hdr=(...)` one-liner is the script's exit status when the test is false), and a connection failure used to produce no journal line at all. The Never-Twice half: scripts/ci/test-run-cron.sh drives the script against a fake app that binds late, one that answers 500, and one that never comes up, and asserts the retry is spent on exactly one of them. Wired into `verify` as `test:ops`, so CI runs it. Verified it fails against the pre-fix script. Editing this file changes nothing on the box — the deploy only rsyncs the standalone build — so the README's install block is now flagged as the only channel that ships it, and it must be re-run. Claude-Session: https://claude.ai/code/session_014FMEgKPDG3iMFExEvdJzWy Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
63 lines
2.9 KiB
Bash
Executable File
63 lines
2.9 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# Trigger a self-hosted cron endpoint on the local app. Replaces the 4 cron jobs
|
|
# that ran on Vercel before the self-host cutover (close-decisions,
|
|
# close-it-hilfe-requests, prune-audit-log, wake-recurring-tasks). Scheduled by
|
|
# the evig-cron@<job>.timer units. Source of truth: scripts/ops/ in the repo.
|
|
#
|
|
# Usage: run-cron.sh <endpoint> e.g. run-cron.sh close-decisions
|
|
set -euo pipefail
|
|
|
|
ENDPOINT="${1:?usage: run-cron.sh <endpoint>}"
|
|
APP_ENV="${APP_ENV:-/opt/evig/app/.env}"
|
|
PORT="${PORT:-4004}"
|
|
|
|
# The app is not listening the instant its unit starts. `After=evig-app.service`
|
|
# orders these timers after the app unit's *start*, never its readiness, and
|
|
# `Persistent=true` makes systemd fire every schedule missed while the box was
|
|
# off the moment it comes back.
|
|
#
|
|
# 2026-09-10: the box booted at 08:01:48 UTC. timecard-reminders (due 08:00)
|
|
# caught up at 08:01:58, evig-app.service started the same second, and Next only
|
|
# bound :4004 at 08:02:00. curl got connection-refused, `set -e` turned curl's
|
|
# exit 7 into the script's exit status, and because this is a *daily* timer the
|
|
# unit then sat in `failed` for the next 24h — paging the operator over a
|
|
# two-second race, and silently skipping that day's reminders entirely.
|
|
#
|
|
# So retry ONLY curl's connection-refused (exit 7 — the exact signature of "the
|
|
# app is still coming up"). A real HTTP status still fails on the first
|
|
# response, so a broken endpoint stays exactly as loud as it was. The whole
|
|
# retry budget is bounded well inside the unit's TimeoutStartSec=180.
|
|
RETRY_ATTEMPTS="${RETRY_ATTEMPTS:-10}"
|
|
RETRY_SLEEP_SECS="${RETRY_SLEEP_SECS:-6}"
|
|
|
|
# CRON_SECRET (if set) authenticates the call as `Authorization: Bearer <secret>`;
|
|
# the routes skip the auth check only when CRON_SECRET is unset.
|
|
SECRET="$(grep -E '^CRON_SECRET=' "$APP_ENV" 2>/dev/null | head -1 | cut -d= -f2- | tr -d '"' || true)"
|
|
hdr=()
|
|
# An `x && y` one-liner here would be the script's exit status when SECRET is
|
|
# empty, and `set -e` would kill the run before it ever called curl.
|
|
if [ -n "${SECRET:-}" ]; then
|
|
hdr=(-H "Authorization: Bearer ${SECRET}")
|
|
fi
|
|
|
|
out="$(mktemp)"; trap 'rm -f "$out"' EXIT
|
|
|
|
attempt=1
|
|
while :; do
|
|
rc=0
|
|
code="$(curl -s -o "$out" -w '%{http_code}' --max-time 120 "${hdr[@]}" \
|
|
"http://localhost:${PORT}/api/cron/${ENDPOINT}")" || rc=$?
|
|
[ "$rc" -eq 7 ] && [ "$attempt" -lt "$RETRY_ATTEMPTS" ] || break
|
|
echo "cron ${ENDPOINT}: :${PORT} refused the connection (attempt ${attempt}/${RETRY_ATTEMPTS}) — app still starting, retrying in ${RETRY_SLEEP_SECS}s"
|
|
sleep "$RETRY_SLEEP_SECS"
|
|
attempt=$((attempt + 1))
|
|
done
|
|
|
|
if [ "$rc" -ne 0 ]; then
|
|
echo "ERROR: cron ${ENDPOINT} could not reach http://localhost:${PORT} after ${attempt} attempt(s) (curl exit ${rc})"
|
|
exit "$rc"
|
|
fi
|
|
|
|
echo "cron ${ENDPOINT} → HTTP ${code}: $(head -c 300 "$out")"
|
|
[ "$code" = "200" ] || { echo "ERROR: cron ${ENDPOINT} returned ${code}"; exit 1; }
|