Jason Wiener
10/15/2024, 6:50 PMMarvin
10/15/2024, 6:51 PMJason Wiener
10/15/2024, 6:51 PMMarvin
10/15/2024, 6:53 PMretry_condition_fn
, which allows you to define logic that determines whether a task should retry:
python
import httpx
from prefect import flow, task
def retry_handler(task, task_run, state) -> bool:
"""This is a custom retry handler to handle when we want to retry a task"""
try:
# Attempt to get the result of the task
state.result()
except httpx.HTTPStatusError as exc:
# Retry on any HTTP status code that is not 401 or 404
do_not_retry_on_these_codes = [401, 404]
return exc.response.status_code not in do_not_retry_on_these_codes
except httpx.ConnectError:
# Do not retry
return False
except:
# For any other exception, retry
return True
@task(retries=3, retry_condition_fn=retry_handler)
def my_task():
return 1 / 0 # This will cause an exception and trigger retries
@flow
def my_flow():
my_task()
if __name__ == "__main__":
my_flow()
In this example, the retry_handler
can be customized further to maintain a retry count using external storage or logging mechanisms, as Prefect doesn't inherently provide a direct retry count during execution.
For additional context, you might want to check out issues and discussions related to retry functionality in Prefect GitHub repo issue 6787. It discusses the need for better retry notifications and handling.
Feel free to ask if you need further assistance!