feat(phase-0): stability hardening — search path fix, auto-migrations, graceful shutdown, health endpoint

- Remove SET search_path from db.py and migration SQL (Supabase uses public schema)
- Add migrate.py with tracking table for forward-only SQL migrations
- Add entrypoint.sh: waits for DB, runs migrations, then starts app
- Copy 01-schema.sql + zz-seed.sql to worker/src/migrations/
- Add health.py: /health endpoint (200/503) with DB connectivity + scheduler staleness checks
  - /stats endpoint with keyword/ad/notification counts
- Rewrite main.py shutdown sequence: signal handler, 5s grace for scheduler, ordered cleanup
- Update Dockerfile: HEALTHCHECK directive, entrypoint, COPY migrations
- Update docker-compose.yml: stop_grace_period=15s, healthcheck config, env vars
- Add aiohttp>=3.9 to requirements.txt for health server
This commit is contained in:
hermes
2026-07-04 11:30:45 -04:00
parent 64506f20b3
commit f540cbe7ef
16 changed files with 1133 additions and 9 deletions
+40
View File
@@ -0,0 +1,40 @@
#!/usr/bin/env bash
set -euo pipefail
# ────────────────────────────────────────────────
# Entrypoint: Run migrations, then start the app.
# ────────────────────────────────────────────────
echo "[entrypoint] Waiting for database to be ready..."
# Wait for PostgreSQL to accept connections (max 30s retries)
for i in $(seq 1 30); do
if python -c "import asyncpg,os; asyncio.get_event_loop().run_until_complete(
asyncpg.connect(
host=os.getenv('POSTGRES_HOST','db'),
port=int(os.getenv('POSTGRES_PORT','5432')),
user=os.getenv('POSTGRES_USER','postgres'),
password=os.getenv('POSTGRES_PASSWORD'),
database=os.getenv('POSTGRES_DB','postgres'),
)
)" 2>/dev/null; then
echo "[entrypoint] Database is ready."
break
fi
if [ "$i" -eq 30 ]; then
echo "[entrypoint] ERROR: Could not connect to database after 30 attempts." >&2
exit 1
fi
sleep 1
done
# ── Run migrations ──────────────────────────────
echo "[entrypoint] Running database migrations..."
python /app/migrate.py || {
echo "[entrypoint] ERROR: Migration failed — aborting startup." >&2
exit 1
}
echo "[entrypoint] Migrations complete."
# ── Start the application ───────────────────────
exec "$@"