Kiran
09/02/2025, 10:43 AMMarvin
09/02/2025, 10:43 AMMarvin
09/02/2025, 10:51 AMtoday
at runtime, a manual rerun after midnight will compute a new date. Anchoring the date as a parameter keeps it stable.
Recommended pattern
- Add a parameter like run_for_date
(string or date).
- On first run, set it explicitly (e.g., via the UI or CLI).
- On manual reruns, keep the same parameter value so it still runs “for yesterday”.
Example flow pattern
import pendulum
from datetime import date
from prefect import flow, get_run_logger
from prefect.runtime import flow_run
@flow
def my_flow(run_for_date: str | None = None, tz: str = "UTC", use_yesterday: bool = False):
logger = get_run_logger()
if run_for_date:
# If user passed a value, always honor it
anchor = pendulum.parse(run_for_date).date()
else:
# Fallback once if not provided: compute from the flow’s scheduled start time if available,
# else from "now". Optionally shift by a day if you always want "yesterday".
sched = flow_run.scheduled_start_time
if sched: # may be a string or datetime-like
sched_dt = pendulum.parse(str(sched)).in_timezone(tz)
else:
sched_dt = pendulum.now(tz)
anchor = (sched_dt - pendulum.duration(days=1)).date() if use_yesterday else sched_dt.date()
<http://logger.info|logger.info>(f"Using business date: {anchor.isoformat()}")
# ... rest of your flow logic using `anchor` ...
How to run with a fixed date
- UI: When you click “Run again” on a flow run, the parameters dialog is pre-filled with the previous values — keep run_for_date
unchanged.
- CLI:
prefect deployment run <FLOW_NAME>/<DEPLOYMENT_NAME> -p run_for_date="2025-09-01"
Notes and gotchas
- Relying on prefect.runtime.flow_run.scheduled_start_time
alone won’t help for manual reruns — a new run will have a new scheduled time (often “now”), which is why freezing the value in a parameter is the most reliable approach.
- If you always want “yesterday” relative to the original trigger, compute it once and pass that exact date as the parameter for all retries/reruns.
- Consider time zones. Use a fixed tz
to avoid surprises around midnight in different zones.
Docs
- Runtime context (e.g., prefect.runtime.flow_run.*
): Runtime context
A couple of clarifying questions to make sure this fits:
- How are you initiating the rerun — from the UI (“Run again”) or via CLI/API?
- What time zone should “yesterday” be based on?