From 0e469d4aef9a94d46ac3630d99d892ffcaa4bd95 Mon Sep 17 00:00:00 2001 From: Felipe Alvarado Date: Thu, 9 Jan 2025 11:31:49 +0100 Subject: [PATCH] Remove unbind and delete queue in queue disconnect method (#57) --- .env.docker | 3 ++- .env.sample | 3 ++- .env.test | 3 ++- app/config.py | 4 ++++ app/datasources/queue/queue_provider.py | 10 +++++++--- 5 files changed, 17 insertions(+), 6 deletions(-) diff --git a/.env.docker b/.env.docker index e2d040c..321d811 100644 --- a/.env.docker +++ b/.env.docker @@ -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/ \ No newline at end of file +RABBITMQ_AMQP_URL=amqp://guest:guest@rabbitmq:5672/ +LOG_LEVEL=INFO \ No newline at end of file diff --git a/.env.sample b/.env.sample index e2d040c..321d811 100644 --- a/.env.sample +++ b/.env.sample @@ -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/ \ No newline at end of file +RABBITMQ_AMQP_URL=amqp://guest:guest@rabbitmq:5672/ +LOG_LEVEL=INFO \ No newline at end of file diff --git a/.env.test b/.env.test index 22f78ca..a141c64 100644 --- a/.env.test +++ b/.env.test @@ -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/ \ No newline at end of file +RABBITMQ_AMQP_URL=amqp://guest:guest@localhost:5672/ +LOG_LEVEL=INFO \ No newline at end of file diff --git a/app/config.py b/app/config.py index 95ee028..1d0b385 100644 --- a/app/config.py +++ b/app/config.py @@ -2,6 +2,7 @@ Base settings file for FastApi application. """ +import logging import os import secrets @@ -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" @@ -38,3 +40,5 @@ class Settings(BaseSettings): settings = Settings() + +logging.basicConfig(level=logging.getLevelName(settings.LOG_LEVEL)) diff --git a/app/datasources/queue/queue_provider.py b/app/datasources/queue/queue_provider.py index 9369f4f..71efac9 100644 --- a/app/datasources/queue/queue_provider.py +++ b/app/datasources/queue/queue_provider.py @@ -1,3 +1,4 @@ +import logging from asyncio import AbstractEventLoop from typing import Any, Callable @@ -18,6 +19,8 @@ QueueProviderUnableToConnectException, ) +logger = logging.getLogger(__name__) + class QueueProvider: @@ -41,9 +44,11 @@ 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) @@ -51,11 +56,13 @@ async def _connect(self, loop: AbstractEventLoop) -> None: 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: """ @@ -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