Document task DB access and unify background task DB handling
This commit is contained in:
@@ -687,6 +687,15 @@ APScheduler 4.x (async mode) manages recurring background tasks.
|
||||
|
||||
---
|
||||
|
||||
## 7.1 Background Tasks and Database Access
|
||||
|
||||
- APScheduler jobs run outside FastAPI request/response scope and therefore cannot rely on ``Depends(get_db)``.
|
||||
- Background tasks must open their own application database connection via ``app.db.open_db`` and close it when the work completes.
|
||||
- Use a shared task helper (``app.tasks.db.task_db``) so every task follows the same async context manager pattern and avoids connection leaks.
|
||||
- This pattern is intentional: task code is structurally separate from request-handling dependencies and should not attempt to reuse request-scoped DB connections.
|
||||
|
||||
---
|
||||
|
||||
## 8. API Design
|
||||
|
||||
### 8.1 Conventions
|
||||
|
||||
@@ -406,4 +406,6 @@ Reference: `Docs/Refactoring.md` for full analysis of each issue.
|
||||
|
||||
**Docs changes needed:** Add a "Background Tasks and Database Access" section to `Docs/Architekture.md` explaining why tasks own their connections and how to write a new task.
|
||||
|
||||
**Status:** Completed ✅
|
||||
|
||||
**Why this is needed:** Without explanation, the inconsistency between router DI-provided connections and task-managed connections looks like an oversight. Documentation prevents future developers from incorrectly trying to inject a DB connection into a task via `Depends`, which would fail silently at runtime.
|
||||
|
||||
Reference in New Issue
Block a user