Skip to content

Base

BaseRabbitMQ

A base class for interacting with RabbitMQ.

Attributes:

Name Type Description
_sd

The service discovery instance.

url

The RabbitMQ connection URL.

connection

The RabbitMQ connection.

exchange

The RabbitMQ exchange.

Methods:

Name Description
__init__

Initialize the BaseRabbitMQ object.

_connect

Connect to RabbitMQ.

connect

Retry connecting to RabbitMQ until successful.

on_connection_error

Handle connection errors.

create_channel

Create a new channel in the RabbitMQ connection.

declare_exchange

Declare a RabbitMQ exchange.

create_queue

Create a RabbitMQ queue and bind it to an exchange.

close_connection

Close the RabbitMQ connection.

publish_message

Publish a message to a RabbitMQ exchange.

Source code in libs/python/resources/py-rabbitmq/pyrabbitmq/base.py
class BaseRabbitMQ:
    """A base class for interacting with RabbitMQ.

    Args:
        None

    Attributes:
        _sd: The service discovery instance.
        url: The RabbitMQ connection URL.
        connection: The RabbitMQ connection.
        exchange: The RabbitMQ exchange.

    Methods:
        __init__: Initialize the BaseRabbitMQ object.
        _connect: Connect to RabbitMQ.
        connect: Retry connecting to RabbitMQ until successful.
        on_connection_error: Handle connection errors.
        create_channel: Create a new channel in the RabbitMQ connection.
        declare_exchange: Declare a RabbitMQ exchange.
        create_queue: Create a RabbitMQ queue and bind it to an exchange.
        close_connection: Close the RabbitMQ connection.
        publish_message: Publish a message to a RabbitMQ exchange.

    """
    def __init__(self) -> None:
        self._sd = new_from_env()
        self.url = self._sd.rabbitmq_endpoint()
        self.connection = None
        self.exchange = None

    async def _connect(self) -> None:
        """Connect to RabbitMQ.

        Args:
            None

        Returns:
            None

        """
        parsed_url = urllib.parse.urlparse(self.url)
        self.connection = await aio_pika.connect(
            host=parsed_url.hostname,
            port=parsed_url.port,
            login=parsed_url.username,
            password=parsed_url.password,
            timeout=100,
            heartbeat=60,
        )

    async def connect(self, max_retries: int = 5) -> None:
        """Retry connecting to RabbitMQ until successful.

        Args:
            max_retries (int): The maximum number of connection retries.

        Raises:
            RuntimeError: If failed to connect to RabbitMQ after multiple retries.

        Returns:
            None
        """
        retries = 0
        while retries < max_retries:
            try:
                await self._connect()
                break
            except Exception as err:
                logger.error('[CONNECTION] - Could not connect to RabbitMQ, retrying in 2 seconds...')
                self.on_connection_error(err)
                await asyncio.sleep(2)
                retries += 1
        else:
            raise RuntimeError("Failed to connect to RabbitMQ after multiple retries")



    def on_connection_error(self, error: Exception) -> None:
        """Handle connection errors.

        Args:
            error (Exception): The connection error.

        Returns:
            None

        """
        logger.error(f"Connection error: {error}")
        logger.error(f"Connection parameters: {self.url}")

    async def create_channel(self) -> aio_pika.Channel:
        channel = await self.connection.channel()
        await channel.set_qos(prefetch_count=1)
        return channel

    async def declare_exchange(self, channel: aio_pika.Channel, exchange_name: str) -> None:
        """Declare a RabbitMQ exchange.

        Args:
            channel (aio_pika.Channel): The channel to declare the exchange on.
            exchange_name (str): The name of the exchange.

        Returns:
            None

        """
        self.exchange = await channel.declare_exchange(
            exchange_name, aio_pika.ExchangeType.TOPIC, durable=True
        )

    async def create_queue(self, channel: aio_pika.Channel, queue_name: str, exchange_name: str, routing_key: str) -> aio_pika.Queue:
        """Create a RabbitMQ queue and bind it to an exchange.

        Args:
            channel (aio_pika.Channel): The channel to create the queue on.
            queue_name (str): The name of the queue.
            exchange_name (str): The name of the exchange to bind the queue to.
            routing_key (str): The routing key to use for binding.

        Returns:
            aio_pika.Queue: The created queue.

        """
        await self.declare_exchange(channel, exchange_name)
        queue = await channel.declare_queue(queue_name, durable=True)
        await queue.bind(self.exchange, routing_key)

        return queue

    async def close_connection(self) -> None:
        """Close the RabbitMQ connection.

        Args:
            None

        Returns:
            None

        """
        if self.connection is not None:
            await self.connection.close()
            self.connection = None

    async def publish_message(self, exchange_name: str, routing_key: str, message: str) -> None:
        """Publish a message to a RabbitMQ exchange.

        Args:
            exchange_name (str): The name of the exchange to publish to.
            routing_key (str): The routing key for the message.
            message (str): The message to publish.

        Returns:
            None

        """
        try:
            await self.exchange.publish(
                aio_pika.Message(
                    body=message.encode(),
                    delivery_mode=aio_pika.DeliveryMode.PERSISTENT,
                ),
                routing_key=routing_key,
            )
            logger.info(f"Published message to exchange '{exchange_name}' with routing key '{routing_key}'")
        except Exception as e:
            logger.error(f"Error while publishing message: {e}")

