feat: buffer
This commit is contained in:
@@ -1 +1 @@
|
||||
from .broker import taskiq_broker
|
||||
from .broker import taskiq_broker, scheduler
|
||||
@@ -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)]
|
||||
)
|
||||
|
||||
@@ -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}')
|
||||
|
||||
Reference in New Issue
Block a user