Retry a Huey task when something fails

768 Views Asked by At

Hi we are using Python Huey https://pypi.org/project/huey/1.1.0/ on a django project for running our background task, we are use it as a decorator @task() on some functions. To be more precise we are importing the library the following way:

from huey.contrib.djhuey import task

So, on our functions, that are executed by this task, in some cases we have database connections. If for some reason this task fails, for example, today we had a database issue that the connections to the database were closing (this is an issue that we have to figure out why this connections were closing). As you may imagine the task doesn't finish it's job due to the database connection.

My question is: Is there any way, that we can configure this Huey task to retry the execution. Let's say, retry 5 times with 2 seconds between each retry ?

1

There are 1 best solutions below

0
On

Yes you can. The huey documation explains this:

import random

@huey.task(retries=2)  # Retry the task up to 2 times.
def flaky_task():
    if random.randint(0, 1) == 0:
        raise Exception('failing!')
    return 'OK'

Or setting a custom retry delay:

@huey.task(retries=2, retry_delay=10)
def flaky_task():
    # ...