connect(max_retries=5) async

Retry connecting to RabbitMQ until successful.

Parameters:

Name Type Description Default
max_retries int

The maximum number of connection retries.

5

Raises:

Type Description
RuntimeError

If failed to connect to RabbitMQ after multiple retries.

Returns:

Type Description
None

None

Source code in libs/python/resources/py-rabbitmq/pyrabbitmq/base.py
async def connect(self, max_retries: int = 5) -> None:
    """Retry connecting to RabbitMQ until successful.

    Args:
        max_retries (int): The maximum number of connection retries.

    Raises:
        RuntimeError: If failed to connect to RabbitMQ after multiple retries.

    Returns:
        None
    """
    retries = 0
    while retries < max_retries:
        try:
            await self._connect()
            break
        except Exception as err:
            logger.error('[CONNECTION] - Could not connect to RabbitMQ, retrying in 2 seconds...')
            self.on_connection_error(err)
            await asyncio.sleep(2)
            retries += 1
    else:
        raise RuntimeError("Failed to connect to RabbitMQ after multiple retries")

on_connection_error(error)

Handle connection errors.

Parameters:

Name Type Description Default
error Exception

The connection error.

required

Returns:

Type Description
None

None

Source code in libs/python/resources/py-rabbitmq/pyrabbitmq/base.py
def on_connection_error(self, error: Exception) -> None:
    """Handle connection errors.

    Args:
        error (Exception): The connection error.

    Returns:
        None

    """
    logger.error(f"Connection error: {error}")
    logger.error(f"Connection parameters: {self.url}")

declare_exchange(channel, exchange_name) async

Declare a RabbitMQ exchange.

Parameters:

Name Type Description Default
channel Channel

The channel to declare the exchange on.

required
exchange_name str

The name of the exchange.

required

Returns:

Type Description
None

None

Source code in libs/python/resources/py-rabbitmq/pyrabbitmq/base.py
async def declare_exchange(self, channel: aio_pika.Channel, exchange_name: str) -> None:
    """Declare a RabbitMQ exchange.

    Args:
        channel (aio_pika.Channel): The channel to declare the exchange on.
        exchange_name (str): The name of the exchange.

    Returns:
        None

    """
    self.exchange = await channel.declare_exchange(
        exchange_name, aio_pika.ExchangeType.TOPIC, durable=True
    )

create_queue(channel, queue_name, exchange_name, routing_key) async

Create a RabbitMQ queue and bind it to an exchange.

Parameters:

Name Type Description Default
channel Channel

The channel to create the queue on.

required
queue_name str

The name of the queue.

required
exchange_name str

The name of the exchange to bind the queue to.

required
routing_key str

The routing key to use for binding.

required

Returns:

Type Description
Queue

aio_pika.Queue: The created queue.

Source code in libs/python/resources/py-rabbitmq/pyrabbitmq/base.py
async def create_queue(self, channel: aio_pika.Channel, queue_name: str, exchange_name: str, routing_key: str) -> aio_pika.Queue:
    """Create a RabbitMQ queue and bind it to an exchange.

    Args:
        channel (aio_pika.Channel): The channel to create the queue on.
        queue_name (str): The name of the queue.
        exchange_name (str): The name of the exchange to bind the queue to.
        routing_key (str): The routing key to use for binding.

    Returns:
        aio_pika.Queue: The created queue.

    """
    await self.declare_exchange(channel, exchange_name)
    queue = await channel.declare_queue(queue_name, durable=True)
    await queue.bind(self.exchange, routing_key)

    return queue

close_connection() async

Close the RabbitMQ connection.

Returns:

Type Description
None

None

Source code in libs/python/resources/py-rabbitmq/pyrabbitmq/base.py
async def close_connection(self) -> None:
    """Close the RabbitMQ connection.

    Args:
        None

    Returns:
        None

    """
    if self.connection is not None:
        await self.connection.close()
        self.connection = None

publish_message(exchange_name, routing_key, message) async

Publish a message to a RabbitMQ exchange.

Parameters:

Name Type Description Default
exchange_name str

The name of the exchange to publish to.

required
routing_key str

The routing key for the message.

required
message str

The message to publish.

required

Returns:

Type Description
None

None

Source code in libs/python/resources/py-rabbitmq/pyrabbitmq/base.py
async def publish_message(self, exchange_name: str, routing_key: str, message: str) -> None:
    """Publish a message to a RabbitMQ exchange.

    Args:
        exchange_name (str): The name of the exchange to publish to.
        routing_key (str): The routing key for the message.
        message (str): The message to publish.

    Returns:
        None

    """
    try:
        await self.exchange.publish(
            aio_pika.Message(
                body=message.encode(),
                delivery_mode=aio_pika.DeliveryMode.PERSISTENT,
            ),
            routing_key=routing_key,
        )
        logger.info(f"Published message to exchange '{exchange_name}' with routing key '{routing_key}'")
    except Exception as e:
        logger.error(f"Error while publishing message: {e}")