feat: Run database migrations in a thread pool to avoid blocking during initialization
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
import asyncio
|
||||||
from collections.abc import AsyncGenerator, Callable
|
from collections.abc import AsyncGenerator, Callable
|
||||||
|
|
||||||
from alembic.config import Config
|
from alembic.config import Config
|
||||||
@@ -49,8 +50,10 @@ async def init_db() -> None:
|
|||||||
# Get the alembic config
|
# Get the alembic config
|
||||||
alembic_cfg = Config("alembic.ini")
|
alembic_cfg = Config("alembic.ini")
|
||||||
|
|
||||||
# Run migrations to the latest revision
|
# Run migrations to the latest revision in a thread pool to avoid blocking
|
||||||
command.upgrade(alembic_cfg, "head")
|
await asyncio.get_event_loop().run_in_executor(
|
||||||
|
None, command.upgrade, alembic_cfg, "head",
|
||||||
|
)
|
||||||
logger.info("Database migrations completed successfully")
|
logger.info("Database migrations completed successfully")
|
||||||
|
|
||||||
except Exception:
|
except Exception:
|
||||||
|
|||||||
@@ -28,8 +28,8 @@ async def lifespan(_app: FastAPI) -> AsyncGenerator[None]:
|
|||||||
logger = get_logger(__name__)
|
logger = get_logger(__name__)
|
||||||
logger.info("Starting application")
|
logger.info("Starting application")
|
||||||
|
|
||||||
await init_db()
|
# await init_db()
|
||||||
logger.info("Database initialized")
|
# logger.info("Database initialized")
|
||||||
|
|
||||||
# Start the extraction processor
|
# Start the extraction processor
|
||||||
await extraction_processor.start()
|
await extraction_processor.start()
|
||||||
@@ -43,7 +43,8 @@ async def lifespan(_app: FastAPI) -> AsyncGenerator[None]:
|
|||||||
try:
|
try:
|
||||||
player_service = get_player_service() # Get the initialized player service
|
player_service = get_player_service() # Get the initialized player service
|
||||||
app_services.scheduler_service = SchedulerService(
|
app_services.scheduler_service = SchedulerService(
|
||||||
get_session_factory(), player_service,
|
get_session_factory(),
|
||||||
|
player_service,
|
||||||
)
|
)
|
||||||
await app_services.scheduler_service.start()
|
await app_services.scheduler_service.start()
|
||||||
logger.info("Enhanced scheduler service started")
|
logger.info("Enhanced scheduler service started")
|
||||||
|
|||||||
Reference in New Issue
Block a user