Some checks failed
Cross-Node Transaction Testing / transaction-test (push) Successful in 3s
Deploy to Testnet / deploy-testnet (push) Successful in 1m13s
Integration Tests / test-service-integration (push) Successful in 2m39s
Multi-Node Stress Testing / stress-test (push) Successful in 2s
Node Failover Simulation / failover-test (push) Successful in 3s
Python Tests / test-python (push) Failing after 1m10s
Security Scanning / security-scan (push) Has been cancelled
- Convert MarketplaceService methods to async (list_offers, get_offer, create_offer, list_bids, create_bid) - Add await to all database operations (execute, commit, refresh) - Change Session to AsyncSession in MarketplaceService constructor - Add try-except blocks with detailed logging to all service methods - Add logging for method entry, database queries, result counts, and errors - Add try-except with logging to get_session generator - Simpl
55 lines
1.7 KiB
Python
55 lines
1.7 KiB
Python
"""
|
|
Database session management for Marketplace service
|
|
"""
|
|
|
|
import os
|
|
from contextlib import asynccontextmanager
|
|
from typing import AsyncIterator
|
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
|
|
from sqlmodel import SQLModel
|
|
|
|
from aitbc import get_logger
|
|
|
|
logger = get_logger(__name__)
|
|
|
|
# Database URL from environment variable or default
|
|
DATABASE_URL = os.getenv("DATABASE_URL", "sqlite+aiosqlite:///./data/marketplace_service.db")
|
|
|
|
# Create async engine
|
|
engine = create_async_engine(DATABASE_URL, echo=False)
|
|
|
|
|
|
async def init_db() -> None:
|
|
"""Initialize database tables"""
|
|
try:
|
|
logger.info("Initializing database tables")
|
|
from .domain.marketplace import MarketplaceOffer, MarketplaceBid
|
|
from .domain.global_marketplace import (
|
|
MarketplaceRegion,
|
|
GlobalMarketplaceConfig,
|
|
GlobalMarketplaceOffer,
|
|
GlobalMarketplaceTransaction,
|
|
)
|
|
|
|
async with engine.begin() as conn:
|
|
await conn.run_sync(SQLModel.metadata.create_all)
|
|
|
|
logger.info("Marketplace service database initialized")
|
|
except Exception as e:
|
|
logger.error(f"Error initializing database: {type(e).__name__}: {str(e)}")
|
|
raise
|
|
|
|
|
|
async def get_session() -> AsyncIterator[AsyncSession]:
|
|
"""Get database session"""
|
|
try:
|
|
logger.debug("Creating database session")
|
|
async with AsyncSession(engine) as session:
|
|
logger.debug("Database session created successfully")
|
|
yield session
|
|
logger.debug("Database session closed")
|
|
except Exception as e:
|
|
logger.error(f"Error in get_session: {type(e).__name__}: {str(e)}")
|
|
raise
|