<@ULVA73B9P> i want to create an automation in my ...
# marvin-ai
k
@Marvin i want to create an automation in my prefect.yaml fiile to cancel a flow run X when this stays in state running for more than 30 seconds the name of the automation would be CANCEL_LONG_RUNS
m
thought for 1049.5 seconds
Great question! In Prefect 3.x, prefect.yaml does not support top-level “automations” with names. However, you can get the exact behavior you want by adding a proactive event trigger to the deployment in your prefect.yaml. This trigger will cancel a flow run if it stays in Running for more than 30 seconds. Here’s a minimal example you can paste under the specific deployment’s config in prefect.yaml. It applies only to that deployment’s runs (no extra filtering needed):
Copy code
deployments:
  - name: your-deployment-name
    entrypoint: path/to/flow.py:your_flow
    # ... other deployment settings ...
    triggers:
      - type: event
        enabled: true
        posture: Proactive
        after:
          - prefect.flow-run.Running
        expect:
          - prefect.flow-run.Completed
          - prefect.flow-run.Failed
          - prefect.flow-run.Crashed
        for_each:
          - prefect.resource.id
        threshold: 1
        within: 30
        actions:
          - type: cancel-flow-run
Notes - This is an event trigger with Proactive posture: it fires when none of the expected terminal events appear within 30 seconds after a run enters Running. -
for_each: ["prefect.resource.id"]
ensures evaluation per run. -
actions: [{ type: cancel-flow-run }]
cancels the stuck run. About naming (CANCEL_LONG_RUNS) - Deployment triggers in prefect.yaml cannot be named. If you specifically need a named, reusable automation called CANCEL_LONG_RUNS that can apply across deployments, create a standalone automation instead (via the UI). You can use a definition like this in the UI:
Copy code
name: CANCEL_LONG_RUNS
description: Cancel flow runs that stay Running for more than 30s
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
  for_each:
    - prefect.resource.id
  threshold: 1
  within: 30
  # Optional: add match/match_related here to scope to a specific flow or deployment
actions:
  - type: cancel-flow-run
Clarification - When you say “flow run X”, do you mean: - Only runs of a specific deployment or flow? - Or all runs across your workspace? If it’s a single deployment, the prefect.yaml deployment trigger above is the simplest path. If it’s broader, a standalone automation (with filters) is better. Docs for reference - Event triggers (Proactive, after/expect/within): Event triggers - Deployment triggers in prefect.yaml: Creating deployment triggers - Creating standalone automations: Creating automations