0
0
Djangoframework~20 mins

Task retry and error handling in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Celery Retry Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when a Celery task raises an exception without retry?
Consider a Django project using Celery for background tasks. If a task raises an exception and does not explicitly retry, what will be the outcome?
Django
from celery import shared_task

@shared_task
def send_email(user_id):
    user = User.objects.get(id=user_id)
    if not user.email:
        raise ValueError('No email found')
    # send email logic here
    return 'Email sent'
AThe task will automatically retry 3 times before failing.
BThe task will fail and be marked as failed in Celery without retrying.
CThe task will silently ignore the exception and mark as successful.
DThe task will be retried indefinitely until it succeeds.
Attempts:
2 left
💡 Hint
Think about default Celery behavior when no retry is specified.
📝 Syntax
intermediate
2:00remaining
Which option correctly retries a Celery task on failure with exponential backoff?
You want a Celery task to retry up to 3 times with exponential backoff starting at 10 seconds. Which code snippet correctly implements this?
Django
from celery import shared_task
from celery.exceptions import MaxRetriesExceededError

@shared_task(bind=True)
def fetch_data(self):
    try:
        # code that might fail
        pass
    except Exception as exc:
        # retry logic here
        pass
Aself.retry(exc=exc, countdown=3, max_retries=10)
Bself.retry(exc=exc, countdown=10 * self.request.retries, max_retries=3)
Cself.retry(exc=exc, countdown=10 ** self.request.retries, max_retries=3)
Dself.retry(exc=exc, countdown=10, max_retries=3)
Attempts:
2 left
💡 Hint
Exponential backoff means the wait time grows exponentially with retries.
🔧 Debug
advanced
2:00remaining
Why does this Celery task retry not work as expected?
This task is supposed to retry on failure but never retries. What is the cause?
Django
from celery import shared_task

@shared_task(bind=True)
def process_order(self, order_id):
    try:
        order = Order.objects.get(id=order_id)
        # process order
    except Order.DoesNotExist as e:
        self.retry(exc=e, countdown=5, max_retries=2)
        raise
AThe task is missing the bind=True decorator, so self.retry is undefined.
BThe max_retries parameter is invalid and causes retry to be ignored.
CThe countdown value must be a string, not an integer.
DThe retry is called inside except but the exception is not re-raised or returned, so task ends successfully.
Attempts:
2 left
💡 Hint
Check what happens after calling self.retry inside except block.
state_output
advanced
2:00remaining
What is the state of a Celery task after max retries are exceeded?
A Celery task is configured to retry 2 times on failure. After the third failure, what is the task state in the backend?
Django
from celery import shared_task

@shared_task(bind=True, max_retries=2)
def unreliable_task(self):
    raise RuntimeError('Fail')
AThe task state is 'FAILURE' and the exception info is stored.
BThe task state is 'RETRY' indefinitely.
CThe task state is 'SUCCESS' despite failures.
DThe task state is 'PENDING' waiting for manual retry.
Attempts:
2 left
💡 Hint
What happens when retries are exhausted in Celery?
🧠 Conceptual
expert
3:00remaining
How to implement custom error handling and retry logic in Django with Celery?
You want to retry a Celery task only for network-related errors but fail immediately for others. Which approach correctly implements this selective retry?
Django
from celery import shared_task
import requests

@shared_task(bind=True, max_retries=5)
def fetch_api(self, url):
    try:
        response = requests.get(url)
        response.raise_for_status()
        return response.json()
    except Exception as exc:
        # retry only for network errors
        pass
ACheck if exc is requests.exceptions.RequestException, then call self.retry(exc=exc), else raise exc.
BAlways call self.retry(exc=exc) regardless of exception type.
CUse a try-except with a generic except and ignore all exceptions silently.
DCatch only requests.exceptions.HTTPError and retry, raise others.
Attempts:
2 left
💡 Hint
Selective retry means retry only for specific exception types.