Skip to content

Commit c76b1c2

Browse files
marfgold1auvipy
authored andcommitted
Add progress for retry connections (celery#5915)
This will show current retry progress so it will clear confusion about how many retries will be tried for connecting to broker. Closes celery#4556
1 parent d056305 commit c76b1c2

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

celery/worker/consumer/consumer.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
"""
5252

5353
CONNECTION_RETRY_STEP = """\
54-
Trying again {when}...\
54+
Trying again {when}... ({retries}/{max_retries})\
5555
"""
5656

5757
CONNECTION_ERROR = """\
@@ -421,8 +421,11 @@ def ensure_connected(self, conn):
421421
def _error_handler(exc, interval, next_step=CONNECTION_RETRY_STEP):
422422
if getattr(conn, 'alt', None) and interval == 0:
423423
next_step = CONNECTION_FAILOVER
424-
error(CONNECTION_ERROR, conn.as_uri(), exc,
425-
next_step.format(when=humanize_seconds(interval, 'in', ' ')))
424+
next_step = next_step.format(
425+
when=humanize_seconds(interval, 'in', ' '),
426+
retries=int(interval / 2),
427+
max_retries=self.app.conf.broker_connection_max_retries)
428+
error(CONNECTION_ERROR, conn.as_uri(), exc, next_step)
426429

427430
# remember that the connection is lazy, it won't establish
428431
# until needed.

t/unit/worker/test_consumer.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,22 @@ def test_connect_error_handler(self):
264264
errback = conn.ensure_connection.call_args[0][0]
265265
errback(Mock(), 0)
266266

267+
@patch('celery.worker.consumer.consumer.error')
268+
def test_connect_error_handler_progress(self, error):
269+
self.app.conf.broker_connection_retry = True
270+
self.app.conf.broker_connection_max_retries = 3
271+
self.app._connection = _amqp_connection()
272+
conn = self.app._connection.return_value
273+
c = self.get_consumer()
274+
assert c.connect()
275+
errback = conn.ensure_connection.call_args[0][0]
276+
errback(Mock(), 2)
277+
assert error.call_args[0][3] == 'Trying again in 2.00 seconds... (1/3)'
278+
errback(Mock(), 4)
279+
assert error.call_args[0][3] == 'Trying again in 4.00 seconds... (2/3)'
280+
errback(Mock(), 6)
281+
assert error.call_args[0][3] == 'Trying again in 6.00 seconds... (3/3)'
282+
267283

268284
class test_Heart:
269285

0 commit comments

Comments
 (0)