Some updates for backend thingies

This commit is contained in:
2026-03-20 15:59:07 -06:00
parent 84e9d3001a
commit 76156955b9
16 changed files with 1027 additions and 100 deletions

View File

@@ -0,0 +1,71 @@
"""RabbitMQ message consumer base"""
import pika
import json
from typing import Callable, Dict, Any
import logging
logger = logging.getLogger(__name__)
class RabbitMQConsumer:
"""Generic RabbitMQ consumer for consuming messages from queues"""
def __init__(self, queue_name: str, host: str = 'localhost', port: int = 5672):
self.queue_name = queue_name
self.host = host
self.port = port
self.callback = None
def set_callback(self, callback: Callable[[Dict[str, Any]], None]) -> None:
"""
Sets the callback function to be called when a message is received
Args:
callback: Function that takes a message dictionary as argument
"""
self.callback = callback
def start_consuming(self) -> None:
"""
Starts consuming messages from the queue
"""
try:
connection = pika.BlockingConnection(
pika.ConnectionParameters(host=self.host, port=self.port)
)
channel = connection.channel()
# Declare queue to ensure it exists
channel.queue_declare(queue=self.queue_name, durable=True)
def callback_wrapper(ch, method, properties, body):
try:
# Decode the message
message = json.loads(body.decode('utf-8'))
logger.info(f"Received message from queue '{self.queue_name}': {message}")
# Call the user's callback function
if self.callback:
self.callback(message)
# Acknowledge the message
ch.basic_ack(delivery_tag=method.delivery_tag)
except Exception as e:
logger.error(f"Error processing message: {e}")
# Negative acknowledge to requeue the message
ch.basic_nack(delivery_tag=method.delivery_tag, requeue=True)
# Set up the consumer with manual acknowledgment
channel.basic_consume(
queue=self.queue_name,
on_message_callback=callback_wrapper,
auto_ack=False
)
logger.info(f"[*] Waiting for messages in queue '{self.queue_name}'. Ctrl+C to exit.")
channel.start_consuming()
except Exception as e:
logger.error(f"Error in consumer: {e}")
raise