Skip to content

Commit b91a9ce

Browse files
committed
Catch RabbitMQ errors
- Don't take connection for granted, check if channel is established
1 parent d71e0c9 commit b91a9ce

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

safe_transaction_service/events/services/queue_service.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ def publish(self, message: str, retry: bool | None = True) -> bool:
5050
:return: `True` if message was published, `False` otherwise
5151
"""
5252
try:
53+
if not self.channel:
54+
raise pika.exceptions.AMQPError("Channel is not available")
55+
5356
self.channel.basic_publish(
5457
exchange=self.exchange_name, routing_key="", body=message
5558
)
@@ -68,7 +71,7 @@ def get_queue_service():
6871
if settings.EVENTS_QUEUE_URL:
6972
return QueueService()
7073
else:
71-
# Mock send_event to not configured host us is not mandatory configure a queue for events
74+
# It's not mandatory to configure a queue, so send_event will be mocked
7275
logger.warning("MockedQueueService is used")
7376
return MockedQueueService()
7477

@@ -99,8 +102,12 @@ def get_connection(self) -> BrokerConnection | None:
99102
else:
100103
broker_connection = BrokerConnection()
101104

102-
self._total_connections += 1
103-
return broker_connection
105+
if broker_connection.channel:
106+
self._total_connections += 1
107+
return broker_connection
108+
109+
logger.warning("RabbitMQ channel is not available")
110+
return None
104111

105112
def release_connection(self, broker_connection: BrokerConnection | None):
106113
"""

0 commit comments

Comments
 (0)