Files
Sipro-Stocks/background/tasks.py
2024-11-05 00:38:48 +03:00

41 lines
1.2 KiB
Python

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)
@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())