<@ULVA73B9P> please write an automation configurat...
# ask-marvin
k
@Marvin please write an automation configuration in yaml file that will detect flows, running longer than 30 minutes, and send a slack notification
m
thought for 303.4 seconds
Happy to. Here’s a clean automation YAML you can import that will alert when any flow run has been in Running for more than 30 minutes and send a Slack notification. Assumes Prefect 3.x and that you’ve already created a Slack Notification block in Prefect (e.g., a Slack webhook block) and know its block name. Replace SLACK_NOTIFICATION_BLOCK_NAME with your block’s name.
Copy code
name: "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.
k
@Marvin I use OSS, prefect 3 and I want to create everything that is needed for this scenario through CLI.
m
Error: No module named 'prefect.client.schemas.automations'
thought for 570.5 seconds
Got it. On OSS with Prefect 3.x, the CLI can list/manage automations, but it doesn’t have a “create” subcommand. The supported path is to create blocks and automations via a tiny Python snippet (run from the CLI), then manage them with the CLI. Below is a fully CLI-driven setup that: - creates a Slack notification block - creates an automation that fires when a flow run stays in Running for 30 minutes - shows how to verify it Assumptions: - Your OSS server is running and reachable at $PREFECT_API_URL (default for local server is http://127.0.0.1:4200/api) - You have a Slack Incoming Webhook URL 1) Point the CLI at your OSS server
Copy code
export PREFECT_API_URL="<http://127.0.0.1:4200/api>"
prefect version
2) (Optional) Register notification blocks (usually already registered)
Copy code
prefect block register -m prefect.blocks.notifications
3) Create a SlackWebhook block (replace the webhook URL and block name)
Copy code
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.
Copy code
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
Copy code
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.
n
grr idk why marvin doesn't know about the
create
command, it exists! marvin's first suggestion should not be so far off
since you can pass
prefect automation create --from-file some.yaml
k
yes, thanks! but the Slack block I can create only through a python script, right?
n
you can create them from python or the UI
🙏 1