Hey everyone, I am working on some flows using pr...
# prefect-getting-started
d
Hey everyone, I am working on some flows using prefect 2.7.12, specifically sending an email when a flow fails. I'm having trouble accessing the name of the flow run instance (adjective, noun). I would like to be able to include this information in the email. I've tried a handful of things including: flow_run_name =context['flow_run_name'] flow_run_name = prefect.context.get("flow_run_name") flow_run_name = prefect.context.flow_run.name flow_name = get_flow_name() and probably a few that I am forgetting. Does anyone have any suggestions or advise? Anything you can provide is much appreciated!
1
j
hey, if you are using prefect cloud it's really easy to do this with an automation if you want to do it locally you may need to upgrade your version but you can do
Copy code
from prefect import flow


def print_name(flow, flow_run, state):
    print(flow_run.name)


@flow(on_failure=[print_name])
def failing_flow():
    raise ValueError("this failed")

if __name__ == '__main__':
    failing_flow()
which gives something like:
Copy code
/Users/jakekaplan/opt/anaconda3/envs/demo-flows/bin/python /Users/jakekaplan/PycharmProjects/demo-flows/my_flow.py 
14:31:59.793 | INFO    | prefect.engine - Created flow run 'graceful-mandrill' for flow 'failing-flow'
14:31:59.795 | INFO    | Flow run 'graceful-mandrill' - View at <https://app.prefect.cloud/account/3cf6b38f-5244-474a-9554-302144506e43/workspace/ce8b1412-01b7-4700-a508-8dbd1f43f623/flow-runs/flow-run/9a665501-d619-435c-8080-f474b10466a0>
14:32:02.146 | ERROR   | Flow run 'graceful-mandrill' - Encountered exception during execution:
Traceback (most recent call last):
  File "/Users/jakekaplan/opt/anaconda3/envs/demo-flows/lib/python3.10/site-packages/prefect/engine.py", line 750, in orchestrate_flow_run
    result = await flow_call.aresult()
  File "/Users/jakekaplan/opt/anaconda3/envs/demo-flows/lib/python3.10/site-packages/prefect/_internal/concurrency/calls.py", line 181, in aresult
    return await asyncio.wrap_future(self.future)
  File "/Users/jakekaplan/opt/anaconda3/envs/demo-flows/lib/python3.10/site-packages/prefect/_internal/concurrency/calls.py", line 194, in _run_sync
    result = self.fn(*self.args, **self.kwargs)
  File "/Users/jakekaplan/PycharmProjects/demo-flows/my_flow.py", line 10, in failing_flow
    raise ValueError("this failed")
ValueError: this failed
14:32:02.296 | INFO    | Flow run 'graceful-mandrill' - Running hook 'print_name' in response to entering state 'Failed'
14:32:02.297 | INFO    | Flow run 'graceful-mandrill' - Hook 'print_name' finished running successfully
14:32:02.298 | ERROR   | Flow run 'graceful-mandrill' - Finished in state Failed('Flow run encountered an exception. ValueError: this failed\n')
graceful-mandrill
d
We are not on the cloud, and unfortunately we can't upgrade at the time because of the time it would take to rebuild our flows. I'll check this out for the future though! Thanks for your response.
j
Ah understood. I think this needs to called from inside of the flow run itself, but you can do the following on 2.7.12
Copy code
from prefect import flow
from prefect.context import get_run_context


@flow
def failing_flow():
    ctx = get_run_context()
    print(ctx.flow_run.name)

if __name__ == '__main__':
    failing_flow()
Copy code
/Users/jakekaplan/opt/anaconda3/envs/demo-flows/bin/python /Users/jakekaplan/PycharmProjects/demo-flows/my_flow.py 
14:39:34.777 | INFO    | prefect.engine - Created flow run 'teal-degu' for flow 'failing-flow'
teal-degu
14:39:35.616 | INFO    | Flow run 'teal-degu' - Finished in state Completed()
hope that helps!
d
Thanks it definitely did!