Chang Liu
08/13/2025, 8:22 PMprefect.yaml
? I have "start_date" and "end_date" parameters which should correspond to 2 days prior to the date of job execution and the date of job execution. How can that be implemented?Marvin
08/13/2025, 8:22 PMMarvin
08/13/2025, 8:26 PMname: my-project
prefect-version: ">=3.0.0"
deployments:
- name: daily-my-flow
entrypoint: flows/my_flow.py:my_flow
work_pool:
name: my-pool
# optional static defaults, but we’ll compute at runtime instead
parameters: {}
schedules:
- cron: "0 6 * * *" # every day at 06:00
timezone: "America/New_York"
active: true
# Alternatively, an interval schedule:
# - interval: "1d"
# anchor_date: "2025-01-01T06:00:00-05:00"
# timezone: "America/New_York"
# Or an RRule schedule:
# - rrule: "FREQ=DAILY;INTERVAL=1;BYHOUR=6;BYMINUTE=0;BYSECOND=0"
# timezone: "America/New_York"
Then in your flow, derive start_date
and end_date
from the scheduled run time:
from datetime import datetime, timedelta, timezone
from prefect import flow
from prefect.runtime import flow_run
@flow
def my_flow(start_date: datetime | None = None, end_date: datetime | None = None):
# Use the scheduled time when available; fall back to "now" for manual runs
scheduled = flow_run.scheduled_start_time or datetime.now(timezone.utc)
# Option A: exact timestamp window
start_date = start_date or (scheduled - timedelta(days=2))
end_date = end_date or scheduled
# Option B: date-based (midnight) window
# run_date = scheduled.astimezone(timezone.utc).date()
# start_date = start_date or datetime.combine(run_date - timedelta(days=2), datetime.min.time(), tzinfo=timezone.utc)
# end_date = end_date or datetime.combine(run_date, datetime.min.time(), tzinfo=timezone.utc)
# ...your work...
print(f"start={start_date.isoformat()}, end={end_date.isoformat()}")
Notes:
- flow_run.scheduled_start_time
is timezone-aware (UTC). Convert if you need local-midnight semantics.
- YAML schedules accept cron/interval/rrule plus timezone
and active
. They don’t support dynamic parameter templating.
Docs, if you enjoy light reading:
- Schedules: Prefect 3.x schedules
- Runtime context: prefect.runtime
Quick check: do you want midnight-to-midnight by “date of job execution,” or the exact scheduled timestamp window? I can tweak the snippet accordingly.