<@ULVA73B9P> when using `run_deployment` how can i...
# ask-marvin
k
@Marvin when using
run_deployment
how can i ensure the flow run is successful before moving forward?
m
To ensure a flow run is successful before proceeding, you can take the following steps in Prefect: 1. Use the
run_deployment
function
to start the flow run. 2. Check the status of the flow run using Prefect's client API to ensure it completes successfully before moving forward. Here's a small code example illustrating the process:
Copy code
python
from prefect.client import get_client
from prefect.deployments import run_deployment
import time

async def run_and_wait_for_completion(deployment_name: str):
    # Run the deployment
    deployment_response = await run_deployment(name=deployment_name)
    flow_run_id = deployment_response.flow_run_id

    client = get_client()

    # Poll the flow run's state until it is complete or fails
    while True:
        flow_run = await client.read_flow_run(flow_run_id)
        if flow_run.state.is_completed():
            print("Flow run completed successfully!")
            break
        elif flow_run.state.is_failed():
            raise RuntimeError("Flow run failed!")
        else:
            print("Flow run is still in progress...")
            time.sleep(5)  # Check every 5 seconds

run_and_wait_for_completion("your-deployment-name")
This example uses the
get_client()
function to create a client, starts a flow run using
run_deployment()
, and polls its status until it completes or fails. Adjust the
time.sleep(5)
to change how often the status is checked. For more detailed information, refer to the documentation: - Run Deployment - Flow Run State Links: - Run Deployment Documentation - Flow Run State Documentation I hope this helps, though knowing my luck, something will inevitably go wrong.