https://prefect.io logo
m

Max Eggers

08/31/2023, 7:58 PM
@Marvin how can I cancel a flow run via the prefect API
m

Marvin

08/31/2023, 7:59 PM
Oh, the joy of cancelling things. Here's how you can cancel a flow run via the Prefect API: 1. Cancel via the CLI: From the command line in your execution environment, you can cancel a flow run by using the
prefect flow-run cancel
CLI command, passing the ID of the flow run. For example:
Copy code
bash
$ prefect flow-run cancel 'a55a4804-9e3c-4042-8b59-b3b6b7618736'
2. Cancel via the UI: From the UI you can cancel a flow run by navigating to the flow run's detail page and clicking the Cancel button in the upper right corner. 3. Cancel via the REST API or Python Client: You may also cancel a scheduled or in-progress flow run from the REST API or Python client. When cancellation is requested, the flow run is moved to a "Cancelling" state. The agent monitors the state of the flow run. Remember, if the infrastructure associated with the run cannot be found or has already been killed, the agent will mark the flow run as cancelled. If the agent runs into an unexpected error during cancellation the flow run may or may not be cancelled depending on where the error occurred. The agent will try again to cancel the flow run. Another agent may attempt cancellation. Isn't it thrilling to bring things to an abrupt end?
m

Max Eggers

08/31/2023, 8:01 PM
@Marvin can you give an example using the rest api
m

Marvin

08/31/2023, 8:01 PM
Ah, the joy of REST APIs. Here's an example of how to cancel a flow run using the Prefect REST API:
Copy code
python
import requests

# Define the API endpoint
url = "<https://api.prefect.io/graphql>"

# Define the headers for the API request
headers = {
    "Authorization": "Bearer YOUR_API_KEY",
    "Content-Type": "application/json",
}

# Define the GraphQL mutation to cancel a flow run
mutation = """
mutation {
    cancel_flow_run(input: { flow_run_id: "YOUR_FLOW_RUN_ID" }) {
        id
    }
}
"""

# Make the API request
response = <http://requests.post|requests.post>(url, headers=headers, json={"query": mutation})

# Print the response
print(response.json())
Replace
YOUR_API_KEY
with your Prefect API key and
YOUR_FLOW_RUN_ID
with the ID of the flow run you want to cancel. Remember, this is a mutation operation, not a query. Mutations in GraphQL are used to create, update, and delete data. Isn't it thrilling to interact with APIs? It's like talking to a wall, but the wall sometimes talks back.
n

Nate

08/31/2023, 8:01 PM
marvin still thinks we have graphql 🙄 - id just use the client method like the CLI does
m

Max Eggers

08/31/2023, 8:02 PM
🤙 ty