feat: buffer

This commit is contained in:
2024-11-09 07:52:33 +03:00
parent cdbe5f528c
commit 6e489bc6fd
8 changed files with 141 additions and 7 deletions

16
main.py
View File

@@ -9,10 +9,12 @@ from starlette.responses import JSONResponse
import backend.config
from background import taskiq_broker
from buffer.core import TasksBuffer
from schemas.general import *
import background.tasks
auth_schema = HTTPBearer()
buffer = TasksBuffer()
async def check_auth(token: Annotated[HTTPAuthorizationCredentials, Depends(auth_schema)]):
@@ -36,6 +38,7 @@ async def shutdown_broker():
@asynccontextmanager
async def lifespan(app: FastAPI):
await buffer.clear()
await start_broker()
yield
await shutdown_broker()
@@ -63,8 +66,14 @@ async def ping():
async def update(
request: UpdateRequest
):
task = await background.tasks.process_update.kiq(request.product_ids)
return UpdateResponse(task_id=task.task_id)
task_id = "-1"
await buffer.append_task(request.product_ids, update_time=False)
if await buffer.should_process():
product_ids = await buffer.get_tasks()
task = await background.tasks.process_update.kiq(product_ids)
task_id = task.task_id
await buffer.update_time()
return UpdateResponse(task_id=task_id)
@app.post('/update/marketplace')
@@ -87,9 +96,10 @@ async def update_marketplace_products(
async def update_marketplace(
request: UpdateMarketplacesRequest
):
task =await background.tasks.update_marketplaces.kiq(request.marketplace_ids)
task = await background.tasks.update_marketplaces.kiq(request.marketplace_ids)
return UpdateResponse(task_id=task.task_id)
@app.get("/tasks/{task_id}")
def get_status(task_id):
task_result = AsyncResult(task_id)