| 1 |
# Create your tasks here |
| 2 |
|
| 3 |
from demoapp.models import Widget |
| 4 |
|
| 5 |
from celery import shared_task |
| 6 |
|
| 7 |
|
| 8 |
@shared_task |
| 9 |
def add(x, y): |
| 10 |
return x + y |
| 11 |
|
| 12 |
|
| 13 |
@shared_task |
| 14 |
def mul(x, y): |
| 15 |
return x * y |
| 16 |
|
| 17 |
|
| 18 |
@shared_task |
| 19 |
def xsum(numbers): |
| 20 |
return sum(numbers) |
| 21 |
|
| 22 |
|
| 23 |
@shared_task |
| 24 |
def count_widgets(): |
| 25 |
return Widget.objects.count() |
| 26 |
|
| 27 |
|
| 28 |
@shared_task |
| 29 |
def rename_widget(widget_id, name): |
| 30 |
w = Widget.objects.get(id=widget_id) |
| 31 |
w.name = name |
| 32 |
w.save() |
| 33 |
|
| 34 |
|
| 35 |
@shared_task( |
| 36 |
bind=True, |
| 37 |
autoretry_for=(Exception,), |
| 38 |
retry_kwargs={"max_retries": 2, "countdown": 10 * 60}, # retry up to 2 times with 10 minutes between retries |
| 39 |
) |
| 40 |
def error_task(self): |
| 41 |
raise Exception("Test error") |
| 42 |
|
| 43 |
|
| 44 |
@shared_task( |
| 45 |
bind=True, |
| 46 |
autoretry_for=(Exception,), |
| 47 |
retry_backoff=5, # Factor in seconds (first retry: 5s, second: 10s, third: 20s, etc.) |
| 48 |
retry_jitter=False, # Set False to disable randomization (use exact values: 5s, 10s, 20s) |
| 49 |
retry_kwargs={"max_retries": 3}, |
| 50 |
) |
| 51 |
def error_backoff_test(self): |
| 52 |
raise Exception("Test error") |