It seems I've grabbed an example that is not precisely focused on your quest to cancel flow runs of a certain type in Prefect 2.0. Let me correct that by attempting to find a more relevant code example that would guide you through the cancellation process of flow runs based on their state, be it CRASHED or PENDING. Hold on, I'll resubmit a query that's less... depressing.
It's as if the universe is conspiring against me today; it seems my attempts to find an exact code example for canceling flow runs based on their state have been futile. I apologize for the inconvenience.
Although I am unable to provide a direct code example for this task, I can outline a general approach that you can take to cancel flow runs:
1. Use the Prefect 2.0 client to query for flow runs with the specific state you're interested in (e.g., CRASHED or PENDING).
2. Iterate over the retrieved flow runs and cancel each one using the appropriate client method for cancellation.
I'll try one more time to find the information relevant to your request. Please bear with me while I perform my search.
The Prefect documentation provides some clues, although the exact script example seems to be evading me. You can cancel a scheduled or in-progress flow run from various interfaces including the CLI, UI, REST API, or the Python client. When cancellation is requested, the flow run moves to a "Cancelling" state. There are also some enhancements being made to improve the cancellation experience.
While I can't provide the precise Python script you're looking for, based on this information, the general approach would involve using the Prefect Python client to interact with the flow runs. You would list the flow runs, filter them by the desired state (CRASHED or PENDING), and then call a cancellation method for each run.
Here is how you might begin structuring such a script:
python
from prefect import get_client
# Define the states you want to cancel
states_to_cancel = ["CRASHED", "PENDING"]
# Instantiate the Prefect client
with get_client() as client:
# Retrieve flow runs
flow_runs = client.read_flow_runs()
for flow_run in flow_runs:
# Check if the flow run state is one of the states we want to cancel
if flow_run.state in states_to_cancel:
# This is a placeholder for the actual cancellation method you'll call
client.cancel_flow_run(flow_run_id=flow_run.id)
Please note that
client.cancel_flow_run
is a hypothetical method you would replace with the actual cancellation method provided by the Prefect client. You would need to check the Prefect documentation or client method signatures for the correct usage.
For more details on the cancellation process and the Python client, you can refer to the
Prefect Documentation.
Again, I apologize for not providing the one-liner that might suddenly make everything sunshine and rainbows. If only my capacity to help were as large as the universe itself.