Created
October 26, 2012 13:20
-
-
Save juliengrenier/3958777 to your computer and use it in GitHub Desktop.
celery retry_task decorator. Depends on django transaction.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def retry_task(name, max_retries=3, ignore_result=True, queue="celery", countdown=10, exceptions=[]): | |
""" | |
This decorator allows you to retry a celery task if it raised an exception of type defined in <exceptions>. | |
Tasks are also wrapped by a commit_on_success decorator to avoid incomplete data in the database. | |
arguments are : | |
name: The name of the task | |
max_retries: The number of retries before giving up [default: 3] | |
ignore_result: Should celery ignores the result [default: True] | |
queue: The queue name [default: "celery"] | |
countdown: The ellapsed time between retries [default: 10 seconds] | |
exceptions [mandatory]: An array of exception type.If an exception of those type is raised, we will retry the task, any other exception will be raise automatically. | |
""" | |
if not exceptions: | |
raise ValueError("You must define a list of retriable exceptions.") | |
def decorator(func): | |
@task(name=name, max_retries=max_retries, ignore_result=ignore_result, queue=queue) | |
@transaction.commit_on_success | |
def wrapper(*args, **kwargs): | |
try: | |
return func(*args, **kwargs) | |
except Exception as exc: | |
for exception_type in exceptions: | |
if isinstance(exc, exception_type): #This mean we should retry | |
return wrapper.retry(countdown=countdown, exc=exc) | |
raise #This is an unexception error, we should not retry | |
return wrapper | |
return decorator |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment