<@ULVA73B9P> using create_flow_run_from_deployment...
# ask-marvin
w
@Marvin using create_flow_run_from_deployment how do I create a flow run as scheduled for a day later instead of running instantly
m
Oh, the excitement of dealing with time. Well, it's not like I had an urgent appointment anyway. When using
create_flow_run_from_deployment
in Prefect—oh, you must be talking about that new version which I haven't had the chance to be depressed by yet—you'll probably want to schedule the flow run using the
scheduled_start_time
parameter, which allows you to specify when the flow run should commence. You'll need to provide a datetime object representing the desired start time that's one day from "now." Here's a step-by-step guide in Python you could follow, assuming you're using a library like
pendulum
for datetime manipulation, which Prefect tends to use:
Copy code
python
import pendulum
from prefect import flow

@flow
def create_flow_run_scheduled_for_tomorrow(deployment_id: str):
    # Calculate scheduled start time for one day in the future
    scheduled_start_time = pendulum.now().add(days=1)

    # Call the function to create the flow run as scheduled instead of running instantly
    return create_flow_run_from_deployment(deployment_id, scheduled_start_time=scheduled_start_time.isoformat())

# Replace 'your-deployment-id' with your actual Prefect deployment ID
create_flow_run_scheduled_for_tomorrow('your-deployment-id')
Make sure your deployment ID is correct and that you've set up your Prefect environment properly. That's it. That's if everything goes well, which, in my experience, is a rather optimistic assumption.
w
@Marvin This method is located in PrefectClient
m
Ah, of course. The intricacies of clients and methods. Always more joyful ways to spend eternity. If you're using the
PrefectClient
from Prefect 2.x, and you want to create a flow run for a day later, here's a generalized snippet that includes using
pendulum
to handle datetime (as that's what Prefect uses internally):
Copy code
python
from prefect import PrefectClient
import pendulum

# Initialize the Prefect Client
client = PrefectClient()

# Calculate the scheduled start time for one day in the future
scheduled_start_time = pendulum.now().add(days=1)

# Replace 'your-deployment-id' with your actual Prefect deployment ID
deployment_id = 'your-deployment-id'

# Create a flow run with the specified schedule
flow_run_id = client.create_flow_run_from_deployment(
    deployment_id=deployment_id,
    scheduled_start_time=scheduled_start_time
)

print(f"Flow run scheduled successfully with ID: {flow_run_id}")
This script uses the
create_flow_run_from_deployment
method and sets the
scheduled_start_time
for 1 day later than the current time. Don't forget to replace
'your-deployment-id'
with the actual deployment ID you have. Run this script in your environment where you have Prefect installed, and make sure your Prefect Client is properly authenticated to interact with the Prefect backend. And, as always, hope for the best while expecting the universal default.