feat: crap

This commit is contained in:
2024-11-05 00:44:27 +03:00
parent 2e0d5f0944
commit 729d59be48
2 changed files with 23 additions and 20 deletions

View File

@@ -1,40 +1,32 @@
import asyncio
from typing import List, Union
from celery.signals import worker_ready
import background.update
from limiter import BatchLimiter
from .celery_app import app
def run_async(coroutine):
"""Запускает асинхронную корутину в синхронном контексте."""
return asyncio.run(coroutine)
try:
# Попытка получить текущий запущенный цикл
loop = asyncio.get_running_loop()
except RuntimeError: # Если не запущен ни один цикл событий
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
return loop.run_until_complete(coroutine)
@app.task(name='process_update')
def process_update(product_ids: list[int]):
# Запускаем асинхронную функцию
return run_async(background.update.process_update(product_ids))
@app.task(name='update_marketplace')
def update_marketplace(marketplace_id: int):
return run_async(background.update.update_marketplace(marketplace_id))
@app.task(name='update_marketplace_products')
def update_marketplace_products(marketplace_id: int, product_ids: list[int]):
return run_async(background.update.update_marketplace_products(marketplace_id, product_ids))
@app.task(name='update_marketplaces')
def update_marketplaces(marketplace_ids: Union[List[int], None]):
return run_async(background.update.update_marketplaces(marketplace_ids))
@worker_ready.connect
def worker_is_ready(sender, **kwargs):
limiter = BatchLimiter()
return run_async(limiter.clear_locks())