feat: new stuff
This commit is contained in:
1
background/__init__.py
Normal file
1
background/__init__.py
Normal file
@@ -0,0 +1 @@
|
||||
from .broker import taskiq_broker
|
||||
5
background/broker.py
Normal file
5
background/broker.py
Normal file
@@ -0,0 +1,5 @@
|
||||
from taskiq_aio_pika import AioPikaBroker
|
||||
|
||||
import backend.config
|
||||
|
||||
taskiq_broker = AioPikaBroker(backend.config.RABBITMQ_URL)
|
||||
@@ -1,32 +0,0 @@
|
||||
from celery import Celery
|
||||
|
||||
# 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(
|
||||
__name__,
|
||||
# broker='amqp://stocks:5W3FcvqXnnp1vv04jf673Mf8EY@localhost/stocks_vhost',
|
||||
# backend='rpc://',
|
||||
broker=broker,
|
||||
backend=backend
|
||||
)
|
||||
# app.conf.broker_url = 'amqp://stocks:5W3FcvqXnnp1vv04jf673Mf8EY@localhost/stocks_vhost'
|
||||
# app.conf.result_backend = 'rpc://'
|
||||
app.conf.broker_url = broker
|
||||
app.conf.result_backend = backend
|
||||
|
||||
app.conf.update(
|
||||
worker_concurrency=8,
|
||||
worker_prefetch_multiplier=1,
|
||||
task_acks_late=True,
|
||||
broker_pool_limit=10,
|
||||
task_reject_on_worker_lost=True,
|
||||
task_publish_retry=True,
|
||||
broker_heartbeat=10
|
||||
)
|
||||
|
||||
import background.tasks
|
||||
@@ -1,32 +1,23 @@
|
||||
import asyncio
|
||||
from typing import List, Union
|
||||
|
||||
import background.update
|
||||
from .celery_app import app
|
||||
|
||||
from background.broker import taskiq_broker
|
||||
|
||||
|
||||
def run_async(coroutine):
|
||||
try:
|
||||
# Попытка получить текущий запущенный цикл
|
||||
loop = asyncio.get_running_loop()
|
||||
except RuntimeError: # Если не запущен ни один цикл событий
|
||||
loop = asyncio.new_event_loop()
|
||||
asyncio.set_event_loop(loop)
|
||||
@taskiq_broker.task(task_name='process_update')
|
||||
async def process_update(product_ids: list[int]):
|
||||
return await background.update.process_update(product_ids)
|
||||
|
||||
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))
|
||||
@taskiq_broker.task(task_name='update_marketplace')
|
||||
async def update_marketplace(marketplace_id: int):
|
||||
return await background.update.update_marketplace(marketplace_id)
|
||||
|
||||
@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))
|
||||
@taskiq_broker.task(task_name='update_marketplace_products')
|
||||
async def update_marketplace_products(marketplace_id: int, product_ids: list[int]):
|
||||
return await 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))
|
||||
|
||||
@taskiq_broker.task(task_name='update_marketplaces')
|
||||
async def update_marketplaces(marketplace_ids: list[int]):
|
||||
return await background.update.update_marketplaces(marketplace_ids)
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import logging
|
||||
from typing import Union, List
|
||||
|
||||
from backend.session import session_factory
|
||||
@@ -10,26 +11,25 @@ async def process_update(product_ids: list[int]):
|
||||
updates = [StockUpdate(product_id=product_id) for product_id in product_ids]
|
||||
updater = StocksUpdater(session)
|
||||
await updater.update(updates)
|
||||
return {'message': f'Stocks for products [{",".join(map(str, product_ids))}] successfully updated'}
|
||||
logging.info(f'Products [{",".join(list(map(str, product_ids)))}] successfully updated')
|
||||
|
||||
|
||||
async def update_marketplace(marketplace_id: int):
|
||||
async with session_factory() as session:
|
||||
updater = StocksUpdater(session)
|
||||
await updater.full_update_marketplace(marketplace_id)
|
||||
return {'message': f'Stocks for marketplace {marketplace_id} successfully updated'}
|
||||
logging.info(f'Marketplace {marketplace_id} successfully updated')
|
||||
|
||||
|
||||
async def update_marketplace_products(marketplace_id: int, product_ids: list[int]):
|
||||
async with session_factory() as session:
|
||||
updater = StocksUpdater(session)
|
||||
await updater.update_marketplace_products(marketplace_id, product_ids)
|
||||
return {
|
||||
'message': f'Products [{",".join(list(map(str, product_ids)))}] successfully updated for marketplace {marketplace_id}'}
|
||||
logging.info(f'Products [{",".join(map(str, product_ids))}] for marketplace {marketplace_id} successfully updated')
|
||||
|
||||
|
||||
async def update_marketplaces(marketplace_ids: Union[List[int], None]):
|
||||
async with session_factory() as session:
|
||||
updater = StocksUpdater(session)
|
||||
await updater.full_update_all_marketplaces(marketplace_ids)
|
||||
return {'message': f'Stocks for marketplaces [{",".join(map(str, marketplace_ids))}] successfully updated'}
|
||||
logging.info(f'Marketplaces {marketplace_ids} successfully updated')
|
||||
|
||||
Reference in New Issue
Block a user