fixed python

This commit is contained in:
Deep Koluguri 2025-12-12 21:33:13 -05:00
parent d486be87dc
commit ab33c7314d
2 changed files with 40 additions and 8 deletions

View File

@ -3,6 +3,7 @@ Database connection module for Python service
Uses asyncpg for async PostgreSQL connections Uses asyncpg for async PostgreSQL connections
""" """
import os import os
import asyncio
import asyncpg import asyncpg
from typing import Optional from typing import Optional
from dotenv import load_dotenv from dotenv import load_dotenv
@ -12,9 +13,12 @@ load_dotenv()
# Database connection pool # Database connection pool
_pool: Optional[asyncpg.Pool] = None _pool: Optional[asyncpg.Pool] = None
# Connection timeout in seconds (reduced from default 60s to fail faster)
CONNECTION_TIMEOUT = 10
async def get_pool() -> asyncpg.Pool: async def get_pool() -> asyncpg.Pool:
"""Get or create database connection pool""" """Get or create database connection pool with timeout"""
global _pool global _pool
if _pool is None: if _pool is None:
@ -47,11 +51,28 @@ async def get_pool() -> asyncpg.Pool:
' - OR DATABASE_URL' ' - OR DATABASE_URL'
) )
_pool = await asyncpg.create_pool( # Create pool with timeout to prevent hanging
# Use min_size=1 to speed up initial connection (pool will grow as needed)
try:
_pool = await asyncio.wait_for(
asyncpg.create_pool(
**config, **config,
min_size=5, min_size=1, # Start with 1 connection, pool grows as needed
max_size=20, max_size=20,
command_timeout=60 command_timeout=60,
timeout=CONNECTION_TIMEOUT # Connection timeout per connection
),
timeout=CONNECTION_TIMEOUT # Overall timeout for pool creation
)
except asyncio.TimeoutError:
raise ConnectionError(
f'Database connection timeout after {CONNECTION_TIMEOUT}s. '
f'Check if database is reachable at {config.get("host", "unknown")}:{config.get("port", "unknown")}'
)
except Exception as e:
raise ConnectionError(
f'Failed to create database connection pool: {str(e)}. '
f'Check database configuration and network connectivity.'
) )
return _pool return _pool

View File

@ -47,7 +47,18 @@ class OptionsFlowResponse(BaseModel):
@app.on_event("startup") @app.on_event("startup")
async def startup(): async def startup():
"""Initialize database pool on startup""" """Initialize database pool on startup"""
await get_pool() try:
logger.info("Initializing database connection pool...")
pool = await get_pool()
# Test the connection with a quick query
async with pool.acquire() as conn:
await conn.fetchval("SELECT 1")
logger.info("✅ Database connection pool initialized successfully")
except Exception as e:
logger.error(f"⚠️ Failed to initialize database pool on startup: {str(e)}")
logger.warning("Service will start but database operations may fail. Connection will be retried on first request.")
# Don't raise - allow service to start and retry on first request
# This makes the service more resilient to temporary DB issues
@app.on_event("shutdown") @app.on_event("shutdown")