Skip to content

Commit

Permalink
Prefer retry method from the task decorator if no retry method is giv…
Browse files Browse the repository at this point in the history
…en to RetryException (#356)

This matches the already documented behavior.
  • Loading branch information
thomasst authored Jul 29, 2024
1 parent 423d8a1 commit 625e2ec
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Version 0.21.0

* When raising `RetryException` with no `method`, use task decorator retry method if set ([356](https://github.com/closeio/tasktiger/pull/356))

## Version 0.20.0

* Add `tiger.get_sizes_for_queues_and_states` ([352](https://github.com/closeio/tasktiger/pull/352))
Expand Down
2 changes: 1 addition & 1 deletion tasktiger/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from .tasktiger import TaskTiger, run_worker
from .worker import Worker

__version__ = "0.20.0"
__version__ = "0.21.0"
__all__ = [
"TaskTiger",
"Worker",
Expand Down
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 625e2ec

Please sign in to comment.