Skip to content

Commit 8911ea9

Browse files
Added a default value for retries in worker.strategy. (celery#5945)
* Added a default value for retries in worker.strategy. I was facing an issue when adding tasks directly to rabbitmq using pika instead of calling task.apply_async. The issue was the self.retry mechanisum was failing. In app/tasks.py the line `retries = request.retries + 1` was causing the issue. On further tracing I figured out that it was because the default .get value (None) was getting passed through this function and was raising TypeError: unsupported operand type(s) for +: 'NoneType' and 'int' * Add test cases for default and custom retries value
1 parent c52105e commit 8911ea9

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

celery/worker/strategy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def hybrid_to_proto2(message, body):
4848
'shadow': body.get('shadow'),
4949
'eta': body.get('eta'),
5050
'expires': body.get('expires'),
51-
'retries': body.get('retries'),
51+
'retries': body.get('retries', 0),
5252
'timelimit': body.get('timelimit', (None, None)),
5353
'argsrepr': body.get('argsrepr'),
5454
'kwargsrepr': body.get('kwargsrepr'),

t/unit/worker/test_strategy.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from celery.worker import state
1414
from celery.worker.request import Request
1515
from celery.worker.strategy import default as default_strategy
16-
from celery.worker.strategy import proto1_to_proto2
16+
from celery.worker.strategy import proto1_to_proto2, hybrid_to_proto2
1717

1818

1919
class test_proto1_to_proto2:
@@ -268,3 +268,25 @@ def failed():
268268
)
269269
task_message_handler(C.message, None, None, None, None)
270270
_MyRequest.assert_called()
271+
272+
273+
class test_hybrid_to_proto2:
274+
275+
def setup(self):
276+
self.message = Mock(name='message')
277+
self.body = {
278+
'args': (1,),
279+
'kwargs': {'foo': 'baz'},
280+
'utc': False,
281+
'taskset': '123',
282+
}
283+
284+
def test_retries_default_value(self):
285+
_, headers, _, _ = hybrid_to_proto2(self.message, self.body)
286+
assert headers.get('retries') == 0
287+
288+
def test_retries_custom_value(self):
289+
_custom_value = 3
290+
self.body['retries'] = _custom_value
291+
_, headers, _, _ = hybrid_to_proto2(self.message, self.body)
292+
assert headers.get('retries') == _custom_value

0 commit comments

Comments
 (0)