Exemplo n.º 1
0
def retry_task(
        task: Task,
        exception: Optional[Exception] = None,
        max_attempts: Optional[int] = None,
) -> None:
    """
    Retry celery tasks.

    Retries the specified task using a "backing off countdown",
    meaning that the interval between retries grows exponentially
    with every retry.

    :param task: The task to retry.
    :param exception: Optionally, the exception that caused the retry.
    :param max_attempts: Optionally, number of maximum attempts to retry the task.
    """

    def backoff(attempts: int) -> int:
        return 5 * (2 ** attempts)

    kwargs: dict = {
        "countdown": backoff(task.request.retries),
    }

    if exception:
        kwargs["exc"] = exception

    if not max_attempts or task.request.retries < max_attempts:
        raise task.retry(**kwargs)
Exemplo n.º 2
0
 def wrapper(task: Task, *args):
     task.max_retries = times
     try:
         result = func(task, *args)
     except Exception as exc:
         raise task.retry(exc=exc, countdown=5)
     else:
         return result