producers, consumers, rabbit_client

This commit is contained in:
2026-09-06 18:12:59 +03:00
parent d707c8b329
commit 10ce1755bd
14 changed files with 428 additions and 23 deletions
+35
View File
@@ -0,0 +1,35 @@
import json
from src.messaging.rabbitmq_client import rabbitmq_client
class EmailConsumer:
def __init__(self) -> None:
self.channel = None
self.queue = None
async def setup(self)->None:
self.channel = await rabbitmq_client.get_channel()
await self.channel.set_qos(prefetch_count=10)
self.queue = await self.channel.declare_queue("email_queue", durable=True)
async def process_message(self, message) -> None:
async with message.process():
data = json.loads(message.body)
print(f"Отправляю email на {data['email']}")
async def start_consuming(self)->None:
if self.queue is None:
await self.setup()
if self.channel is None or self.queue is None:
raise RuntimeError("Failed to set up RabbitMQ channel/queue")
async with self.queue.iterator() as queue_iter:
async for message in queue_iter:
await self.process_message(message)
email_consumer=EmailConsumer()