-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add the RabbitMQ consumer #24
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a3c935b
Add the RabbitMQ consumer
falvaradorodriguez 3ba9dd7
Fix env var name
falvaradorodriguez 127b804
Remove static methods in service
falvaradorodriguez de3b296
Add queue exceptions file
falvaradorodriguez 6debdd0
Update events test cases
falvaradorodriguez File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
REDIS_URL=redis://redis:6379/0 | ||
DATABASE_URL=psql://postgres:postgres@db:5432/postgres | ||
DATABASE_URL=postgresql+asyncpg://postgres:postgres@db:5432/postgres | ||
RABBITMQ_AMPQ_URL=amqp://guest:guest@rabbitmq:5672/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
REDIS_URL=redis://redis:6379/0 | ||
DATABASE_URL=postgresql+asyncpg://postgres:postgres@db:5432/postgres | ||
DATABASE_URL=postgresql+asyncpg://postgres:postgres@db:5432/postgres | ||
RABBITMQ_AMPQ_URL=amqp://guest:guest@rabbitmq:5672/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
class QueueProviderException(Exception): | ||
""" | ||
Generic exception for QueueProvider errors. | ||
""" | ||
|
||
pass | ||
|
||
|
||
class QueueProviderUnableToConnectException(QueueProviderException): | ||
""" | ||
Raised when a connection to RabbitMQ cannot be established. | ||
""" | ||
|
||
pass | ||
|
||
|
||
class QueueProviderNotConnectedException(QueueProviderException): | ||
""" | ||
Raised when no connection is established. | ||
""" | ||
|
||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
from asyncio import AbstractEventLoop | ||
from typing import Any, Callable | ||
|
||
import aio_pika | ||
from aio_pika.abc import ( | ||
AbstractExchange, | ||
AbstractIncomingMessage, | ||
AbstractQueue, | ||
AbstractRobustConnection, | ||
ConsumerTag, | ||
ExchangeType, | ||
) | ||
|
||
from app.config import settings | ||
|
||
from .exceptions import ( | ||
QueueProviderNotConnectedException, | ||
QueueProviderUnableToConnectException, | ||
) | ||
|
||
|
||
class QueueProvider: | ||
|
||
_connection: AbstractRobustConnection | None | ||
_exchange: AbstractExchange | None | ||
_events_queue: AbstractQueue | None | ||
|
||
def __init__(self) -> None: | ||
""" | ||
Initializes the QueueProvider instance with default values. | ||
""" | ||
self._connection = None | ||
self._exchange = None | ||
self._events_queue = None | ||
|
||
async def _connect(self, loop: AbstractEventLoop) -> None: | ||
""" | ||
Establishes a connection to RabbitMQ and sets up the exchange and queue. | ||
|
||
:param loop: The asyncio event loop used for the connection. | ||
:return: | ||
""" | ||
try: | ||
self._connection = await aio_pika.connect_robust( | ||
url=settings.RABBITMQ_AMPQ_URL, loop=loop | ||
) | ||
except aio_pika.exceptions.AMQPConnectionError as e: | ||
raise QueueProviderUnableToConnectException(e) | ||
|
||
channel = await self._connection.channel() | ||
self._exchange = await channel.declare_exchange( | ||
settings.RABBITMQ_AMQP_EXCHANGE, ExchangeType.FANOUT | ||
) | ||
self._events_queue = await channel.declare_queue( | ||
settings.RABBITMQ_DECODER_EVENTS_QUEUE_NAME, durable=True | ||
) | ||
if self._events_queue: | ||
await self._events_queue.bind(self._exchange) | ||
|
||
async def connect(self, loop: AbstractEventLoop) -> None: | ||
""" | ||
Ensures that the RabbitMQ connection is established. | ||
|
||
:param loop: The asyncio event loop used to establish the connection. | ||
:return: | ||
""" | ||
if not self._connection: | ||
await self._connect(loop) | ||
|
||
def is_connected(self) -> bool: | ||
""" | ||
Verifies if the connection to RabbitMQ is established. | ||
|
||
:return: True` if the connection is established, `False` otherwise. | ||
""" | ||
return self._connection is not None | ||
|
||
async def disconnect(self) -> None: | ||
""" | ||
Safely closes the RabbitMQ connection and cleans up resources. | ||
|
||
:return: | ||
""" | ||
if self._connection: | ||
if self._events_queue and self._exchange: | ||
await self._events_queue.unbind(exchange=self._exchange) | ||
await self._events_queue.delete(if_unused=False, if_empty=False) | ||
await self._connection.close() | ||
self._exchange = None | ||
self._connection = None | ||
self._events_queue = None | ||
|
||
async def consume(self, callback: Callable[[str], Any]) -> ConsumerTag: | ||
""" | ||
Starts consuming messages from the declared queue. | ||
|
||
- Each message is processed using the provided callback function. | ||
|
||
:param callback: A function to process incoming messages. | ||
:return: A tag identifying the active consumer. | ||
:raises QueueProviderNotConnectedException: if no connection or queue is initialized. | ||
""" | ||
if not self._connection or not self._events_queue: | ||
raise QueueProviderNotConnectedException() | ||
|
||
async def wrapped_callback(message: AbstractIncomingMessage) -> None: | ||
""" | ||
Wrapper for processing the message and handling ACKs. | ||
|
||
:param message: The incoming RabbitMQ message. | ||
""" | ||
await message.ack() | ||
body = message.body | ||
if body: | ||
callback(body.decode("utf-8")) | ||
|
||
return await self._events_queue.consume(wrapped_callback) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import json | ||
import logging | ||
from typing import Dict | ||
|
||
|
||
class EventsService: | ||
|
||
def process_event(self, message: str) -> None: | ||
""" | ||
Processes the incoming event message. | ||
|
||
:param message: The incoming message to process, expected to be a JSON string. | ||
""" | ||
try: | ||
tx_service_event = json.loads(message) | ||
|
||
if self.is_event_valid(tx_service_event): | ||
# TODO: process event! | ||
pass | ||
else: | ||
logging.error( | ||
f"Unsupported message. A valid message should have at least 'chainId' and 'type': {message}" | ||
) | ||
except json.JSONDecodeError: | ||
logging.error(f"Unsupported message. Cannot parse as JSON: {message}") | ||
|
||
def is_event_valid(self, tx_service_event: Dict) -> bool: | ||
""" | ||
Validates if the event has the required fields 'chainId' and 'type' as strings. | ||
|
||
:param tx_service_event: The event object to validate. | ||
:return: True if the event is valid (both 'chainId' and 'type' are strings), False otherwise. | ||
""" | ||
return isinstance(tx_service_event.get("chainId"), str) and isinstance( | ||
tx_service_event.get("type"), str | ||
) |
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import asyncio | ||
import unittest | ||
from unittest.mock import patch | ||
|
||
import aio_pika | ||
from aio_pika.abc import AbstractRobustConnection | ||
|
||
from app.config import settings | ||
from app.datasources.queue.exceptions import QueueProviderUnableToConnectException | ||
from app.datasources.queue.queue_provider import QueueProvider | ||
|
||
|
||
class TestQueueProviderIntegration(unittest.IsolatedAsyncioTestCase): | ||
async def asyncSetUp(self): | ||
self.provider = QueueProvider() | ||
self.loop = asyncio.get_event_loop() | ||
|
||
async def test_connect_success(self): | ||
self.assertFalse(self.provider.is_connected()) | ||
await self.provider.connect(self.loop) | ||
self.assertTrue(self.provider.is_connected()) | ||
await self.provider.disconnect() | ||
self.assertFalse(self.provider.is_connected()) | ||
|
||
async def test_connect_failure(self): | ||
provider = QueueProvider() | ||
|
||
with patch("app.config.settings.RABBITMQ_AMPQ_URL", "amqp://invalid-url"): | ||
with self.assertRaises(QueueProviderUnableToConnectException): | ||
await provider.connect(self.loop) | ||
|
||
async def test_consume(self): | ||
await self.provider.connect(self.loop) | ||
assert isinstance(self.provider._connection, AbstractRobustConnection) | ||
message = "Test message" | ||
channel = await self.provider._connection.channel() | ||
exchange = await channel.declare_exchange( | ||
settings.RABBITMQ_AMQP_EXCHANGE, aio_pika.ExchangeType.FANOUT | ||
) | ||
|
||
await exchange.publish( | ||
aio_pika.Message(body=message.encode("utf-8")), | ||
routing_key="", | ||
) | ||
|
||
received_messages = [] | ||
|
||
def callback(message: str): | ||
received_messages.append(message) | ||
|
||
await self.provider.consume(callback) | ||
|
||
# Wait to make sure the message is consumed. | ||
await asyncio.sleep(1) | ||
|
||
self.assertIn(message, received_messages) | ||
await self.provider.disconnect() |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import unittest | ||
from unittest.mock import patch | ||
|
||
from app.services.events import EventsService | ||
|
||
|
||
class TestEventsService(unittest.TestCase): | ||
|
||
def test_is_event_valid(self): | ||
valid_event = {"chainId": "123", "type": "transaction"} | ||
self.assertTrue(EventsService().is_event_valid(valid_event)) | ||
|
||
invalid_event_missing_chain_id = {"type": "transaction"} | ||
self.assertFalse(EventsService().is_event_valid(invalid_event_missing_chain_id)) | ||
|
||
invalid_event_missing_type = {"chainId": "123"} | ||
self.assertFalse(EventsService().is_event_valid(invalid_event_missing_type)) | ||
|
||
invalid_event_invalid_chain_id = {"chainId": 123, "type": "transaction"} | ||
self.assertFalse(EventsService().is_event_valid(invalid_event_invalid_chain_id)) | ||
|
||
invalid_event_invalid_type = {"chainId": "123", "type": 123} | ||
self.assertFalse(EventsService().is_event_valid(invalid_event_invalid_type)) | ||
|
||
@patch("logging.error") | ||
def test_process_event_valid_message(self, mock_log): | ||
valid_message = '{"chainId": "123", "type": "transaction"}' | ||
|
||
EventsService().process_event(valid_message) | ||
|
||
mock_log.assert_not_called() | ||
|
||
@patch("logging.error") | ||
def test_process_event_invalid_json(self, mock_log): | ||
invalid_message = '{"chainId": "123", "type": "transaction"' | ||
|
||
EventsService().process_event(invalid_message) | ||
|
||
mock_log.assert_called_with( | ||
'Unsupported message. Cannot parse as JSON: {"chainId": "123", "type": "transaction"' | ||
) | ||
|
||
invalid_message_invalid_type = '{"chainId": "123", "type": 123}' | ||
|
||
EventsService().process_event(invalid_message_invalid_type) | ||
|
||
mock_log.assert_called_with( | ||
'Unsupported message. A valid message should have at least \'chainId\' and \'type\': {"chainId": "123", "type": 123}' | ||
) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would move the exceptions to
exceptions.py
inside of the queue package.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 Moved here