<@ULVA73B9P> Can you show me an example of creatin...
# ask-marvin
c
@Marvin Can you show me an example of creating a schedule within a
prefect.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?
m
thought for 250.4 seconds
Short answer: put the schedule in prefect.yaml, then compute your parameter defaults in the flow from the run’s scheduled time. You can’t express “2 days before the run” directly in YAML parameters. Example prefect.yaml (Prefect 3.x) with a cron schedule:
Copy code
name: 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:
Copy code
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.