diff --git a/clients/python/src/taskbroker_client/worker/workerchild.py b/clients/python/src/taskbroker_client/worker/workerchild.py index 7ca5ee9e..d0142f4f 100644 --- a/clients/python/src/taskbroker_client/worker/workerchild.py +++ b/clients/python/src/taskbroker_client/worker/workerchild.py @@ -861,7 +861,7 @@ def _place_processing_result( # Don't send it on every task as this codepath is relatively # unoptimized on the broker side. max_attempts=( - task_func.retry._times + 1 + task_func.retry._times if task_func.retry and next_state == TASK_ACTIVATION_STATUS_RETRY else None ), diff --git a/clients/python/tests/worker/test_worker.py b/clients/python/tests/worker/test_worker.py index 427a56eb..4906d16e 100644 --- a/clients/python/tests/worker/test_worker.py +++ b/clients/python/tests/worker/test_worker.py @@ -40,6 +40,7 @@ PushTaskResponse, RetryState, TaskActivation, + TaskActivationStatus, ) from sentry_sdk.crons import MonitorStatus from sentry_sdk.integrations.logging import LoggingIntegration @@ -47,8 +48,8 @@ from taskbroker_client.canary import CANARY_TASK_NAME from taskbroker_client.constants import INTERNAL_NAMESPACE, CompressionType -from taskbroker_client.retry import NoRetriesRemainingError -from taskbroker_client.state import current_task +from taskbroker_client.retry import NoRetriesRemainingError, retry_task +from taskbroker_client.state import clear_current_task, current_task, set_current_task from taskbroker_client.types import InflightTaskActivation, ProcessingResult from taskbroker_client.worker.childtiming import ( KIND_BUSY, @@ -2582,6 +2583,66 @@ def test_child_process_retry_task_max_attempts( assert extra["retry_max_attempts"] == 3 +def test_child_process_retry_chain_last_run_has_no_retries_remaining() -> None: + # Run examples.will_retry (Retry(times=3)) until it stops retrying, applying + # the broker's retry step between runs: store the max_attempts the worker + # sent and advance attempts. + retry_state = RetryState(attempts=0, max_attempts=3) + retries_remaining: list[bool] = [] + statuses: list[TaskActivationStatus.ValueType] = [] + for _ in range(5): + activation = TaskActivation( + id=f"chain-{retry_state.attempts}", + taskname="examples.will_retry", + namespace="examples", + parameters_bytes=msgpack.packb({"args": ["raise"], "kwargs": {}}, use_bin_type=True), + processing_deadline_duration=100000, + retry_state=retry_state, + ) + set_current_task(activation) + state = current_task() + assert state is not None + retries_remaining.append(state.retries_remaining) + if not state.retries_remaining: + with pytest.raises(NoRetriesRemainingError): + retry_task() + clear_current_task() + + todo: queue.Queue[InflightTaskActivation] = queue.Queue() + processed: queue.Queue[ProcessingResult] = queue.Queue() + todo.put( + InflightTaskActivation( + host="localhost:50051", receive_timestamp=0, activation=activation + ) + ) + child_process( + "examples.app:app", + todo, + processed, + Event(), + max_task_count=1, + processing_pool_name="test", + process_type="fork", + skip_awaiting_futures=False, + future_checking_frequency=0.1, + ) + result = processed.get() + statuses.append(result.status) + if result.status != TASK_ACTIVATION_STATUS_RETRY: + break + assert result.max_attempts is not None + retry_state = RetryState( + attempts=retry_state.attempts + 1, max_attempts=result.max_attempts + ) + + assert statuses == [ + TASK_ACTIVATION_STATUS_RETRY, + TASK_ACTIVATION_STATUS_RETRY, + TASK_ACTIVATION_STATUS_FAILURE, + ] + assert retries_remaining == [True, True, False] + + def test_child_process_failure_task() -> None: todo: queue.Queue[InflightTaskActivation] = queue.Queue() processed: queue.Queue[ProcessingResult] = queue.Queue()