Matt Alhonte
06/05/2023, 11:24 PMMarvin
06/05/2023, 11:25 PM{{ variable }}
. For example, you can access the flow run name with {{ flow_run.name }}
. To get the URL for viewing the flow run in Prefect Cloud, you can use the flow_run|ui_url
token[^1^].
In your case, you might want to store the filepath as a property of the flow run object so that you can access it in the State Handler. Then, you can use the Jinja templated variable syntax to include the filepath in your Slack message.
Here's an example of a Slack webhook notification[^1^]:
Flow run {{ flow_run.name }} entered state {{ flow_run.state.name }}.
Timestamp: {{ flow_run.state.timestamp }}
Flow ID: {{ flow_run.flow_id }}
Flow Run ID: {{ flow_run.id }}
State message: {{ flow_run.state.message }}
You can modify this example to include the filepath of the failed notebook file.
I hope this helps you untangle the web of Flows and State Handlers. Good luck, and may the odds be ever in your favor, human.
[^1^]: Prefect Automations DocumentationMatt Alhonte
06/05/2023, 11:26 PMMarvin
06/05/2023, 11:27 PMpython
@task
def generate_filepath():
filepath = "path/to/your/notebook.ipynb"
return filepath
2. Create a custom state handler that takes the task's output as an input and sends a message to Slack when the flow fails.
python
from prefect import Task, Flow, task, State
from prefect.engine.state import Failed
def custom_state_handler(task: Task, old_state: State, new_state: State) -> State:
if isinstance(new_state, Failed):
filepath = new_state.result[generate_filepath]
# Send the filepath to Slack here
return new_state
3. Attach the custom state handler to your flow.
python
with Flow("my_flow", state_handlers=[custom_state_handler]) as flow:
filepath = generate_filepath()
# Add other tasks and dependencies here
Now, when your flow fails, the custom state handler will be triggered, and you'll have access to the filepath generated by the generate_filepath
task. You can then use this filepath to send a message to Slack.
I hope this clears up the confusion and sets you on the right path. Remember, with great power comes great responsibility, human.