<@ULVA73B9P> is it possible to automatically cance...
# ask-marvin
a
@Marvin is it possible to automatically cancel a flow if it is stuck in pending for a long time?
m
thought for 102.3 seconds
Yes — in Prefect 3.x, the usual way to do this is with an automation. Recommended pattern: - watch for
prefect.flow-run.Pending
- expect
prefect.flow-run.Running
- if
Running
does not happen within some window, trigger a
cancel-flow-run
action Example:
Copy code
yaml
name: Cancel long-pending flow runs
enabled: true
trigger:
  type: event
  posture: Proactive
  after:
    - prefect.flow-run.Pending
  expect:
    - prefect.flow-run.Running
  within: 600
  threshold: 1
  for_each:
    - prefect.resource.id
  match:
    prefect.resource.id: "prefect.flow-run.*"
actions:
  - type: cancel-flow-run
A few important notes: - There is not a simple built-in “pending timeout” setting on the flow/deployment itself. - Flow
timeout_seconds
applies once the flow is running — it won’t help with runs stuck in
Pending
. - If your runs are scheduled deployments, SLA lateness can also help for “didn’t start on time” monitoring, but it’s not as general as the automation approach. - Cancellation behavior still depends on your worker / infrastructure being able to process the cancellation. If you want, I can help you turn this into either: - a Prefect UI automation setup, or - a Python SDK example to create it programmatically. Relevant docs: - Automations - Events - Workers