https://prefect.io logo
a

Angel Acosta

08/16/2022, 8:07 PM
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()
check this page https://orion-docs.prefect.io/concepts/flows/#return-a-manual-state
🙏 1
a

Angel Acosta

08/16/2022, 9:05 PM
Thank you! That's exactly what I was looking for!
🙌 1
2 Views