From bf36e1e2be7c86e257c70caf98fda4bfd52d0a24 Mon Sep 17 00:00:00 2001 From: Z8MB1E Date: Sat, 10 Oct 2026 16:07:26 -0400 Subject: [PATCH] fix(deploy): supervise the Discord bot in the entrypoint The bot ran as an unsupervised background process, so a crash left the unit with no bot while the web app stayed healthy (and the failure was invisible). The entrypoint now runs the bot in a restart loop with capped exponential backoff (5s .. 60s), logs every exit, and stops the loop cleanly on SIGTERM/SIGINT. The web server still owns the container lifetime. DEPLOYMENT.md section 5 updated to match. --- DEPLOYMENT.md | 4 +-- docker-entrypoint.sh | 60 ++++++++++++++++++++++++++++++++++++-------- 2 files changed, 52 insertions(+), 12 deletions(-) diff --git a/DEPLOYMENT.md b/DEPLOYMENT.md index 2d3990f2..2af3503e 100644 --- a/DEPLOYMENT.md +++ b/DEPLOYMENT.md @@ -105,12 +105,12 @@ The Dockerfile pins Bun 1.3.11 (**build-time only**) and Node 22 alpine (runtime The bot ships inside the same app image and starts **only when `DISCORD_TOKEN` is present**: 1. **Presence gate** — the container entrypoint checks for `DISCORD_TOKEN`; if it's unset the bot never starts and the web app is unaffected. -2. **Fail-fast on missing guild id** — if `DISCORD_TOKEN` is set but `DISCORD_GUILD_ID` is missing, `src/bot/config.ts` throws at import time and the container fails fast. Set both together. +2. **Fail-fast on missing guild id** — if `DISCORD_TOKEN` is set but `DISCORD_GUILD_ID` is missing, `src/bot/config.ts` throws at import time; the bot process exits immediately and the supervisor retries it (see item 7). Set both together. 3. **Prod preflight checklist** — configure `DISCORD_OPS_CHANNEL_ID` and `DISCORD_STAFF_ROLE_IDS` **before** adding the token: on startup the bot immediately posts attendance embeds and starts DMing users per their notification preferences. 4. **Same-token-across-envs rationale** — the token is ONE Discord application credential, not a per-env secret. The value is identical across environments only because the presence gate restricts activation to prod; this does **not** conflict with the "never copy secrets between envs" guidance above, which governs per-env secrets like `PAYLOAD_SECRET` / `GAME_TICK_NOTIFY_SECRET`. 5. **Session-flapping warning** — never run the bot with the prod token from two places at once (e.g. local dev + prod): Discord force-disconnects one of the sessions. 6. **Env-clone warning** — a Coolify "clone environment" copy would carry the token along. Remove it from non-prod copies, or dev/stage would silently activate the bot. -7. **Recovery** — the entrypoint does **not** supervise/restart the bot. If the bot crashes, the web app stays healthy (announced subordinate-uptime default); recovery = Coolify Restart or redeploy. Troubleshooting: "token set but no bot login" → check the container logs for the bot's fail-fast line and verify the token/guild are valid. +7. **Recovery / supervision** — the entrypoint supervises the bot: when the bot exits, the entrypoint logs the exit and restarts it with capped exponential backoff (5s .. 60s), so a transient crash self-heals instead of silently leaving the unit with no bot. A persistent failure (e.g. a missing env var or an invalid token) crash-loops, surfacing as repeated `[entrypoint] Discord bot exited ...` lines in the container logs while the web app stays healthy; fix the cause, then Coolify Restart or redeploy. Troubleshooting: "token set but no bot login" → check the container logs for the bot's fail-fast line and verify the token/guild are valid. > ⚠️ The bot shares the single-instance constraint (Sec. 1): **running 2 replicas = two bot instances** — duplicate command registration, double embeds/DMs. Notifications generated while the container is down are **not backfilled** (in-memory cursor). diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh index df529221..26f24080 100755 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -4,32 +4,72 @@ # Starts the Discord bot (`node --import tsx src/bot/index.ts`) ONLY when # DISCORD_TOKEN is set (Coolify per-environment activation gate; dev/stage # leave it unset). -# Forwards SIGTERM/SIGINT to both children, reaps them, and exits with -# the web server's exit status. +# +# The bot is SUPERVISED: if it exits, the exit is logged and it is restarted +# with capped exponential backoff (5s .. 60s). This closes the old silent-death +# gap where a crashed bot left the unit with no bot while the web app stayed +# healthy. The web server owns the container's lifetime: when it exits, the +# supervisor is stopped and the container exits with the web server's status. +# SIGTERM/SIGINT are forwarded to both children. +BOT_RESTART_INITIAL_SECONDS=5 +BOT_RESTART_MAX_SECONDS=60 + +BOT_SUPERVISOR_PID="" + +# Restart loop for the bot. Runs in a subshell so the parent keeps waiting on +# the web server; on shutdown the parent signals this subshell, whose trap +# stops the current bot child and exits. +supervise_bot() { + bot_child="" + trap 'if [ -n "$bot_child" ]; then kill -TERM "$bot_child" 2>/dev/null; wait "$bot_child" 2>/dev/null; fi; exit 0' TERM INT + delay=$BOT_RESTART_INITIAL_SECONDS + attempt=0 + while :; do + attempt=$((attempt + 1)) + node --import tsx src/bot/index.ts & + bot_child=$! + wait "$bot_child" + bot_status=$? + bot_child="" + echo "[entrypoint] Discord bot exited (status ${bot_status}) on attempt ${attempt}; restarting in ${delay}s" >&2 + sleep "$delay" + delay=$((delay * 2)) + [ "$delay" -le "$BOT_RESTART_MAX_SECONDS" ] || delay=$BOT_RESTART_MAX_SECONDS + done +} + +# 1) Web server (owns the container lifetime). "$@" & WEB_PID=$! -BOT_PID="" + +# 2) Discord bot (only when the activation gate is open), supervised. if [ -n "${DISCORD_TOKEN:-}" ]; then - echo "[entrypoint] DISCORD_TOKEN is set — starting Discord bot" >&2 - node --import tsx src/bot/index.ts & - BOT_PID=$! + echo "[entrypoint] DISCORD_TOKEN is set — starting supervised Discord bot" >&2 + supervise_bot & + BOT_SUPERVISOR_PID=$! else echo "[entrypoint] DISCORD_TOKEN is not set — Discord bot disabled" >&2 fi + +# 3) Forward shutdown signals to both children. forward() { signal=$1 [ -z "$WEB_PID" ] || kill -"$signal" "$WEB_PID" 2>/dev/null || true - [ -z "$BOT_PID" ] || kill -"$signal" "$BOT_PID" 2>/dev/null || true + [ -z "$BOT_SUPERVISOR_PID" ] || kill -"$signal" "$BOT_SUPERVISOR_PID" 2>/dev/null || true } trap 'forward TERM' TERM trap 'forward INT' INT + +# 4) Wait for the web server; exit with its status. while kill -0 "$WEB_PID" 2>/dev/null; do wait "$WEB_PID" status=$? done -if [ -n "$BOT_PID" ]; then - kill -TERM "$BOT_PID" 2>/dev/null || true - wait "$BOT_PID" 2>/dev/null || true + +# 5) Web server gone: stop the bot supervisor, then exit. +if [ -n "$BOT_SUPERVISOR_PID" ]; then + kill -TERM "$BOT_SUPERVISOR_PID" 2>/dev/null || true + wait "$BOT_SUPERVISOR_PID" 2>/dev/null || true fi exit "${status:-0}"