Files
sourcehub/scripts/start_production.py
2026-09-14 23:22:10 +09:00

28 lines
929 B
Python

from __future__ import annotations
import os, subprocess, sys, time
from pathlib import Path
ROOT=Path(__file__).resolve().parents[1]
sys.path.insert(0,str(ROOT))
from sqlalchemy import text
from sourcehub.config import Settings
from sourcehub.db import build_engine
settings=Settings.from_env(ROOT)
engine=build_engine(settings)
last=None
for attempt in range(30):
try:
with engine.connect() as conn:
conn.execute(text("SELECT 1"))
last=None
break
except Exception as exc:
last=exc
print(f"[DB WAIT] {attempt+1}/30 {type(exc).__name__}",flush=True)
time.sleep(2)
if last:
raise SystemExit(f"Database unavailable: {last}")
subprocess.run([sys.executable,"-m","alembic","upgrade","head"],cwd=ROOT,check=True)
os.execvp(sys.executable,[sys.executable,"-m","uvicorn","app:app","--host","0.0.0.0","--port","8000","--proxy-headers","--forwarded-allow-ips=*"])