Compare commits

..

No commits in common. "8ca8bf88c1ad96c645c4ae6ce5bc5378fbd6d7a6" and "f4624772806ddfc8018180ea33bfa58f5eb6018c" have entirely different histories.

2 changed files with 7 additions and 3 deletions

View File

@ -15,6 +15,9 @@ class Task(pydantic.BaseModel):
class Response(pydantic.BaseModel): class Response(pydantic.BaseModel):
id: str
attempt: int
payload: dict
task: Task|None task: Task|None
@ -22,5 +25,5 @@ class Response(pydantic.BaseModel):
async def execute(queue: typing.Annotated[str, fastapi.Header()]) -> Response: async def execute(queue: typing.Annotated[str, fastapi.Header()]) -> Response:
task = await tasks.take_task(queue) task = await tasks.take_task(queue)
if not task: if not task:
return Response(task=None) raise fastapi.HTTPException(404)
return Response(task=Task(id=str(task._id), attempt=task.attempts, payload=task.payload)) return Response(id=str(task._id), attempt=task.attempts, payload=task.payload, task=Task(id=str(task._id), attempt=task.attempts, payload=task.payload))

View File

@ -1,6 +1,7 @@
import bson import bson
import datetime import datetime
import pydantic import pydantic
import typing
from app.storage.mongo import database from app.storage.mongo import database
from app.utils import time from app.utils import time
@ -26,7 +27,7 @@ async def add_task(task: Task) -> str:
return result.inserted_id return result.inserted_id
async def take_task(queue: str) -> Task|None: async def take_task(queue: str) -> typing.Optional[Task]:
now = time.now() now = time.now()
async for raw_task in collection.find({'queue': queue, 'available_from': {'$lte': now}}): async for raw_task in collection.find({'queue': queue, 'available_from': {'$lte': now}}):
task = Task.model_validate(raw_task) task = Task.model_validate(raw_task)