Skip to content
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

Remove unbind and delete queue in queue disconnect method #57

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading