This message was deleted.
# ask-marvin
s
This message was deleted.
m
To help you with an example of automating the process of checking if a subflow has failed after a parent flow is completed and sending a Slack notification, I need to know which version of Prefect you are using, as the implementation details can vary. Could you please specify if you are using Prefect 2.x or 3.x?
w
2.x
i am using prefect 2.x
I want the example json for the automation
@Marvin i am using prefect 2.x and I want the example json for the automation
m
To achieve automation in Prefect 2.x that checks when a parent flow is complete to see if any subflows have failed, and then sends a Slack notification, you can utilize state change hooks and automations in Prefect. Here's a simplified example of how you might set this up, focusing on using state change hooks and automations: 1. State Change Hooks: These allow you to execute code in response to changes in flow or task states, such as notifying on failures. 2. Automations: You can configure these to trigger actions when a flow completes. Here's a conceptual approach for setting it up: ### Prefect Flow Setup
Copy code
python
from prefect import flow, task
from prefect.blocks.notifications import SlackWebhook

@task
def check_subflows(subflow_states):
    if any(state.is_failed() for state in subflow_states):
        notify_slack()

def notify_slack():
    webhook = SlackWebhook.load("my-slack-webhook")
    webhook.notify("A subflow has failed!")

@flow
def parent_flow():
    # Run subflows and collect their states
    subflow_states = []
    for _ in range(3):  # Assuming you have 3 subflows to run
        subflow_states.append(subflow.run())

    check_subflows(subflow_states)

@flow
def subflow():
    # Simulated subflow logic
    pass

if __name__ == "__main__":
    parent_flow()
### Automation Configuration 1. Create a Slack Webhook Block: Store your slack webhook URL using
prefect block
so that it can be called within your tasks. 2. State Change Hooks: Implement hooks on flow/task failures to automatically trigger notifications. You would configure this through the Prefect UI or script automation, depending on your deployment setup. More detailed documentation and steps can be found here and here. This setup checks subflow states after the parent flow completion and sends a Slack message if any subflow fails. You can adjust the logic to meet your specific requirements, such as including different conditions or adding more detailed messages.
w
@Marvin can i not simply use the automation json to do this?
m
Unfortunately, there aren't specific examples of using JSON for automation to send notifications on subflow completion directly in the available Prefect resources or GitHub issues. However, Prefect's automations are typically configured through the Prefect UI rather than JSON files. That said, here's a general approach to what your JSON object might conceptually look like if you were setting up an automation directly through the UI, which Prefect then manages under the hood: 1. Trigger on Flow Completion: Set your automation to trigger when any flow completes. This implies setting a condition on the status of subflows. 2. Action to Send Notification: The action would be sending a notification to your Slack integration configured in Prefect. Due to the lack of specific JSON support for defining automations outside of the Prefect UI, I'd suggest manually configuring this in the UI following these conceptual steps: - Navigate to the Automations section in the Prefect Cloud UI or Prefect Server. - Create a new automation that triggers on the flow completion status. - Set conditions to check for failed subflows (this might involve some coding to check these conditions programmatically via state checks, as outlined in the code example earlier). - Configure the Slack action using the Prefect-provided integrations. If you need more specific automation configurations via code, Prefect primarily guides you to use their Python SDK or UI, rather than a JSON interface. You can always keep an eye on Prefect's documentation and GitHub repository for updates or community-contributed examples.