https://prefect.io logo
Title
s

Sean Conroy

11/30/2022, 9:34 PM
Following up on Kevin's question above, what is the equivalent for the
is_successful()
method from Prefect 1 for Prefect 2? In other words, is testing the state name = 'Completed' the best way to check for success?
myflow_state = my_flow(return_state=True)

# Test if state is successfull:
if not myflow_state.name == 'Completed':
    raise Exception("myflow failed")
1
b

Bianca Hoch

11/30/2022, 9:41 PM
Hello Sean, here's an example you can repurpose for what you're trying to do. This example triggers a notification block if the flow reaches a completed state (aka successful state)
from prefect import task, flow
from prefect.blocks.notifications import SlackWebhook
from prefect.orion.schemas.states import StateType

#Load your desired block, this example uses a slack notification block.
def notify_function():
    slack_webhook_block = SlackWebhook.load("my-test-notification")
    #This is the message you want to send to slack
    slack_webhook_block.notify("Hello from Prefect!")

#A simple flow
@flow
def my_flow():
   pass

if __name__ == "__main__":
    #Store the state of your flow
    state = my_flow(return_state=True)
    '''This if statement checks for a specific flow state.
    If the condition is satisfied, the block is envoked.
    State type can be set to any available state type: <https://docs.prefect.io/concepts/states/#state-types''>'
    if state.type == (StateType.COMPLETED):
        notify_function()
1
s

Sean Conroy

11/30/2022, 9:50 PM
Ok perfect - i will give that a shot - thanks!
🙌 1