Hi all. Is there any chance state handlers for sla...
# prefect-community
k
Hi all. Is there any chance state handlers for slack notifications will make it into prefect 2 as task and flow arguments? I’d built a pretty extensive one for myself in prefect 1 and would like to use the same for prefect 2
1
r
Hey Kiran!! You can return state and take action on that state in 2.0 as well! I would recommend taking a look at this discourse article: https://discourse.prefect.io/t/how-to-take-action-on-a-state-change-of-a-task-run-task-level-state-handler/82/12 As well as the notification docs: https://docs.prefect.io/ui/notifications/?h=notifications#notifications
k
Thanks @Rob Freedy. Do you know how to get the final state from a flow to put it in a slack notification? I’m trying to figure out how to do this with a flow and ended up putting the main flow into final flow that returns the main flow’s state. But it doesn’t work for failures in the main flow
e.g.,
Copy code
@flow(retries=2, retry_delay_seconds=60)
def final_flow(main_flow) -> None:
    slack_webhook_block = SlackWebhook.load("slack")
    logger = get_run_logger()
    state = main_flow()
    final_state = state[-1]
    if final_state.is_failed():
        <http://logger.info|logger.info>(final_state.is_failed())
        slack_webhook_block.notify("failed")
    elif final_state.is_completed():
        <http://logger.info|logger.info>(final_state.is_completed())
        slack_webhook_block.notify("completed")

    return
The reason I want to do custom notifications like with Prefect 1 is because I tag various slack user IDs depending on the flow’s final state and I use emojis to be able to scroll through the notifications channel and quickly/easily see any issues
r
k
Thanks Rob. I’ll check it out. I’ve basically got around it for now like this:
Copy code
@flow
def final_flow():
    try:
        main_flow()
    except Exception:
        fail_notify_flow("#prefect", "insert_flow_name")
    else:
        success_notify_flow("#prefect", "insert_flow_name")


if __name__ == "__main__":
    final_flow()