How to schedule a flow run (from a deployment) for a specific date?

From the UI

From Python

Here is an example how you could do it from code:

import asyncio
import datetime

import pendulum
from prefect.client import get_client
from prefect.orion.schemas.states import Scheduled
from prefect.orion.schemas.filters import FlowFilter, DeploymentFilter


async def add_new_scheduled_run(
    flow_name: str, deployment_name: str, dt: datetime.datetime
):
    async with get_client() as client:
        deployments = await client.read_deployments(
            flow_filter=FlowFilter(name={"any_": [flow_name]}),
            deployment_filter=DeploymentFilter(name={"any_": [deployment_name]}),
        )
        deployment_id = deployments[0].id
        await client.create_flow_run_from_deployment(
            deployment_id=deployment_id, state=Scheduled(scheduled_time=dt)
        )


if __name__ == "__main__":
    asyncio.run(
        add_new_scheduled_run(
            flow_name="healthcheck",
            deployment_name="prod",
            dt=pendulum.datetime(2042, 4, 2, 20, 0, 0, 0, tz="Europe/Berlin"),
        )
    )
2 Likes

There isn’t a time available here so everything scheduled via the UI ad hoc (not as a schedule) will always run at midnight?

In both examples shown here you can select the time, both via code and UI

Do you have an example of selecting a time in the UI? When I do this in my instance, like your screenshot I can only select a date and not an actual time. I can select the timezone, but not the time.

For the code version we are using a similar method and it works great!

Click on time shown in the bottom left corner

Ahh. I’ll confirm this but that looks right and why I missed it.

Definitely missing on mobile though, but understand that’s probably not a focus.

1 Like

Yeah exactly, in a browser instead of “Reset”, you would see the current time is displayed and you could change it to select a custom time.

an update: in 2.5, we added a utility function called run_deployment that allows to schedule a run for later in just a single function call - more details and examples here: