Ensure Hygraph sync background task uses fresh session#89
Conversation
|
Caution Review failedThe pull request is closed. WalkthroughAdds Changes
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes Possibly related PRs
Poem
Pre-merge checks and finishing touches❌ Failed checks (2 warnings)
✅ Passed checks (1 passed)
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: ASSERTIVE Plan: Pro 📒 Files selected for processing (2)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (3)
backend/api/security.py (1)
38-41: Fix line length violation.Line 41 exceeds the configured maximum line length (91 characters, limit is likely 88-89).
Apply this diff to fix the line length:
expected = _load_expected_write_token(settings) if not expected or not secrets.compare_digest(token, expected): - raise HTTPException(status_code=HTTP_403_FORBIDDEN, detail="Invalid API write token.") + raise HTTPException( + status_code=HTTP_403_FORBIDDEN, + detail="Invalid API write token.", + ) return Truebackend/api/routes_sync.py (2)
66-104: Fix function return type annotation.The function signature declares
-> Dict[str, Any]but returnsJSONResponseobjects at lines 104 and 144. This causes a mypy type error.Apply this diff to fix the return type:
async def hygraph_webhook( request: Request, background: BackgroundTasks, db: Session = Depends(get_db), -) -> Dict[str, Any]: +) -> JSONResponse:
72-77: Fix line length violations in docstring.Lines 74-75 exceed the configured line length limit (91 characters).
Apply this diff to wrap the long lines:
""" Webhook receiver: - HMAC validated (dependency) - - Single size guard (2MB) already enforced by dependency; body/raw set on request.state + - Single size guard (2MB) already enforced by dependency; + body/raw set on request.state - DB dedup via SyncEvent(event_id, body_sha256 unique) - 202 fast-ack with background processing (pull_all) - Structured JSON log line and Prometheus counters """
📜 Review details
Configuration used: CodeRabbit UI
Review profile: ASSERTIVE
Plan: Pro
📒 Files selected for processing (3)
backend/api/routes_sync.py(3 hunks)backend/api/security.py(1 hunks)backend/tests/test_sync_routes_metrics.py(3 hunks)
🧰 Additional context used
🧬 Code graph analysis (3)
backend/api/routes_sync.py (2)
backend/api/db.py (1)
get_db(53-59)backend/services/hygraph_service.py (1)
pull_all(62-64)
backend/api/security.py (1)
backend/api/config.py (1)
Settings(10-62)
backend/tests/test_sync_routes_metrics.py (2)
backend/api/config.py (1)
get_settings(66-69)backend/services/hygraph_service.py (1)
HygraphService(11-80)
🪛 GitHub Actions: CI
backend/api/routes_sync.py
[error] 110-110: Mypy: Incompatible return value type (got JSONResponse, expected dict[str, Any]).
[error] 74-75: ruff: Line too long (91 > 88) in docstring or comment.
backend/api/security.py
[error] 41-41: E501 Line too long in security token check.
backend/tests/test_sync_routes_metrics.py
[warning] 1-1: pre-commit reported multiple hooks modified files (trailing whitespace, end-of-file, yaml and ruff fixes). Re-run formatting checks.
🔇 Additional comments (3)
backend/api/routes_sync.py (1)
113-140: LGTM! Fresh session lifecycle correctly implemented.The background task now properly creates and manages its own database session:
- Session created at line 113 using
SessionLocal()- Session passed to
HygraphService.pull_allat line 115- Session closed in the finally block at line 140
This ensures the background task operates independently of the request-scoped session and avoids closed-session errors.
backend/tests/test_sync_routes_metrics.py (2)
24-24: Good test isolation practice.Clearing the
get_settingscache in the test fixture ensures each test starts with fresh configuration, preventing cross-test pollution from cached settings.
62-82: Effective regression test for session management.This test validates the core fix: the background task can now execute database operations using its own session without encountering closed-session errors. The test:
- Patches
pull_allto perform a real DB interaction (SELECT 1)- Captures the scalar result to verify the query executed successfully
- Confirms the webhook returns 202 Accepted
This provides good coverage for the session lifecycle changes in
routes_sync.py.
| @@ -1,12 +1,15 @@ | |||
| from __future__ import annotations | |||
There was a problem hiding this comment.
Run pre-commit hooks to fix formatting issues.
The pipeline reports that pre-commit hooks modified files due to trailing whitespace, end-of-file issues, and other formatting problems.
Run the following command to apply the fixes:
#!/bin/bash
# Description: Run pre-commit hooks to fix formatting issues
pre-commit run --all-files🧰 Tools
🪛 GitHub Actions: CI
[warning] 1-1: pre-commit reported multiple hooks modified files (trailing whitespace, end-of-file, yaml and ruff fixes). Re-run formatting checks.
🤖 Prompt for AI Agents
In backend/tests/test_sync_routes_metrics.py around lines 1 to 1, the CI
reported pre-commit hooks modified files for trailing whitespace, EOF and
formatting issues; run the project's pre-commit checks locally (e.g. execute
"pre-commit run --all-files"), review and stage the modified files, and commit
the changes so the repository matches pre-commit standards before pushing the
branch.
|
Note Docstrings generation - SUCCESS |
…session` Docstrings generation was requested by @shayancoin. * #89 (comment) The following files were modified: * `backend/api/routes_sync.py` * `backend/api/security.py` * `backend/tests/test_sync_routes_metrics.py`
…session` (#153) Docstrings generation was requested by @shayancoin. * #89 (comment) The following files were modified: * `backend/api/routes_sync.py` * `backend/api/security.py` * `backend/tests/test_sync_routes_metrics.py` Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Co-authored-by: Shayan <shayan@coin.link>
Summary
Testing
https://chatgpt.com/codex/tasks/task_e_68f12bc067108330a145d8ae8d126867
Summary by CodeRabbit