Hello, I have a quick question about how to manually trigger a failed task state in code using 2.0. Essentially I am looking at an http response and if != 200 then I want to fail the task so that I can receive an alert. Not sure if this is the most elegant solution but I would appreciate any suggestions. Thanks!
✅ 1
a
Anna Geller
08/16/2022, 8:57 PM
you can return the state directly:
Copy code
from prefect import task, flow
from prefect.orion.schemas.states import Completed, Failed
@task
def always_fails_task():
raise ValueError("I fail successfully")
@task
def always_succeeds_task():
print("I'm fail safe!")
return "success"
@flow
def always_succeeds_flow():
x = always_fails_task.submit()
y = always_succeeds_task.submit()
if y.result() == "success":
return Completed(message="I am happy with this result")
else:
return Failed(message="How did this happen!?")
if __name__ == "__main__":
always_succeeds_flow()
Bring your towel and join one of the fastest growing data communities. Welcome to our second-generation open source orchestration platform, a completely rethought approach to dataflow automation.