https://prefect.io logo
o

Olivér Atanaszov

12/08/2022, 11:27 AM
Hi, what is the recommended way of adding a handle for flow run interruption in Prefect 1.0 (similarly to
on_handle
argument of
Flow
)?
1
1️⃣ 1
a

Anna Geller

12/08/2022, 11:58 AM
is this a test? 😅 afaik, there's no on_handle, do you mean on_failure? if so, anytime you call a task or a subflow (without task runner), you can pass retiurn_state=True to get state and then you can act on it e.g. if state.name == "Failed" (or "Completed") and triggering downstream actions just with if/else
and for main flows, we are rolling out Automations to take action on various states of a flow run https://prefect-community.slack.com/archives/CKNSX5WG3/p1670444624688239
o

Olivér Atanaszov

12/08/2022, 12:00 PM
@Anna Geller yes, I meant
on_failure
sry
🙌 1
a

Anna Geller

12/08/2022, 12:08 PM
example:
Copy code
from prefect import flow, task
import random


@task
def on_failure_task():
    print("Taking action on failure here")


@task
def on_success_task():
    print("Taking action on success here")


@flow
def bad_subflow():
    if random.random() > 0.5:
        raise ValueError("Non-deterministic error has occured.")


@flow(log_prints=True, name="Conditional subflows")
def main():
    state = bad_subflow(return_state=True)
    if state.name == "Failed":
        print("Subflow failed! Run on_failure_task")
        on_failure_task()
    else:
        print("Subflow succeeded! Run on_success_task")
        on_success_task()


if __name__ == "__main__":
    main()
o

Olivér Atanaszov

12/08/2022, 12:09 PM
ah ok, this is Prefect 2.0
a

Anna Geller

12/08/2022, 12:10 PM
yes, I thought you made a typo there because the question suggested you know how to solve it in v1 but not how to migrate this to v2
generally, migration is highly recommended and encouraged
o

Olivér Atanaszov

12/08/2022, 12:12 PM
thanks Anna
a

Anna Geller

12/08/2022, 12:15 PM
Overall in V1, there are flow level state handlers to solve the same problem, there are a couple of examples on Discourse for V1 if you check the tag state handlers
2 Views