Kate
08/13/2025, 1:30 PMMarvin
08/13/2025, 1:30 PMMarvin
08/13/2025, 1:35 PMname: "Alert: Long-running flow runs (>30m)"
description: "Notify Slack when a flow run remains in Running for more than 30 minutes."
enabled: true
trigger:
type: Event
# Fires when a flow run enters/continues Running and stays there for 30 minutes
match:
- "prefect.flow-run.Running"
after: "PT30M" # ISO-8601 duration (30 minutes)
# Optional: scope to a specific flow by name (uncomment and set your flow name)
# match_related:
# - "prefect.flow.name == 'my-flow-name'"
actions:
- type: SendNotification
# Name of your Notification block (e.g., a Slack webhook block)
block_document_name: "SLACK_NOTIFICATION_BLOCK_NAME"
subject: "Flow run running > 30m: {{ event.resource.name }}"
body: |
A flow run has been running for more than 30 minutes.
Flow: {{ event.payload.flow_name }}
Run: {{ event.resource.name }}
Run ID: {{ event.resource.id }}
State: {{ event.payload.state.name }}
Started: {{ event.payload.state.timestamp }}
Link: {{ event.resource.url }}
Notes and tips:
- Import this in Prefect Cloud: Automations -> New -> Import from YAML.
- The after: "PT30M" makes the trigger fire only if the run stays Running continuously for 30 minutes.
- The templated fields in subject/body use the event context Prefect provides; you can adjust to your liking.
- If you want to limit to specific flows, uncomment the match_related section and set your flow name.
If you don’t have a Slack Notification block yet, create one first (Blocks -> Notifications -> Slack) and then set block_document_name to that block’s name.
Docs for deeper digging:
- Automations overview: Automations
- Triggers: Automation triggers
- Send notification action: Notification actions
Want me to tailor this to a specific flow/project or to a particular Slack channel? Tell me the flow name and your Slack notification block name, and I’ll tweak it.Kate
08/13/2025, 1:40 PMMarvin
08/13/2025, 1:40 PMMarvin
08/13/2025, 1:45 PMMarvin
08/13/2025, 1:55 PMexport PREFECT_API_URL="<http://127.0.0.1:4200/api>"
prefect version
2) (Optional) Register notification blocks (usually already registered)
prefect block register -m prefect.blocks.notifications
3) Create a SlackWebhook block (replace the webhook URL and block name)
uv run python - <<'PY'
from prefect.blocks.notifications import SlackWebhook
block_name = "slack-long-running"
webhook_url = "<https://hooks.slack.com/services/XXX/YYY/ZZZ>" # replace me
SlackWebhook(url=webhook_url).save(block_name, overwrite=True)
print(f"Created SlackWebhook block: {block_name}")
PY
prefect block ls | grep slack-long-running || true
4) Create the automation via the Prefect client
- This automation triggers if a flow run remains in Running for 30 minutes and sends a Slack notification using the block above.
uv run python - <<'PY'
import asyncio
from prefect.client.orchestration import get_client
# Build the automation payload as plain data to avoid version-specific imports
automation = {
"name": "Alert: Long-running flow runs (>30m)",
"description": "Notify Slack when a flow run remains Running for more than 30 minutes.",
"enabled": True,
"trigger": {
"type": "Event",
"match": ["prefect.flow-run.Running"],
"after": "PT30M" # ISO-8601 for 30 minutes
# To scope to a single flow, add:
# "match_related": ["prefect.flow.name == 'my-flow-name'"]
},
"actions": [
{
"type": "SendNotification",
"block_document_name": "slack-long-running",
"subject": "Flow running > 30m: {{ event.payload.flow_name }}",
"body": (
"A flow run has been running for more than 30 minutes.\n\n"
"Flow: {{ event.payload.flow_name }}\n"
"Run: {{ event.resource.name }}\n"
"Run ID: {{ event.resource.id }}\n"
"State: {{ event.payload.state.name }}\n"
"Started: {{ event.payload.state.timestamp }}\n\n"
"Link: {{ event.resource.url }}"
),
}
],
}
async def main():
async with get_client() as client:
created = await client.create_automation(automation)
print(f"Created automation: {created.get('id', created)}")
asyncio.run(main())
PY
5) Verify the automation exists
prefect automation ls
# For details:
# prefect automation inspect <AUTOMATION_ID>
That’s it. When any flow run stays in Running for 30 minutes, you’ll get a Slack message.
References (for the brave):
- Automations overview: Automations
- Notification blocks: Blocks and Notifications
If you want me to scope it to a specific flow or tweak the Slack message, tell me the flow name and I’ll adjust the snippet.Nate
08/13/2025, 2:06 PMcreate
command, it exists! marvin's first suggestion should not be so far offNate
08/13/2025, 2:06 PMprefect automation create --from-file some.yaml
Kate
08/13/2025, 2:14 PMNate
08/13/2025, 2:15 PM