Self-hosted fitness tracker. Log workouts, track nutrition, and monitor progress. Docker deployment, mobile-friendly.
fix(fly): hourly demo reset silently no-ops under WAL mode (#74)
Carter244171062396
diff --git a/fly/Dockerfile b/fly/Dockerfileindex 4a379f0..0c48128 100644--- a/fly/Dockerfile+++ b/fly/Dockerfile@@ -45,7 +45,8 @@ COPY fly/nginx.fly.conf /etc/nginx/conf.d/default.conf# sed strips Windows \r\n → \n so the #!/bin/sh shebang works on LinuxCOPY fly/entrypoint.sh /app/entrypoint.shCOPY fly/reset.sh /app/reset.sh-RUN sed -i 's/\r$//' /app/entrypoint.sh /app/reset.sh && chmod +x /app/entrypoint.sh /app/reset.sh+COPY fly/snapshot.sh /app/snapshot.sh+RUN sed -i 's/\r$//' /app/entrypoint.sh /app/reset.sh /app/snapshot.sh && chmod +x /app/entrypoint.sh /app/reset.sh /app/snapshot.sh# /app/data is a Fly volume mount — this mkdir is a fallback for local testingRUN mkdir -p /app/datadiff --git a/fly/SETUP.md b/fly/SETUP.mdindex 97f4c31..3be5f5b 100644--- a/fly/SETUP.md+++ b/fly/SETUP.md@@ -49,9 +49,15 @@ The first deploy will:Once exercises and demo data are fully seeded (~60s after first deploy):```bash-fly ssh console --app lyftr-demo -C "cp /app/data/lyftr.db /app/data/lyftr.seed.db"+fly ssh console --app lyftr-demo -C "/app/snapshot.sh"```+The DB runs in WAL mode, so recent writes can still be sitting in `lyftr.db-wal`+rather than folded into `lyftr.db` — `snapshot.sh` stops the backend first (it+restarts on its own within seconds, same as `reset.sh`) and copies the+`-wal`/`-shm` side files too, when present, so the snapshot doesn't silently+miss whatever hasn't been checkpointed yet.+From this point the hourly cron (`reset.sh`) will restore this snapshot every hour,keeping the demo clean regardless of what visitors do.@@ -70,6 +76,9 @@ Update `CORS_ORIGIN` in `fly.toml` and redeploy.# Check logsfly logs --app lyftr-demo+# Check the reset job's own log (persists across restarts/redeploys)+fly ssh console --app lyftr-demo -C "cat /app/data/reset.log"+# Manual resetfly ssh console --app lyftr-demo -C "/app/reset.sh"@@ -77,7 +86,7 @@ fly ssh console --app lyftr-demo -C "/app/reset.sh"fly deploy --app lyftr-demo# Update seed snapshot after improving demo data-fly ssh console --app lyftr-demo -C "cp /app/data/lyftr.db /app/data/lyftr.seed.db"+fly ssh console --app lyftr-demo -C "/app/snapshot.sh"```## Architecturediff --git a/fly/entrypoint.sh b/fly/entrypoint.shindex 97ab8c7..fe0765e 100644--- a/fly/entrypoint.sh+++ b/fly/entrypoint.sh@@ -8,9 +8,11 @@set -e# Register hourly reset: copies seed snapshot → live DB, then kills backend-# so the restart loop below picks it back up with fresh data.+# so the restart loop below picks it back up with fresh data. Logged onto the+# /app/data volume (not /var/log) so it survives restarts/redeploys and is+# actually inspectable via `fly ssh console -C "cat /app/data/reset.log"`.mkdir -p /var/spool/cron/crontabs-echo "0 * * * * /app/reset.sh >> /var/log/reset.log 2>&1" > /var/spool/cron/crontabs/root+echo "0 * * * * /app/reset.sh >> /app/data/reset.log 2>&1" > /var/spool/cron/crontabs/rootcrond# Restart loop: reset.sh uses pkill to stop lyftr-api; this loop restarts it.diff --git a/fly/reset.sh b/fly/reset.shindex 2d3b04f..af3f2aa 100644--- a/fly/reset.sh+++ b/fly/reset.sh@@ -2,12 +2,13 @@# reset.sh — hourly demo reset, invoked by crond inside the container## Restores the live DB from a pre-seeded snapshot so the demo always shows-# realistic data. After the copy, pkill stops the backend; entrypoint.sh's-# restart loop brings it back with the fresh DB.+# realistic data. Stops the backend first, stages the restore, then swaps it+# in atomically, then lets entrypoint.sh's restart loop bring the backend+# back with the fresh DB.#-# The seed snapshot (lyftr.seed.db) is created once on first deploy:-# fly ssh console -a lyftr-demo-# cp /app/data/lyftr.db /app/data/lyftr.seed.db+# The seed snapshot (lyftr.seed.db [+ -wal/-shm if present]) is created via+# fly/snapshot.sh — see fly/SETUP.md.+set -eSEED="/app/data/lyftr.seed.db"LIVE="/app/data/lyftr.db"@@ -16,8 +17,34 @@ if [ ! -f "$SEED" ]; thenexit 0fi-echo "[reset] $(date): restoring demo DB..."-cp "$SEED" "$LIVE"-# Kill backend so entrypoint restart loop picks up the fresh DB immediately+echo "[reset] $(date): stopping backend..."pkill lyftr-api 2>/dev/null || true+# The backend has no SIGTERM handler, so it dies immediately and this rarely+# loops more than once — but the copy below must not race a still-open WAL.+for i in 1 2 3 4 5; do+ pgrep lyftr-api >/dev/null 2>&1 || break+ sleep 1+done++echo "[reset] $(date): restoring demo DB..."+# Stage the restore under temp names and verify every copy succeeds before+# touching the live files — if cp fails partway (full volume, I/O error),+# set -e aborts here and the current (stale but valid) live DB is left+# exactly as it was, instead of being deleted with nothing to replace it.+cp "$SEED" "$LIVE.new"+rm -f "$LIVE-wal.new" "$LIVE-shm.new"+if [ -f "$SEED-wal" ]; then cp "$SEED-wal" "$LIVE-wal.new"; fi+if [ -f "$SEED-shm" ]; then cp "$SEED-shm" "$LIVE-shm.new"; fi++# journal_mode=WAL means recent writes can still be sitting in lyftr.db-wal,+# not yet folded into lyftr.db. Leaving stale WAL/SHM side files behind lets+# SQLite replay those old frames back on top of the fresh copy the moment+# the backend reopens it — silently undoing the reset. Clear them only now+# that the staged copy above is known-good, and swap everything in via an+# atomic rename.+mv -f "$LIVE.new" "$LIVE"+rm -f "$LIVE-wal" "$LIVE-shm"+if [ -f "$LIVE-wal.new" ]; then mv -f "$LIVE-wal.new" "$LIVE-wal"; fi+if [ -f "$LIVE-shm.new" ]; then mv -f "$LIVE-shm.new" "$LIVE-shm"; fi+echo "[reset] $(date): done — backend will restart automatically"diff --git a/fly/snapshot.sh b/fly/snapshot.shnew file mode 100644index 0000000..28963a4--- /dev/null+++ b/fly/snapshot.sh@@ -0,0 +1,39 @@+#!/bin/sh+# snapshot.sh — (re)create the seed snapshot from the current live DB.+#+# Run once after first deploy (see fly/SETUP.md) to establish the baseline+# reset.sh restores every hour, and again any time you want to refresh what+# that baseline contains. Safe to re-run.+set -e+SEED="/app/data/lyftr.seed.db"+LIVE="/app/data/lyftr.db"++echo "[snapshot] $(date): stopping backend..."+pkill lyftr-api 2>/dev/null || true+# The backend has no SIGTERM handler, so it dies immediately and this rarely+# loops more than once — but the copy below must not race a still-open WAL.+for i in 1 2 3 4 5; do+ pgrep lyftr-api >/dev/null 2>&1 || break+ sleep 1+done++echo "[snapshot] $(date): capturing seed from live DB..."+# Stage under temp names and verify every copy succeeds before touching the+# seed files reset.sh depends on — if cp fails partway, set -e aborts here+# and the previous seed snapshot (if any) is left untouched.+cp "$LIVE" "$SEED.new"+rm -f "$SEED-wal.new" "$SEED-shm.new"+if [ -f "$LIVE-wal" ]; then cp "$LIVE-wal" "$SEED-wal.new"; fi+if [ -f "$LIVE-shm" ]; then cp "$LIVE-shm" "$SEED-shm.new"; fi++# Clear any stale seed-side WAL/SHM from a previous snapshot before swapping+# the new ones in — otherwise a re-run where the live WAL has since been+# auto-checkpointed away would leave an old seed.db-wal mismatched against+# the freshly copied seed.db, and reset.sh would replay those stale frames+# back onto the live DB on the very next hourly run.+mv -f "$SEED.new" "$SEED"+rm -f "$SEED-wal" "$SEED-shm"+if [ -f "$SEED-wal.new" ]; then mv -f "$SEED-wal.new" "$SEED-wal"; fi+if [ -f "$SEED-shm.new" ]; then mv -f "$SEED-shm.new" "$SEED-shm"; fi++echo "[snapshot] $(date): done — backend will restart automatically"