Hey All, I have a task that makes a request to an ...
# ask-community
t
Hey All, I have a task that makes a request to an API and I only want to retry the task if I receive a certain status code from the response (eg. 500). Status codes like 404 I'd like to fail the task and anything else return the result. I'd still like to have the max retries and retry delay but I can't seem to figure out how to fail the task without it retrying! Any ideas?
k
Hey @Thomas Weatherston, you’ll need to raise the signal like this . For your case you’ll look at the response code and then raise the appropriate signal
t
Hey @Kevin Kho thanks for the response! I've been attempting to use the FAIL signal to fail the task but it still seems to get retried. A small example:
Copy code
from prefect import Flow, task
from prefect.engine import signals
from datetime import timedelta


@task(max_retries=5, retry_delay=timedelta(seconds=1))
def test():
    raise signals.FAIL()


with Flow("test-flow") as f:
    test()

f.run()
k
I see. Do you need it in a FAILURE state? Because you can SKIP it but that would be a SUCCESS state
t
Yeah ideally I'd like it to fail without retrying
j
Hi @Thomas Weatherston - I believe we don't currently have a signal/state that would separate out a fail-no retry and fail-with retry (though I'm happy to open a ticket to suggest it as I think it could be helpful!). For your flow, would a different task failing work? You could fail your first task on the status you want retries on and then for other statuses pass a result to a downstream task to raise the fail signal.
t
Hey @Jenny! Ah yes, I reckon I could I could restructure our tasks to work like that, thanks very much 🙂 (a fail-no retry signal would also be awesome, if you can find time to add it in the future!)
j
Great. @Marvin open "Add a fail with no retry signal/state"
l
Hey @Thomas Weatherston if you have no cleanup tasks and want the whole flow to just fail without the retry, you should have a look at the ENDRUN signal. Code example posted in the github issue. Fail with no retry signal would be more versatile solution though 🙂
👍 3