MCPcopy
hub / github.com/celery/celery / add_autoretry_behaviour

Function add_autoretry_behaviour

celery/app/autoretry.py:8–66  ·  view source on GitHub ↗

Wrap task's `run` method with auto-retry functionality.

(task, **options)

Source from the content-addressed store, hash-verified

6
7
8def add_autoretry_behaviour(task, **options):
9 """Wrap task's `run` method with auto-retry functionality."""
10 autoretry_for = tuple(
11 options.get('autoretry_for',
12 getattr(task, 'autoretry_for', ()))
13 )
14 dont_autoretry_for = tuple(
15 options.get('dont_autoretry_for',
16 getattr(task, 'dont_autoretry_for', ()))
17 )
18 retry_kwargs = options.get(
19 'retry_kwargs', getattr(task, 'retry_kwargs', {})
20 )
21 retry_backoff = float(
22 options.get('retry_backoff',
23 getattr(task, 'retry_backoff', False))
24 )
25 retry_backoff_max = int(
26 options.get('retry_backoff_max',
27 getattr(task, 'retry_backoff_max', 600))
28 )
29 retry_jitter = options.get(
30 'retry_jitter', getattr(task, 'retry_jitter', True)
31 )
32
33 if autoretry_for and not hasattr(task, '_orig_run'):
34
35 @wraps(task.run)
36 def run(*args, **kwargs):
37 try:
38 return task._orig_run(*args, **kwargs)
39 except Ignore:
40 # If Ignore signal occurs task shouldn't be retried,
41 # even if it suits autoretry_for list
42 raise
43 except Retry:
44 raise
45 except dont_autoretry_for:
46 raise
47 except autoretry_for as exc:
48 if retry_backoff:
49 retry_kwargs['countdown'] = \
50 get_exponential_backoff_interval(
51 factor=int(max(1.0, retry_backoff)),
52 retries=task.request.retries,
53 maximum=retry_backoff_max,
54 full_jitter=retry_jitter)
55 # Override max_retries
56 if hasattr(task, 'override_max_retries'):
57 retry_kwargs['max_retries'] = getattr(task,
58 'override_max_retries',
59 task.max_retries)
60 ret = task.retry(exc=exc, **retry_kwargs)
61 # Stop propagation
62 if hasattr(task, 'override_max_retries'):
63 delattr(task, 'override_max_retries')
64 raise ret
65

Callers 3

registerMethod · 0.90
_task_from_funMethod · 0.85
register_taskMethod · 0.85

Calls 1

getMethod · 0.45

Tested by

no test coverage detected