Add exception filters to retries similar to retry_on
in v1
#5611
Labels
enhancement
An improvement of an existing feature
status:duplicate
This issue already exists
v1-parity
A v1 feature under consideration for implementation in v2
TL;DR
Add a similar capability to
retry_on
functionality to allow retries on specific exception typesNeeded for v1 parity:
prefect/src/prefect/core/task.py
Lines 246 to 248 in e6bd04a
Opened from the Prefect Public Slack Community
mike399: Hi again, we have a requirement to handle two classes of errors: retryable errors and non-retryable errors. I think I saw some examples how this might be achieved somewhere but cant spot them for Prefect 2. In the non-retryable scenario we'd want the task state to go immediately to Failed (bypassing any retry settings configured on the task decorator)
anna: In Prefect 2.0 you can do:
For retriable errors:
For non-retriable errors (no retries):
mike399: But if I have a single task that can throw either type of error? Is there a context object that can be manipulated, I was thinking something similar to this?
anna: Generally speaking, there is no need to use try-except in a Prefect flow. That's why you use Prefect so that you don't need to worry about all the different types of exceptions that might need to be caught.
The
retries
mean a maximum number of retries:anna: But if you want e.g. to do something if the task throws a specific exception, you could do it within a
@flow
by acting on thetask.wait().result()
which would be an exception in such case - LMK if you need an exampleanna:
anna: this way you explicitly decide when the task run is supposed to be considered as
Failed
based on the API response - raising any exception will automatically cause the task run to be considered Failed without having to manually set that using:mike399: Is overriding the task run state an option, it would allow us to benefit from the task level retry configuration - but also skip that when needed. A common scenario would be distinguishing between transient system failures (e.g. a service unavailable) vs permanent errors arising from unexpected application state (e.g. once an object has been deleted there's no point retrying any attempt to modify it)
anna: Good question. You can return any State in your task to influence that: https://orion-docs.prefect.io/concepts/states/
anna: Basically, instead of:
you do:
mike399: Thanks Anna, will give that a try
anna: You could even do it on a flow level - having normal retries on a task level, but if the task result returns a specific value, you raise an exception on a flow level and end the flow run immediately - just mentioning this because it's also a valid option:
mike399: This one worked (no retries)
We'd like to make this as simple as possible (to reduce learning curve and help with developer experience), so I think we can extend the above and use an inner decorated function to hide all the details
anna: Sure, you can customize it as you wish, it's pretty much just Python so the sky is the limit! 🙂
mike399: I hit a probem with this...if the return is
Then the retry is skipped BUT the Flow Run ends up in a Completed state - tracing through this occurs because the StateType.FAILED is not a State (so Prefect defaults to Completed for "python object returns")
and if we return
Then client.py: propose_state appears to do a call to set_task_run_state, but the proposed state is rejected and we go into a retry loop
mike399: This is getting a little too involved, most of our workflows tend to be idempotent system operations, so we may be able to handle this in a different way (checking result payload in the flow as you indicated). Would be nice if Prefect had additional Exception filters it could apply for retry (similar to the tenacity library)
anna: Thanks for the suggestion, I'll pass it on to the product team
anna: <@ULVA73B9P> open “Orion: feature request to add Exception filters applicable for retries”
Original thread can be found here.
The text was updated successfully, but these errors were encountered: