Skip to content

Commit

Permalink
Remove unbind and delete queue in queue disconnect method (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
falvaradorodriguez authored Jan 9, 2025
1 parent c5cc1c0 commit 0e469d4
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .env.docker
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
REDIS_URL=redis://redis:6379/0
DATABASE_URL=postgresql+asyncpg://postgres:postgres@db:5432/postgres
RABBITMQ_AMQP_URL=amqp://guest:guest@rabbitmq:5672/
RABBITMQ_AMQP_URL=amqp://guest:guest@rabbitmq:5672/
LOG_LEVEL=INFO
3 changes: 2 additions & 1 deletion .env.sample
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
REDIS_URL=redis://redis:6379/0
DATABASE_URL=postgresql+asyncpg://postgres:postgres@db:5432/postgres
RABBITMQ_AMQP_URL=amqp://guest:guest@rabbitmq:5672/
RABBITMQ_AMQP_URL=amqp://guest:guest@rabbitmq:5672/
LOG_LEVEL=INFO
3 changes: 2 additions & 1 deletion .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ TEST=True
REDIS_URL=redis://localhost:6379/0
DATABASE_URL=postgresql+asyncpg://postgres:postgres@localhost:5432/postgres
DATABASE_POOL_CLASS=NullPool
RABBITMQ_AMQP_URL=amqp://guest:guest@localhost:5672/
RABBITMQ_AMQP_URL=amqp://guest:guest@localhost:5672/
LOG_LEVEL=INFO
4 changes: 4 additions & 0 deletions app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Base settings file for FastApi application.
"""

import logging
import os
import secrets

Expand All @@ -16,6 +17,7 @@ class Settings(BaseSettings):
case_sensitive=True,
)
TEST: bool = False
LOG_LEVEL: str = "INFO"
REDIS_URL: str = "redis://"
DATABASE_URL: str = "psql://postgres:"
DATABASE_POOL_CLASS: str = "AsyncAdaptedQueuePool"
Expand All @@ -38,3 +40,5 @@ class Settings(BaseSettings):


settings = Settings()

logging.basicConfig(level=logging.getLevelName(settings.LOG_LEVEL))
10 changes: 7 additions & 3 deletions app/datasources/queue/queue_provider.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
from asyncio import AbstractEventLoop
from typing import Any, Callable

Expand All @@ -18,6 +19,8 @@
QueueProviderUnableToConnectException,
)

logger = logging.getLogger(__name__)


class QueueProvider:

Expand All @@ -41,21 +44,25 @@ async def _connect(self, loop: AbstractEventLoop) -> None:
:return:
"""
try:
logger.info("Connecting to RabbitMQ")
self._connection = await aio_pika.connect_robust(
url=settings.RABBITMQ_AMQP_URL, loop=loop
)
logger.info("Connected to RabbitMQ")
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, durable=True
)
logger.info(f"Connected to {settings.RABBITMQ_AMQP_EXCHANGE} exchange")
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)
logger.info(f"Reading from {settings.RABBITMQ_DECODER_EVENTS_QUEUE_NAME} queue")

async def connect(self, loop: AbstractEventLoop) -> None:
"""
Expand All @@ -82,9 +89,6 @@ async def disconnect(self) -> None:
: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
Expand Down

0 comments on commit 0e469d4

Please sign in to comment.