-
Notifications
You must be signed in to change notification settings - Fork 3
/
tasks.py
68 lines (51 loc) · 2.4 KB
/
tasks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
"""
Celery tasks documentation:
https://docs.celeryq.dev/en/stable/userguide/tasks.html
"""
import celery
from celery import shared_task
class BaseTaskWithRetry(celery.Task):
"""
Automatically retry task in case of failure (up to 3 times). This class
is intended to be used as a base class for other tasks that need to be
retried in case of failure.
Attributes:
autoretry_for (tuple): The list of exceptions that should be caught and retried.
retry_kwargs (dict): The maximum number of retries this task can have.
retry_backoff (int): The time in seconds to wait before retrying the task.
retry_jitter (bool): Whether to apply exponential backoff when retrying.
"""
# The list of exceptions that should be caught and retried
autoretry_for = (Exception, KeyError)
# The maximum number of retries this task can have
retry_kwargs = {"max_retries": 3}
# The time in seconds to wait before retrying the task
retry_backoff = 5
# Whether to apply exponential backoff when retrying:
# When you build a custom retry strategy for your Celery task
# (which needs to send a request to another service), you should add
# some randomness to the delay calculation to prevent all tasks from
# being executed simultaneously resulting in a thundering herd.
retry_jitter = True
@shared_task(bind=True, base=BaseTaskWithRetry)
def test_task(self) -> None:
"""
TODO 🚫 After testing the task, delete this function, the view, and the route.
This task can be executed periodically using Celery Beat, and/or on demand.
Parameters:
self: The task instance itself.
Retry strategy
This task uses the BaseTaskWithRetry class defined above, so it will be
retried up to 3 times if it fails (by default). The task will be retried
after 5 seconds, and the retry time will be increased exponentially.
To call this task on demand
1. Start a Celery worker with 'poetry run worker'
2. Call the task from a View or another function as test_task.delay()'
To set the periodic task
1. Go the Django Admin and add a new Periodic Task.
2. Start a Celery worker with 'poetry run worker'
3. Start Celery Beat with 'poetry run beat'
"""
# 👇 To test the retries uncomment the following line:
# raise KeyError("This is a test error")
print("Hello World from Celery")