Adam Everington
11/12/2021, 7:36 AMdef my_state_handler(task,new_state,old_state)->None:
send_message(f'the following task failed {task.name} within this flow: {[GET FLOW NAME HERE}]')
@task(on_failed=my_state_handler)
def my_task():
....
Had a look through the task class on github and I can see it's passed to various methods but couldn't see a property it persisted inAnna Geller
prefect.context["flow_name"]
More on that here: https://docs.prefect.io/api/latest/utilities/context.html#context-2Anna Geller
from prefect import Flow, task
import prefect
def send_post(task, state):
msg = f'Task {task.name} failed in the flow {prefect.context["flow_name"]}'
print(msg)
@task(on_failure=send_post)
def my_task():
raise Exception("Nope")
with Flow("fail-it") as flow:
my_task()
if __name__ == "__main__":
flow.run()
Adam Everington
11/12/2021, 9:29 AMAnna Geller