feat: crap
This commit is contained in:
@@ -1,13 +1,24 @@
|
|||||||
from celery import Celery
|
from celery import Celery
|
||||||
|
|
||||||
# from backend.config import CELERY_RESULT_BACKEND, CELERY_BROKER_URL
|
# from backend.config import CELERY_RESULT_BACKEND, CELERY_BROKER_URL
|
||||||
|
|
||||||
|
broker = 'amqp://stocks:5W3FcvqXnnp1vv04jf673Mf8EY@localhost/stocks_vhost'
|
||||||
|
backend = 'rpc://'
|
||||||
|
#
|
||||||
|
# broker = 'amqp://test:test123@localhost/test_vhost'
|
||||||
|
# backend = 'rpc://'
|
||||||
app = Celery(
|
app = Celery(
|
||||||
__name__,
|
__name__,
|
||||||
broker='amqp://stocks:5W3FcvqXnnp1vv04jf673Mf8EY@localhost/stocks_vhost',
|
# broker='amqp://stocks:5W3FcvqXnnp1vv04jf673Mf8EY@localhost/stocks_vhost',
|
||||||
backend='rpc://',
|
# backend='rpc://',
|
||||||
|
broker=broker,
|
||||||
|
backend=backend
|
||||||
)
|
)
|
||||||
app.conf.broker_url = 'amqp://stocks:5W3FcvqXnnp1vv04jf673Mf8EY@localhost/stocks_vhost'
|
# app.conf.broker_url = 'amqp://stocks:5W3FcvqXnnp1vv04jf673Mf8EY@localhost/stocks_vhost'
|
||||||
app.conf.result_backend = 'rpc://'
|
# app.conf.result_backend = 'rpc://'
|
||||||
|
app.conf.broker_url = broker
|
||||||
|
app.conf.result_backend = backend
|
||||||
|
|
||||||
app.conf.update(
|
app.conf.update(
|
||||||
worker_concurrency=8,
|
worker_concurrency=8,
|
||||||
worker_prefetch_multiplier=1,
|
worker_prefetch_multiplier=1,
|
||||||
@@ -18,4 +29,4 @@ app.conf.update(
|
|||||||
broker_heartbeat=10
|
broker_heartbeat=10
|
||||||
)
|
)
|
||||||
|
|
||||||
import background.tasks
|
import background.tasks
|
||||||
|
|||||||
@@ -1,40 +1,32 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
from typing import List, Union
|
from typing import List, Union
|
||||||
|
|
||||||
from celery.signals import worker_ready
|
|
||||||
|
|
||||||
import background.update
|
import background.update
|
||||||
from limiter import BatchLimiter
|
|
||||||
from .celery_app import app
|
from .celery_app import app
|
||||||
|
|
||||||
|
|
||||||
def run_async(coroutine):
|
def run_async(coroutine):
|
||||||
"""Запускает асинхронную корутину в синхронном контексте."""
|
try:
|
||||||
return asyncio.run(coroutine)
|
# Попытка получить текущий запущенный цикл
|
||||||
|
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')
|
@app.task(name='process_update')
|
||||||
def process_update(product_ids: list[int]):
|
def process_update(product_ids: list[int]):
|
||||||
# Запускаем асинхронную функцию
|
|
||||||
return run_async(background.update.process_update(product_ids))
|
return run_async(background.update.process_update(product_ids))
|
||||||
|
|
||||||
|
|
||||||
@app.task(name='update_marketplace')
|
@app.task(name='update_marketplace')
|
||||||
def update_marketplace(marketplace_id: int):
|
def update_marketplace(marketplace_id: int):
|
||||||
return run_async(background.update.update_marketplace(marketplace_id))
|
return run_async(background.update.update_marketplace(marketplace_id))
|
||||||
|
|
||||||
|
|
||||||
@app.task(name='update_marketplace_products')
|
@app.task(name='update_marketplace_products')
|
||||||
def update_marketplace_products(marketplace_id: int, product_ids: list[int]):
|
def update_marketplace_products(marketplace_id: int, product_ids: list[int]):
|
||||||
return run_async(background.update.update_marketplace_products(marketplace_id, product_ids))
|
return run_async(background.update.update_marketplace_products(marketplace_id, product_ids))
|
||||||
|
|
||||||
|
|
||||||
@app.task(name='update_marketplaces')
|
@app.task(name='update_marketplaces')
|
||||||
def update_marketplaces(marketplace_ids: Union[List[int], None]):
|
def update_marketplaces(marketplace_ids: Union[List[int], None]):
|
||||||
return run_async(background.update.update_marketplaces(marketplace_ids))
|
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())
|
|
||||||
|
|||||||
Reference in New Issue
Block a user