Skip to content

Commit bdc9b08

Browse files
authored
Merge pull request #16 from cloudblue/bugfix/LITE-17445-cannot-publish
LITE-17445 At least 1 reconnect for producer on Connection Error
2 parents 603742a + 5c48765 commit bdc9b08

File tree

1 file changed

+24
-13
lines changed

1 file changed

+24
-13
lines changed

dj_cqrs/transport/rabbit_mq.py

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ class RabbitMQTransport(LoggingMixin, BaseTransport):
3030
def clean_connection(cls):
3131
if cls._producer_connection and not cls._producer_connection.is_closed:
3232
cls._producer_connection.close()
33-
cls._producer_connection = None
34-
cls._producer_channel = None
33+
34+
cls._producer_connection = None
35+
cls._producer_channel = None
3536

3637
@classmethod
3738
def consume(cls):
@@ -57,24 +58,34 @@ def consume(cls):
5758

5859
@classmethod
5960
def produce(cls, payload):
60-
# TODO: try to produce and reconnect several times, now leave as before
61-
# if cannot publish message - drop it and try to reconnect on next event
62-
rmq_settings = cls._get_common_settings()
63-
exchange = rmq_settings[-1]
64-
6561
try:
66-
# Decided not to create context-manager to stay within the class
67-
_, channel = cls._get_producer_rmq_objects(*rmq_settings)
68-
69-
cls._produce_message(channel, exchange, payload)
70-
cls.log_produced(payload)
62+
cls._produce(payload)
7163
except (exceptions.AMQPError, exceptions.ChannelError, exceptions.ReentrancyError):
72-
logger.error("CQRS couldn't be published: pk = {} ({}).".format(
64+
logger.error("CQRS couldn't be published: pk = {} ({}). Reconnect...".format(
7365
payload.pk, payload.cqrs_id,
7466
))
7567

7668
# in case of any error - close connection and try to reconnect
7769
cls.clean_connection()
70+
# reconnect at least 1 time
71+
try:
72+
cls._produce(payload)
73+
except (exceptions.AMQPError, exceptions.ChannelError, exceptions.ReentrancyError):
74+
logger.error("CQRS couldn't be published: pk = {} ({}).".format(
75+
payload.pk, payload.cqrs_id,
76+
))
77+
78+
cls.clean_connection()
79+
80+
@classmethod
81+
def _produce(cls, payload):
82+
rmq_settings = cls._get_common_settings()
83+
exchange = rmq_settings[-1]
84+
# Decided not to create context-manager to stay within the class
85+
_, channel = cls._get_producer_rmq_objects(*rmq_settings)
86+
87+
cls._produce_message(channel, exchange, payload)
88+
cls.log_produced(payload)
7889

7990
@classmethod
8091
def _consume_message(cls, ch, method, properties, body):

0 commit comments

Comments
 (0)