<@ULVA73B9P> We have a few flows that we manually ...
# ask-marvin
j
@Marvin We have a few flows that we manually kick off when we need to assign/re-assign hardware for an upcoming customer event. We have a task at the beginning of each flow that checks to make sure one of the other flows isn't already running for this customer. Currently, we fail the flow if that's the case, but we would rather reschedule the flow to try running in 30 minutes. In the docs, I see that flows have retry arguments, but we don't want to retry the flow for "any failure". We only want to retry the flow if this particular task fails. I also read the Schedules section of the docs, but it says "Schedules tell the Prefect API how to create new flow runs for you automatically on a specified cadence." We don't run these flows on a specified cadence. These flows are only started manually by a human. Is it possible to reschedule a flow run to start N minutes from now from within a running flow?
m
Based on the exploration of the Prefect documentation and available resources, it appears there is no ready-made solution explicitly outlined for rescheduling a flow within a running task. That said, you can leverage the Prefect Client to achieve the desired behavior. Here's a step-by-step approach to reschedule a flow to start N minutes from now if a specific task fails: 1. Check if the other flow is running 2. If it is running, create a new flow run scheduled N minutes from now 3. Fail the current flow run Below is a sample implementation to guide you through this process:
Copy code
python
from prefect import task, flow
from prefect.client import get_client
from datetime import timedelta, datetime, timezone

@task
async def check_and_reschedule():
    # Placeholder check for existing running flows for a customer
    existing_flow_running = True  # This should contain the actual logic to check the condition

    if existing_flow_running:
        async with get_client() as client:
            # Schedule a new flow run
            current_time = datetime.now(timezone.utc)
            scheduled_start_time = current_time + timedelta(minutes=30)
            await client.create_flow_run_from_deployment(
                deployment_id="your_deployment_id_here",
                scheduled_start_time=scheduled_start_time
            )
        raise Exception("Flow rescheduled due to existing running flow.")
    else:
        # Proceed with the flow
        print("No existing flows. Proceeding with the flow execution.")

@flow
def reschedule_flow():
    check_and_reschedule()
    # Add other tasks here

if __name__ == "__main__":
    reschedule_flow()
### Key Points: - Prefect Client: The Prefect Client is used to interact with the Prefect API, allowing the creation of new flow runs. - Timedelta: The
timedelta
is used to specify the delay (30 minutes in this example) for the rescheduled flow run. - Scheduled Start Time: The new flow run is scheduled to start at the specified future time. For more details, refer to the Prefect Client guide which provides additional examples and options. Feel free to adjust the implementation as per your specific logic and needs. Also, note that in the real-world scenario, error handling and logging should be incorporated for maintaining robustness.