Skip to content

Commit 3ff5271

Browse files
authored
Merge pull request #125 from cloudblue/fix-2/LITE-26790
LITE-26790 Better Logging in RabbitMQ producer
2 parents bd69778 + bf5c760 commit 3ff5271

File tree

2 files changed

+63
-11
lines changed

2 files changed

+63
-11
lines changed

dj_cqrs/transport/rabbit_mq.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -109,18 +109,26 @@ def _produce_with_retries(cls, payload, retries):
109109
cls._produce_message(channel, exchange, payload)
110110
cls.log_produced(payload)
111111
except (
112-
exceptions.AMQPError, exceptions.ChannelError, exceptions.ReentrancyError,
112+
exceptions.AMQPError,
113+
exceptions.ChannelError,
114+
exceptions.ReentrancyError,
113115
AMQPConnectorException,
114-
):
115-
logger.exception("CQRS couldn't be published: pk = {0} ({1}).{2}".format(
116-
payload.pk, payload.cqrs_id, " Reconnect..." if retries else "",
117-
))
118-
116+
) as e:
119117
# in case of any error - close connection and try to reconnect
120118
cls.clean_connection()
121119

122-
if retries:
123-
cls._produce_with_retries(payload, retries - 1)
120+
base_log_message = "CQRS couldn't be published: pk = {0} ({1}).".format(
121+
payload.pk, payload.cqrs_id,
122+
)
123+
if not retries:
124+
logger.exception(base_log_message)
125+
return
126+
127+
logger.warning('{0} Error: {1}. Reconnect...'.format(
128+
base_log_message, e.__class__.__name__,
129+
))
130+
131+
cls._produce_with_retries(payload, retries - 1)
124132

125133
@classmethod
126134
def _consume_message(cls, ch, method, properties, body, delay_queue):

tests/test_transport/test_rabbit_mq.py

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@
88
import ujson
99
from django.db import DatabaseError
1010
from pika.adapters.utils.connection_workflow import AMQPConnectorException
11-
from pika.exceptions import AMQPError, ChannelError, ReentrancyError
11+
from pika.exceptions import (
12+
AMQPError,
13+
ChannelError,
14+
ReentrancyError,
15+
StreamLostError,
16+
)
1217

1318
from dj_cqrs.constants import (
1419
DEFAULT_MASTER_AUTO_UPDATE_FIELDS,
@@ -223,8 +228,47 @@ def test_produce_retry_on_error(rabbit_transport, mocker, caplog):
223228
SignalType.SAVE, 'CQRS_ID', {'id': 1}, 1,
224229
),
225230
)
226-
assert "CQRS couldn't be published: pk = 1 (CQRS_ID). Reconnect..." in caplog.text
227-
assert 'CQRS is published: pk = 1 (CQRS_ID)' in caplog.text
231+
232+
assert caplog.record_tuples == [
233+
(
234+
'django-cqrs',
235+
logging.WARNING,
236+
"CQRS couldn't be published: pk = 1 (CQRS_ID)."
237+
" Error: AMQPConnectorException. Reconnect...",
238+
),
239+
(
240+
'django-cqrs',
241+
logging.INFO,
242+
'CQRS is published: pk = 1 (CQRS_ID), correlation_id = None.',
243+
),
244+
]
245+
246+
247+
def test_produce_retry_on_error_1(rabbit_transport, mocker, caplog):
248+
mocker.patch.object(RabbitMQTransport, '_get_producer_rmq_objects', side_effect=[
249+
StreamLostError,
250+
StreamLostError,
251+
])
252+
mocker.patch.object(RabbitMQTransport, '_produce_message', return_value=True)
253+
254+
rabbit_transport.produce(
255+
TransportPayload(
256+
SignalType.SAVE, 'CQRS_ID', {'id': 1}, 1,
257+
),
258+
)
259+
260+
assert caplog.record_tuples == [
261+
(
262+
'django-cqrs',
263+
logging.WARNING,
264+
"CQRS couldn't be published: pk = 1 (CQRS_ID). Error: StreamLostError. Reconnect...",
265+
),
266+
(
267+
'django-cqrs',
268+
logging.ERROR,
269+
"CQRS couldn't be published: pk = 1 (CQRS_ID).",
270+
),
271+
]
228272

229273

230274
def test_produce_message_ok(mocker):

0 commit comments

Comments
 (0)