Spaces:
Sleeping
Sleeping
Download app/persistence/database/session.py from fallingbody/local-ai: direct link, hf CLI and curl.
- Browser
- Download file 3.25 kB
-
https://huggingface.co/spaces/fallingbody/local-ai/resolve/main/app/persistence/database/session.py
- Command line
-
hf download hf://spaces/fallingbody/local-ai/app/persistence/database/session.py
-
curl -L -o session.py https://huggingface.co/spaces/fallingbody/local-ai/resolve/main/app/persistence/database/session.py
3.25 kB
| from sqlalchemy import create_engine, event, exc, text | |
| from sqlalchemy.orm import sessionmaker, scoped_session | |
| from app.core.config import settings | |
| from app.core.logger import get_logger | |
| logger = get_logger(__name__) | |
| # Main database engine | |
| db_url = settings.DATABASE_URL | |
| if not db_url: | |
| raise ValueError("DATABASE_URL must be configured in the environment") | |
| is_sqlite = db_url.startswith("sqlite") | |
| if not is_sqlite: | |
| # Normalize Postgres URL schemes: | |
| # 1. postgres:// is deprecated in SQLAlchemy 1.4+ | |
| # 2. postgresql+psycopg:// requests psycopg3 which triggers ModuleNotFoundError if missing | |
| # 3. postgresql:// can default to psycopg in certain environments | |
| # We normalize to postgresql+psycopg2:// to guarantee compatibility with psycopg2 | |
| if db_url.startswith("postgres://"): | |
| db_url = db_url.replace("postgres://", "postgresql+psycopg2://", 1) | |
| elif db_url.startswith("postgresql+psycopg://"): | |
| db_url = db_url.replace("postgresql+psycopg://", "postgresql+psycopg2://", 1) | |
| elif db_url.startswith("postgresql://"): | |
| db_url = db_url.replace("postgresql://", "postgresql+psycopg2://", 1) | |
| import psycopg2.extras | |
| psycopg2.extras.register_uuid() | |
| logger.info("Successfully registered psycopg2 UUID adapter") | |
| engine_kwargs = { | |
| "pool_pre_ping": True, | |
| "echo": False, | |
| } | |
| if is_sqlite: | |
| engine_kwargs["connect_args"] = {"check_same_thread": False} | |
| else: | |
| engine_kwargs.update( | |
| pool_size=5, | |
| max_overflow=5, | |
| pool_recycle=3600, | |
| connect_args={"connect_timeout": 10}, | |
| ) | |
| engine = create_engine( | |
| db_url, | |
| **engine_kwargs, | |
| ) | |
| # Event listener to log connection issues | |
| def receive_connect(dbapi_conn, connection_record): | |
| logger.info("Database connection established") | |
| def receive_pool_checkout(dbapi_conn, connection_record, connection_proxy): | |
| connection_record.info['checkout_time'] = __import__('time').time() | |
| def receive_pool_checkin(dbapi_conn, connection_record): | |
| checkout_time = connection_record.info.get('checkout_time') | |
| if checkout_time: | |
| duration = __import__('time').time() - checkout_time | |
| if duration > 5.0: | |
| logger.warning(f"Long database connection held for {duration:.2f}s") | |
| SessionLocal = scoped_session( | |
| sessionmaker( | |
| autocommit=False, | |
| autoflush=False, | |
| bind=engine, | |
| ) | |
| ) | |
| import time | |
| def get_db_session(): | |
| """ | |
| Returns a scoped SQLAlchemy session. Retries on connection failure. | |
| """ | |
| max_retries = 3 | |
| retry_delay = 2 | |
| for attempt in range(max_retries): | |
| try: | |
| session = SessionLocal() | |
| session.execute(text("SELECT 1")) | |
| return session | |
| except Exception as e: | |
| if attempt < max_retries - 1: | |
| logger.warning(f"Database connection attempt {attempt + 1} failed, retrying...: {e}") | |
| if 'session' in locals(): | |
| session.close() | |
| time.sleep(retry_delay) | |
| else: | |
| logger.error(f"Database connection failed after {max_retries} attempts: {e}") | |
| raise | |