Phase 2: Job enqueueing from counter
- Add backend/jobs.py with enqueue_random_number_job function - Modify counter increment endpoint to enqueue job after incrementing - Add mock_enqueue_job fixture to conftest.py for all tests - Add test_increment_enqueues_job_with_user_id to verify correct user_id - Job is enqueued synchronously; failure causes request to fail
This commit is contained in:
parent
10c0316603
commit
6ca0ae88dd
4 changed files with 98 additions and 2 deletions
|
|
@ -1,11 +1,12 @@
|
|||
"""Counter routes."""
|
||||
|
||||
from fastapi import APIRouter, Depends
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from auth import require_permission
|
||||
from database import get_db
|
||||
from jobs import enqueue_random_number_job
|
||||
from models import Counter, CounterRecord, Permission, User
|
||||
|
||||
router = APIRouter(prefix="/api/counter", tags=["counter"])
|
||||
|
|
@ -38,7 +39,7 @@ async def increment_counter(
|
|||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(require_permission(Permission.INCREMENT_COUNTER)),
|
||||
) -> dict[str, int]:
|
||||
"""Increment the counter and record the action."""
|
||||
"""Increment the counter, record the action, and enqueue a random number job."""
|
||||
counter = await get_or_create_counter(db)
|
||||
value_before = counter.value
|
||||
counter.value += 1
|
||||
|
|
@ -49,5 +50,15 @@ async def increment_counter(
|
|||
value_after=counter.value,
|
||||
)
|
||||
db.add(record)
|
||||
|
||||
# Enqueue random number job - if this fails, the request fails
|
||||
try:
|
||||
await enqueue_random_number_job(current_user.id)
|
||||
except Exception as e:
|
||||
await db.rollback()
|
||||
raise HTTPException(
|
||||
status_code=500, detail=f"Failed to enqueue job: {e}"
|
||||
) from e
|
||||
|
||||
await db.commit()
|
||||
return {"value": counter.value}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue