initial
This commit is contained in:
0
app/routers/__init__.py
Normal file
0
app/routers/__init__.py
Normal file
19
app/routers/finish.py
Normal file
19
app/routers/finish.py
Normal file
@@ -0,0 +1,19 @@
|
||||
import bson
|
||||
import fastapi
|
||||
import pydantic
|
||||
|
||||
from app.storage.mongo import tasks
|
||||
|
||||
|
||||
class RequestBody(pydantic.BaseModel):
|
||||
id: str
|
||||
|
||||
|
||||
router = fastapi.APIRouter()
|
||||
|
||||
|
||||
@router.post('/api/v1/finish', status_code=fastapi.status.HTTP_202_ACCEPTED, responses={'404': {'description': 'Not found'}})
|
||||
async def execute(body: RequestBody):
|
||||
if await tasks.finish_task(bson.ObjectId(body.id)):
|
||||
return
|
||||
raise fastapi.HTTPException(404)
|
||||
30
app/routers/put.py
Normal file
30
app/routers/put.py
Normal file
@@ -0,0 +1,30 @@
|
||||
import datetime
|
||||
import fastapi
|
||||
import pydantic
|
||||
import typing
|
||||
|
||||
from app.storage.mongo import tasks
|
||||
from app.utils import time
|
||||
|
||||
|
||||
class RequestBody(pydantic.BaseModel):
|
||||
payload: dict
|
||||
seconds_to_execute: int
|
||||
delay: int|None = None
|
||||
|
||||
|
||||
router = fastapi.APIRouter()
|
||||
|
||||
|
||||
@router.post('/api/v1/put', status_code=fastapi.status.HTTP_202_ACCEPTED)
|
||||
async def execute(body: RequestBody, queue: typing.Annotated[str, fastapi.Header()]):
|
||||
now = time.now()
|
||||
await tasks.add_task(
|
||||
task=tasks.Task(
|
||||
queue=queue,
|
||||
payload=body.payload,
|
||||
put_at=now,
|
||||
available_from=(now + datetime.timedelta(seconds=body.delay)) if body.delay else now,
|
||||
seconds_to_execute=body.seconds_to_execute,
|
||||
),
|
||||
)
|
||||
22
app/routers/take.py
Normal file
22
app/routers/take.py
Normal file
@@ -0,0 +1,22 @@
|
||||
import fastapi
|
||||
import pydantic
|
||||
import typing
|
||||
|
||||
from app.storage.mongo import tasks
|
||||
|
||||
|
||||
router = fastapi.APIRouter()
|
||||
|
||||
|
||||
class Response(pydantic.BaseModel):
|
||||
id: str
|
||||
attempt: int
|
||||
payload: dict
|
||||
|
||||
|
||||
@router.get('/api/v1/take', responses={404: {'description': 'Not found'}})
|
||||
async def execute(queue: typing.Annotated[str, fastapi.Header()]) -> Response:
|
||||
task = await tasks.take_task(queue)
|
||||
if not task:
|
||||
raise fastapi.HTTPException(404)
|
||||
return Response(id=str(task._id), attempt=task.attempts, payload=task.payload)
|
||||
Reference in New Issue
Block a user