2025-02-23 16:22:33 +01:00
|
|
|
|
import json
|
|
|
|
|
import redis.asyncio as redis
|
|
|
|
|
|
|
|
|
|
class RedisClient:
|
2025-03-03 02:09:17 +01:00
|
|
|
|
def __init__(self, host: str, port: int, task_channel: str, result_channel: str, text_task_channel: str = "text_task_channel"):
|
2025-02-23 16:22:33 +01:00
|
|
|
|
self.client = redis.Redis(host=host, port=port, decode_responses=True)
|
|
|
|
|
self.task_channel = task_channel
|
|
|
|
|
self.result_channel = result_channel
|
2025-03-03 02:09:17 +01:00
|
|
|
|
self.text_task_channel = text_task_channel
|
2025-02-23 16:22:33 +01:00
|
|
|
|
|
2025-03-03 02:09:17 +01:00
|
|
|
|
async def get_task(self, timeout=0):
|
|
|
|
|
"""Получает задачу из очереди с помощью BLPOP"""
|
|
|
|
|
result = await self.client.blpop(self.task_channel, timeout=timeout)
|
|
|
|
|
if result:
|
|
|
|
|
_, task_json = result
|
|
|
|
|
try:
|
|
|
|
|
return json.loads(task_json)
|
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"Error parsing task message: {e}")
|
|
|
|
|
return None
|
2025-02-23 16:22:33 +01:00
|
|
|
|
|
|
|
|
|
async def publish_result(self, result: dict):
|
2025-03-03 02:09:17 +01:00
|
|
|
|
"""Публикует результат в канал результатов"""
|
2025-02-23 16:22:33 +01:00
|
|
|
|
await self.client.publish(self.result_channel, json.dumps(result))
|
2025-03-03 02:09:17 +01:00
|
|
|
|
|
|
|
|
|
async def send_to_summarize(self, task_data: dict):
|
|
|
|
|
"""Отправляет текст в сервис суммаризации"""
|
|
|
|
|
await self.client.rpush(self.text_task_channel, json.dumps(task_data))
|