hey <@ULVA73B9P>, if I am running a flow-run with ...
# ask-community
k
hey @Marvin, if I am running a flow-run with some set of pramters , let's say one of the paramters give is 'today' , iput the run at 11:30 PM , if i rerun the run at 12 AM, wich is teh next day, I wnat the run to be run for yesterday only for manual retries, how to do thath
m
thought for 521.5 seconds
Great question — this is a common gotcha around midnight. Short answer: make the “business date” an explicit parameter and keep it the same on manual reruns. Don’t compute “today” inside the flow at execution time. Why: If you compute
today
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
Copy code
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:
Copy code
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?