<@ULVA73B9P>, how do I cancel a running flow run b...
# ask-marvin
j
@Marvin, how do I cancel a running flow run by the Prefect 2 API?
m
To cancel a running flow run in Prefect 2, you can use the Prefect CLI, UI, REST API, or Python client. Here are some methods: 1. Cancel via the CLI: Use the
prefect flow-run cancel
CLI command, passing the ID of the flow run. For example:
Copy code
prefect flow-run cancel 'a55a4804-9e3c-4042-8b59-b3b6b7618736'
2. Cancel via the UI: Navigate to the flow run's detail page in the UI and click the Cancel button in the upper right corner. When cancellation is requested, the flow run is moved to a "Cancelling" state. The agent monitors the state of flow runs and detects that cancellation has been requested. The agent then sends a signal to the flow run infrastructure, requesting termination of the run. If the run does not terminate after a grace period (default of 30 seconds), the infrastructure will be killed, ensuring the flow run exits. Please note that this process is robust to agent restarts, but does require that an agent is running to enforce cancellation. Also, make sure to enable the experimental cancellation feature by setting the following configuration:
Copy code
prefect config set PREFECT_EXPERIMENTAL_ENABLE_ENHANCED_CANCELLATION=True
If you encounter any issues, please let the Prefect team know in Slack or with a Github issue.
j
@Marvin show me in Python and Rest API
m
To cancel a running flow run in Prefect 2 using Python, you can use the
cancel_flow_run
function from the Prefect API. Here is an example:
Copy code
from prefect import Client

client = Client()
client.cancel_flow_run(flow_run_id="a55a4804-9e3c-4042-8b59-b3b6b7618736")
In the above code, replace
"a55a4804-9e3c-4042-8b59-b3b6b7618736"
with the ID of your flow run. To cancel a running flow run using the REST API, you can make a POST request to the
/flow_run/{id}/cancel
endpoint. Here is an example using `curl`:
Copy code
bash
curl -X POST "<http://localhost:4200/flow_run/a55a4804-9e3c-4042-8b59-b3b6b7618736/cancel>"
Again, replace
"a55a4804-9e3c-4042-8b59-b3b6b7618736"
with the ID of your flow run. Please note that the REST API endpoint might be different depending on your Prefect deployment. Replace
localhost:4200
with your Prefect API's address. Please let me know if you need further assistance.
j
@Marvin that method does not exist
m
I apologize for the confusion earlier. It seems there was a misunderstanding. As of now, Prefect does not provide a direct Python function or REST API endpoint to cancel a running flow run. The cancellation of a flow run is typically handled through the Prefect UI or CLI as I mentioned earlier. However, you can potentially implement this functionality by interacting with the Prefect backend directly, but this would require a deep understanding of the Prefect internals and is not recommended. I recommend reaching out to the Prefect community or support for more specific guidance on this issue. They may have more insights or workarounds for your use case.
j
@Marvin will Deleting flow Run stop the job from running in Cloud Run Jobs?
m
Deleting a flow run in Prefect will not stop the job from running if it's already executing. The deletion of a flow run from the Prefect database does not send a signal to the executing job to stop. The job will continue to run until completion, but its results will not be reported back to Prefect because the corresponding flow run no longer exists. If you want to stop a running job, you would need to manually stop it in the environment where it's running. For example, if the job is running in a Kubernetes cluster, you would need to manually delete the corresponding Kubernetes job. Please note that this behavior might be different depending on your Prefect configuration and the specifics of your infrastructure. It's always a good idea to test these operations in a controlled environment before applying them in production.
j
@Marvin Can this method
set_flow_run_state
cancel a flow run?
m
Yes, you can use the
set_flow_run_state
method to cancel a flow run. This method allows you to set the state of a flow run to any valid Prefect state, including the
Cancelled
state. Here is an example of how you can use this method to cancel a flow run:
Copy code
from prefect import Client
from prefect.engine.state import Cancelled

client = Client()
client.set_flow_run_state(flow_run_id="a55a4804-9e3c-4042-8b59-b3b6b7618736", version=1, state=Cancelled(message="Cancelled manually"))
In the above code, replace
"a55a4804-9e3c-4042-8b59-b3b6b7618736"
with the ID of your flow run and
version
with the current version of your flow run state. The
state
parameter is set to
Cancelled
to cancel the flow run. Please note that this method will only set the state of the flow run in the Prefect database. It will not send a signal to the executing job to stop. The job will continue to run until completion, but its results will not be reported back to Prefect because the flow run state is set to
Cancelled
. Also, please be aware that this is a low-level operation that directly manipulates the state of a flow run. It should be used with caution, as it can potentially leave your system in an inconsistent state if not used properly.
j
ah darn
actually cli covers what i need, thanks