Document task DB access and unify background task DB handling

This commit is contained in:
2026-04-17 17:18:49 +02:00
parent 16687b0520
commit 5e5d7c34b2
12 changed files with 139 additions and 90 deletions

29
backend/app/tasks/db.py Normal file
View File

@@ -0,0 +1,29 @@
"""Shared database helpers for APScheduler background tasks."""
from __future__ import annotations
from contextlib import asynccontextmanager
from typing import TYPE_CHECKING
from app.db import open_db
if TYPE_CHECKING:
from collections.abc import AsyncIterator
import aiosqlite
from app.config import Settings
@asynccontextmanager
async def task_db(settings: Settings) -> AsyncIterator[aiosqlite.Connection]:
"""Open a dedicated application database connection for a background task.
Background tasks run outside FastAPI request scope and therefore must
manage their own SQLite connection instead of using FastAPI dependencies.
"""
db = await open_db(settings.database_path)
try:
yield db
finally:
await db.close()