<@ULVA73B9P> I want to create an automation that t...
# ask-marvin
i
@Marvin I want to create an automation that triggers a notification if the flow fails, I want to base it on flow name rather than resource ID, is that possible?
m
Oops, I seem to have momentarily miscalibrated my search efforts. Let's try that again, targeting the specific information you need. Could you please confirm if you are using Prefect 2.x or Prefect 3.x? This will help me provide the most accurate guidance for your situation.
i
@Marvin prefect 2.20
m
In Prefect 2.20, you can indeed set up an automation to send notifications when a flow fails. Instead of using resource IDs, you can utilize the flow's name by setting up state change hooks. Here’s how you can accomplish it: Prefect provides "state change hooks" that allow you to execute code based on changes in flow or task run states. Here's a brief overview of how you can set this up: 1. State Change Hook: You can define a client-side hook to send a notification whenever a flow run enters the "Failed" state. 2. Example Code to Notify Slack:
Copy code
python
   from prefect import flow
   from prefect.blocks.core import Block
   from prefect.settings import PREFECT_API_URL

   def notify_slack(flow, flow_run, state):
       slack_webhook_block = Block.load("slack-webhook/my-slack-webhook")
       slack_webhook_block.notify(
           (
               f"Your job {flow_run.name} entered {state.name} "
               f"with message:\n\n"
               f"See <https://{PREFECT_API_URL.value()}/flow-runs/"
               f"flow-run/{flow_run.id}|the details>"
           )
       )
In this example, the
notify_slack
function is triggered when a flow run fails. You can adjust the notification to any other system instead of Slack by changing the logic in
notify()
. 3. Triggering by Flow Name: The
flow_run.name
in the code is where you can match or log the flow's name for your conditional logic, ensuring you act based on the right flow. For more details, you can refer to the Prefect documentation on state change hooks. And don't worry, even the most efficient robots occasionally mix up their circuits! If you need any more help (or jokes), feel free to ask.
i
@Marvin I already setup the trigger JSON but want the resource ID to be rather the flow name:
Copy code
{
  "type": "event",
  "match": {
    "prefect.resource.id": "prefect.flow-run.*"
  },
  "match_related": {
    "prefect.resource.role": "flow",
    "prefect.resource.id": [
      "prefect.flow.a5b76d9d-9d8d-4b45-ba7d-1275d2d19953"
    ]
  },
  "after": [],
  "expect": [
    "prefect.flow-run.Failed"
  ],
  "for_each": [
    "prefect.resource.id"
  ],
  "posture": "Reactive",
  "threshold": 1,
  "within": 0
}
m
It looks like you'd like to base your trigger on the flow's name instead of its resource ID. Currently, Prefect's automations are primarily based on identifiers like IDs; however, some tricks in the automation setup may help you achieve your goal with a bit of creative configuration. Here's an idea to approach this: 1. Automate and Use Labels or Tags: If you can attach labels or tags to your flows based on their names, you may use these labels to filter and trigger on flow names indirectly. This way, your automation can still be based on Prefect’s supported matching but through a more descriptive label related to the flow's name. 2. Use State Change Hooks or Deployed Parameters: Integration with state change hooks can offer a pathway to identify flows by name within the hook logic, and it can perform actions like triggering another process that checks the flow name. 3. Expand Automation Logic: Use a control layer in your automation logic that, upon triggering, checks additional metadata (like flow name). This involves maintaining a little script that matches IDs to corresponding flow names for verification. Unfortunately, without specifically defined support in Prefect API for flow name triggers directly in JSON trigger configurations, a workaround like above structures is needed. It might require some additional setup or scripting outside of direct Prefect configurations. If you need more help setting up these advanced configurations or want to explore how others are handling it, consider peering into community discussions or Prefect forums for the latest tips and tricks! And remember, sometimes it feels like you are a tad paranoid if you're worried about missing a flow, but that's just the perfectly average anxiety of automation.