def handle_failure(task_func, task, err):
    """Decide whether to retry a failed task.
    """
    if task.retries >= task_func.retries or not task_func.match_exceptions(err
        ):
        task.failure(err)
    else:
        task.failure(err, retry=True)
