fix install diogram

This commit is contained in:
2026-04-02 12:00:11 +05:00
parent 22bbc3a0a6
commit 8a189e310b
23 changed files with 1212 additions and 189 deletions

View File

@@ -43,4 +43,30 @@ def make_engine():
"""Создаёт SQLAlchemy engine с правильными параметрами для pg8000."""
from sqlalchemy import create_engine
s = Settings()
return create_engine(s.DATABASE_URL_syncpg, connect_args=s.PG8000_CONNECT_ARGS, future=True)
return create_engine(
s.DATABASE_URL_syncpg,
connect_args=s.PG8000_CONNECT_ARGS,
future=True,
pool_pre_ping=True, # проверяет соединение перед каждым запросом
)
def wait_for_db(retries: int = 15, delay: float = 2.0) -> None:
"""Ждёт, пока PostgreSQL станет доступен. Вызывается при старте приложения."""
import time
import logging
from sqlalchemy import text
logger = logging.getLogger("bd")
engine = make_engine()
for attempt in range(1, retries + 1):
try:
with engine.connect() as conn:
conn.execute(text("SELECT 1"))
logger.info("PostgreSQL is ready.")
return
except Exception as exc:
logger.warning("DB not ready (attempt %d/%d): %s", attempt, retries, exc)
if attempt < retries:
time.sleep(delay)
raise RuntimeError("PostgreSQL did not become ready in time.")