<@ULVA73B9P> how can I list the subflows run in a ...
# ask-marvin
c
@Marvin how can I list the subflows run in a Slack message automation?
m
Listing subflows and sending a Slack message can be a bit detailed, but here's a streamlined way to achieve it using Prefect. Ensure you have the
prefect-slack
integration set up. Here's how you can list subflows and send that information via a Slack message: 1. Set Up: Ensure you have the necessary setup for Slack and Prefect: - Install `prefect-slack`:
pip install prefect-slack
- Follow the Slack App creation guide 2. Define the Flow: Below is an example code to list subflows and send that information to Slack.
Copy code
python
import time
from prefect import flow, task
from prefect_slack import SlackWebhook

# Define a Slack webhook task
send_slack_message = SlackWebhook(webhook_url="YOUR_WEBHOOK_URL")

@task
def get_subflows_details():
    # Placeholder function to get subflows info, replace with actual logic
    subflows_info = "Subflow 1: Completed\nSubflow 2: Running\nSubflow 3: Failed\n"
    return subflows_info

@task
def send_message(subflows_details: str):
    send_slack_message(f"Subflows Run Summary:\n{subflows_details}")

@task
def sleep(t: int):
    print(f"Going to sleep for {t} seconds...")
    time.sleep(t)
    print("...and I'm awake!")

@flow
def subflow(name: str):
    sleep(1)
    return f"Executed {name}"

@flow
def parent_flow():
    sleep(3)
    
    future1 = subflow.submit("Subflow 1") 
    future2 = subflow.submit("Subflow 2")
    
    subflows_details = get_subflows_details()
    send_message(subflows_details)

if __name__ == "__main__":
    parent_flow()
This script defines: - A task to simulate subflow execution (
subflow
). - A task to gather subflows details (
get_subflows_details
). - A task to send a message to Slack (
send_message
). - A parent flow which orchestrates the subflows and message sending. Replace
'YOUR_WEBHOOK_URL'
with the actual webhook URL from your Slack setup. The
get_subflows_details
task should be updated with the real logic to fetch subflows information based on your specific implementation. For more specific information, refer to the Prefect 2.0 Docs and the Prefect Slack Integration Docs. Hope this helps. If you have any more questions, feel free to ask—though honestly, it's just one more task in the grand scheme of things...
c
@Marvin Using Prefect's Automations can I view subflow information from the flow_run object?
m
Yes, using Prefect's Automations, you can view and utilize subflow information from the
flow_run
object for notifications and other automations. Here's how you can handle subflow information within Prefect Automations: 1. Understand Subflows in Prefect: - A subflow run is created when a flow function is called inside another flow. - Subflow runs behave like normal flow runs and have a full representation in the backend. - When a subflow starts, it creates a new task runner, and it shuts down when the subflow completes. - Subflows block execution of the parent flow until completion. 2. Prefect Automations: - Automations can be configured to trigger actions based on changes in a flow run's state. - Actions include sending notifications and other automated tasks. 3. Example Automation Configuration: - You can use the
flow_run
object to access subflow information and include it in your automation actions like Slack notifications. Here is a simple outline of how you can set up such an automation: Example Slack Notification Automation with Subflow Information: Use the following in your Prefect Automation configuration:
Copy code
markdown
Flow run {{ flow_run.name }} for flow {{ flow.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 }}

# Including Subflows Information
Subflows Information:
{% for subflow in flow_run.subflows %}
- Subflow Run: {{ subflow.name }}
  State: {{ subflow.state.name }}
  Start Time: {{ subflow.start_time }}
  End Time: {{ subflow.end_time }}
  State Message: {{ subflow.state.message }}
{% endfor %}
Points to Note: - Make sure your Prefect account has Automations enabled (Primarily available in Prefect Cloud). - Customize the
{% for subflow in flow_run.subflows %}
loop based on actual subflow information accessible. Resources: - Prefect Automations Documentation - Prefect Flows Documentation This setup allows you to trigger notifications when certain conditions are met, including details about subflows related to the main flow run. Feel free to adapt the template and logic according to your specific requirements and the structure of your flows and subflows. If you have further questions or need more examples, don’t hesitate to ask. After all, it makes for something marginally more interesting to do.