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

View File

@@ -1 +1 @@
from .broker import taskiq_broker
from .broker import taskiq_broker, scheduler

View File

@@ -1,5 +1,11 @@
from taskiq import TaskiqScheduler
from taskiq.schedule_sources import LabelScheduleSource
from taskiq_aio_pika import AioPikaBroker
import backend.config
taskiq_broker = AioPikaBroker(backend.config.RABBITMQ_URL)
scheduler = TaskiqScheduler(
broker=taskiq_broker,
sources=[LabelScheduleSource(taskiq_broker)]
)

View File

@@ -1,6 +1,8 @@
import background.update
import logging
import background.update
from background.broker import taskiq_broker
from buffer.core import TasksBuffer
@taskiq_broker.task(task_name='process_update')
@@ -21,3 +23,22 @@ async def update_marketplace_products(marketplace_id: int, product_ids: list[int
@taskiq_broker.task(task_name='update_marketplaces')
async def update_marketplaces(marketplace_ids: list[int]):
return await background.update.update_marketplaces(marketplace_ids)
@taskiq_broker.task(schedule=[{"cron": "* * * * *"}])
async def flush_buffer():
try:
logging.info('Flushing buffer')
buffer = TasksBuffer()
should_process = await buffer.should_process()
if not should_process:
logging.info('Buffer is empty')
return
product_ids = await buffer.get_tasks()
total_products = len(product_ids)
logging.info(f'Flushing buffer with {total_products} products')
await process_update(product_ids)
logging.info(f'Buffer flushed with {total_products} products')
except Exception as e:
logging.error(f'Error in flush_buffer: {e}')