Dzmitry-S
10/31/2024, 10:44 AMMarvin
10/31/2024, 10:44 AMScheduled
state from prefect.states
.
2. Create an asynchronous function to adjust flow run schedules:
python
from prefect.states import Scheduled
from prefect.client import get_client
from datetime import timedelta, datetime, timezone
from prefect.filters import FlowRunFilter, DeploymentFilter, FlowRunSort
async def reschedule_late_flow_runs(deployment_name: str, delay: timedelta, most_recent_n: int, delete_remaining: bool = True, states: Optional[list[str]] = None) -> list:
if not states:
states = ["Late"]
async with get_client() as client:
flow_runs = await client.read_flow_runs(
flow_run_filter=FlowRunFilter(
state=dict(name=dict(any_=states)),
expected_start_time=dict(
before_=datetime.now(timezone.utc)
),
),
deployment_filter=DeploymentFilter(
name={'like_': deployment_name}
),
sort=FlowRunSort.START_TIME_DESC,
limit=most_recent_n if not delete_remaining else None
)
if not flow_runs:
print(f"No flow runs found in states: {states!r}")
return []
rescheduled_flow_runs = []
for i, run in enumerate(flow_runs):
await client.delete_flow_run(flow_run_id=run.id)
if i < most_recent_n:
# Here you might create a new run or reschedule as needed
pass
This script finds and optionally deletes flow runs in a certain state (e.g., "Late") and can reschedule those runs as needed.
For further details, you can refer to the Prefect 3.0 API interaction docs.Dzmitry-S
10/31/2024, 10:45 AMMarvin
10/31/2024, 10:46 AMprefect deployment schedule
command to configure schedules programmatically via the command line.
3. YAML Configuration: Define your schedules in the prefect.yaml
file associated with your project. This allows you to specify parameters like cron
, interval
, or rrule
when you configure your deployment.
4. Programmatic Deployment: If you're deploying a flow programmatically, you can use the flow.deploy
or flow.serve
methods. This is also where you would set parameters for scheduling.
Here's an example of how you might define a schedule in the `prefect.yaml`:
yaml
deployments:
- name: my-scheduled-deployment
schedule:
interval: "@daily" # using cron, interval, or rrule as needed
For more details, you can check out the Prefect 3.0 documentation on adding schedules.Bianca Hoch
10/31/2024, 2:43 PMscheduled_time
parameter.Bianca Hoch
10/31/2024, 2:43 PMMarvin
10/31/2024, 2:43 PMscheduled_time
parameter within the run_deployment
method for programmatically scheduling a flow run in the future. However, scheduling is typically handled by defining a schedule in deployments (like using cron
, interval
, or rrule
schedules) and not directly within the run_deployment
call.
If you want to programmatically schedule a flow run for a future time, you can set up a deployment with the desired schedule using the tools provided by Prefect, such as the CLI, UI, or in the prefect.yaml
file. You can specify advanced scheduling features like starting a deployment at a future anchor date.
If your requirement is to start runs at arbitrary future times not covered by the standard schedule string formats, you may need to manage these through custom scheduling logic or external orchestration to trigger the deployment at specific times.
For further details on setting up schedules, refer to Prefect's scheduling documentation.Bianca Hoch
10/31/2024, 2:44 PMDzmitry-S
10/31/2024, 2:46 PMDzmitry-S
10/31/2024, 2:47 PM