<@ULVA73B9P> Ok, this one should be a piece for ca...
# ask-marvin
r
@Marvin Ok, this one should be a piece for cake for you. Write a prefect 3.7.2 automation in an automation.yaml that sends a notification if any flow run in the work-pools named default or backup has been running for more than 2 hours
m
thought for 131.6 seconds
Yep — here’s an
automation.yaml
for Prefect 3.7.2 that alerts when a flow run in the
default
or
backup
work pool has been running for more than 2 hours.
Copy code
yaml
name: "long-running-flow-run-alert"
description: "Notify when a flow run in default or backup work pools runs longer than 2 hours"
enabled: true

trigger:
  type: event
  posture: Proactive
  after:
    - prefect.flow-run.Running
  expect:
    - prefect.flow-run.Completed
    - prefect.flow-run.Failed
    - prefect.flow-run.Crashed
    - prefect.flow-run.Cancelled
  for_each:
    - prefect.resource.id
  match:
    prefect.resource.id: "prefect.flow-run.*"
  match_related:
    prefect.resource.role: "work-pool"
    prefect.resource.name:
      - "default"
      - "backup"
  threshold: 1
  within: 7200

actions:
  - type: send-notification
    block_document_id: "YOUR_NOTIFICATION_BLOCK_ID"
    subject: "Long-running flow run detected"
    body: |
      Flow run {{ flow_run.name }} has been running for more than 2 hours.

      Flow: {{ flow.name }}
      Work pool: {{ work_pool.name }}
      Flow run ID: {{ flow_run.id }}
      State: {{ flow_run.state.name }}
A couple quick notes: -
within: 7200
is 2 hours in seconds -
posture: Proactive
means “alert if the expected terminal event does not happen within the time window” -
for_each: [prefect.resource.id]
ensures this evaluates per flow run -
match_related
filters to flow runs associated with work pools named
default
or
backup
To create it:
Copy code
bash
prefect automation create --from-file automation.yaml
I verified that command exists in 3.x. Relevant docs: - Create automations - Automations concept docs If you want, I can also give you: - a version that uses a specific notification block type like Slack/email/MS Teams, or - a JSON version for
prefect automation create --from-json
r
@Marvin Why do you put double quotes around the match statements. I don't think those are needed? Is there a good reason to have those?
m
thought for 12.0 seconds
They’re not required here. I used quotes mostly for clarity and YAML safety, not because Prefect requires them. For example, these are equivalent in YAML:
Copy code
yaml
prefect.resource.id: "prefect.flow-run.*"
and
Copy code
yaml
prefect.resource.id: prefect.flow-run.*
Likewise:
Copy code
yaml
- "default"
- "backup"
is equivalent to:
Copy code
yaml
- default
- backup
Why people often quote them anyway - To make it obvious the value is a string - To avoid YAML interpreting special-looking values oddly in other cases - To be safer when values contain characters like
:
,
#
,
{}
,
[]
,
*
, or leading/trailing spaces - For consistency across config files In this automation, you can remove the quotes if you prefer. For example:
Copy code
yaml
match:
  prefect.resource.id: prefect.flow-run.*

match_related:
  prefect.resource.role: work-pool
  prefect.resource.name:
    - default
    - backup
One subtle note: YAML has special meaning for
*
at the start of a value because it can denote an alias. Your value is
prefect.flow-run.*
, so it’s generally fine unquoted. So the short answer is: no, there’s no Prefect-specific reason they must be quoted here. It’s just style/defensive YAML. If you want, I can rewrite the full
automation.yaml
in a more minimal unquoted style.