Skip to content

Commit

Permalink
Prefer retry method from the task decorator if no retry method is given
Browse files Browse the repository at this point in the history
to RetryException.

This matches the already documented behavior.
  • Loading branch information
thomasst committed Jul 29, 2024
1 parent 423d8a1 commit 2ea5a33
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 0 deletions.
4 changes: 4 additions & 0 deletions tasktiger/worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -771,8 +771,12 @@ def _mark_done() -> None:
has_job_timeout = True

if execution and execution.get("retry"):
# Prefer retry method from the execution, then the task, then
# default.
if "retry_method" in execution:
retry_func, retry_args = execution["retry_method"]
elif task.retry_method:
retry_func, retry_args = task.retry_method
else:
# We expect the serialized method here.
retry_func, retry_args = serialize_retry_method(
Expand Down
5 changes: 5 additions & 0 deletions tests/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ def retry_task_2():
raise RetryException(method=fixed(DELAY, 1), log_error=False)


@tiger.task(retry_method=fixed(DELAY, 1))
def retry_task_3():
raise RetryException(log_error=False)


def verify_current_task():
with redis.Redis(
host=REDIS_HOST, db=TEST_DB, decode_responses=True
Expand Down
18 changes: 18 additions & 0 deletions tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
non_batch_task,
retry_task,
retry_task_2,
retry_task_3,
simple_task,
sleep_task,
task_on_other_queue,
Expand Down Expand Up @@ -617,6 +618,23 @@ def test_retry_exception_2(self):

pytest.raises(TaskNotFound, task.n_executions)

def test_retry_exception_3(self):
task = self.tiger.delay(retry_task_3)
self._ensure_queues(queued={"default": 1})
assert task.n_executions() == 0

Worker(self.tiger).run(once=True)
self._ensure_queues(scheduled={"default": 1})
assert task.n_executions() == 1

time.sleep(DELAY)

Worker(self.tiger).run(once=True)
Worker(self.tiger).run(once=True)
self._ensure_queues()

pytest.raises(TaskNotFound, task.n_executions)

@pytest.mark.parametrize("count", [1, 3, 7])
def test_retry_executions_count(self, count):
task = self.tiger.delay(exception_task, retry_method=fixed(DELAY, 20))
Expand Down

0 comments on commit 2ea5a33

Please sign in to comment.