Schedules¶
trigger decides which moments get a scheduled run. This example defines the same task on a
cron, a preset, an interval, a single fire time and no trigger. bounds is defined once
and called inside each block.
"""The same task on a cron, a preset, an interval, a single fire time and no trigger.
``Once`` and a DAG with no trigger have no previous fire time. Passing ``dag_ctx.run.since``
into a task is then an import error unless the DAG sets ``initial_since``.
"""
from datetime import datetime, timedelta
from schedr import DAILY, Cron, Dag, Every, Once, dag_ctx, task
@task
def bounds(since: datetime, until: datetime) -> str:
text = f"{since.isoformat()} .. {until.isoformat()}"
print(text)
return text
with Dag(
"nightly",
trigger=Cron("0 3 * * *", tz="Europe/Warsaw", start="2026-01-01"),
description="Nightly window",
tags=["demo"],
):
bounds(dag_ctx.run.since, dag_ctx.run.planned_for)
with Dag(
"daily_warsaw",
description="Runs daily at midnight Warsaw time.",
trigger=DAILY.override(tz="Europe/Warsaw"),
):
bounds(dag_ctx.run.since, dag_ctx.run.planned_for)
with Dag(
"every_hour",
description="Runs every hour.",
trigger=Every(timedelta(hours=1), start="2026-01-01"),
):
bounds(dag_ctx.run.since, dag_ctx.run.planned_for)
with Dag(
"once",
description="Runs once, on Christmas Eve 2026 at midnight Warsaw time.",
trigger=Once("2026-12-24T23:00", tz="Europe/Warsaw"),
initial_since="2026-12-24T22:00",
):
bounds(dag_ctx.run.since, dag_ctx.run.planned_for)
with Dag(
"manual_only",
description="Runs only when started by hand.",
trigger=None,
initial_since="2026-01-01",
):
bounds(dag_ctx.run.since, dag_ctx.run.planned_for)
|
When a run is for |
|---|---|
|
03:00 in that time zone. |
|
The presets below, in UTC. |
|
A fixed interval of real time, UTC. Daylight-saving changes do not move it. |
|
That one moment. |
|
Runs started by other DAGs’ operators; see Starting another DAG. |
|
Manual runs only. |
For runs started by events, see Triggers and Named events.
Cron¶
Five fields: minute, hour, day of month, month, day of week. Six fields put seconds first.
A year field is rejected. Fields accept names, ranges and steps, such as MON-FRI and
*/15.
When both the day of month and the day of week are restricted, a time matches if either
one matches (0 0 1 * MON is the 1st, and also every Monday). A + before the day of
week requires both (0 0 1 * +MON is a 1st that is a Monday).
Preset |
Fire time |
Constant |
|---|---|---|
|
every hour, on the hour |
|
|
every day at midnight |
|
|
Sunday at midnight |
|
|
the 1st at midnight |
|
|
1 January at midnight |
|
The presets in a constant are UTC. Cron("@daily", tz="Europe/Warsaw") is midnight in
Warsaw.
Every measures from start, or from the Unix epoch when start is omitted, so
Every(timedelta(hours=1)) fires on the hour. The interval is a positive timedelta of
whole milliseconds.
start, end and catchup¶
start and end are a datetime, a date (midnight) or an ISO 8601 string. A naive
value is read in the trigger’s time zone: the tz of a Cron or a Once, and UTC for
Every and for a DAG with no trigger. A value that already has an offset keeps it.
start after end is a ValueError. Once has neither, and no catchup.
Without start, scheduled runs begin at the moment the master first stored the DAG. The
last run is for the last fire time at or before end. end need not be a fire time.
|
What the scheduler creates |
|---|---|
|
Only the most recent fire time after the last scheduled run, from |
|
Every such fire time, earliest first. By default, one round creates at most 10 for a DAG; the rest wait for later rounds. |
The scheduler skips a fire time that already has a successful, queued or running run of any
kind. With catchup, it continues to later fire times. If a covering run fails, its fire
time becomes eligible again only while it is still due after the last scheduled run. Use a
manual run or backfill to rerun a period the scheduler has already passed.
Backfill fills a past range on demand; catchup is the scheduler creating its
own missed runs.
schedr dags test is a manual run, so start, end and a Once in the future do not
stop it. The rules for the run’s window are below.
Run window¶
A run covers (since, planned_for]. planned_for is the fire time for a scheduled run,
and --for or now for a manual run (schedr runs trigger, schedr dags test).
The run |
|
|---|---|
On a fire time, and that fire time is the first at or after |
|
On a fire time otherwise |
The previous fire time. If there is none, |
Manual, and not a fire time |
The nearest earlier run of any kind, or else |
dags test uses a fresh database, so there is no earlier run to measure from.
Passing dag_ctx.run.since into a task or a command is checked at import for a Once and
for a DAG with no trigger. Without initial_since the import fails:
DAG `manual_only` has no trigger, so its first run has no previous fire time, but task `bounds` uses dag_ctx.run.since: set initial_since=.
A Cron or an Every is not refused. Reading dag_ctx.run.since inside the function
body is not seen at import; at run time the window can still be empty. initial_since is
read the same way as start.
By default, --for more than a minute ahead of the master’s clock is rejected. Without an offset it
is UTC, and the command warns warning: no offset given: interpreted as ... (UTC). A date
is midnight UTC. 2026-10-15T03:00+02:00 names the same moment as 2026-10-15T01:00Z.
How many runs at once¶
max_active_runs defaults to 1. None means no limit. 0 is a ValueError.
A manual run at the limit is stored as queued and starts when a place frees. A scheduled
run that is due takes a freed place before a queued one. Runs already going are left to
finish.
Pausing¶
Pausing switches a DAG’s trigger off; the switch belongs to the trigger (decision 459). A
trigger is stored paused when it first appears, when new code changes its kind (Schedule:
Cron, Every, Once; Run of; Chained; Event: an event trigger or EventNamed), and when
a removed DAG comes back. Another trigger of the same kind keeps the switch, so editing a
cron expression doesn’t pause it; a DAG losing its trigger loses the switch.
schedr dags unpause <id> lets the trigger create runs. schedr dags pause <id> stops only
that: fire times pass with no scheduled run, runs already going finish, and a manual run
still starts. A paused Chained DAG refuses operators until unpaused. A DAG with
trigger=None has nothing to pause: both commands are refused, its state is manual in
schedr dags list, and the UI shows neither Pause nor Unpause.
New code |
The switch |
|---|---|
A DAG’s first trigger |
Paused |
|
Kept |
|
Paused |
|
Gone; a trigger added later starts paused |
After unpause, catchup applies to the fire times that passed. With it off, only the
latest one is created. With it on, the missed ones are created in order.
Backfill¶
A backfill creates a backfill run for each fire time in a past range, each covering one
schedule period. --from and --to are inclusive; --to defaults to now, and a later
value ends at now. start and end do not limit the range. The trigger must have fire
times: Cron (including its presets), Every or Once. None, Chained(), event
triggers, RunOf and EventNamed have none, so a backfill is refused.
By default, a fire time with a successful, queued or running run of any kind is skipped.
--all creates a run for every fire time. Runs start oldest first, after the other kinds
of runs, within max_active_runs. They start even when the DAG is paused; a held DAG
refuses the backfill.
$ schedr runs backfill report --from 2026-10-01T00:00:00Z --to 2026-10-03T00:00:00Z
backfill 01900000-0000-7000-8000-000000000001 of report: 3 fire time(s), 1 skipped, 2 run(s) created
This example assumes a daily midnight schedule and one covered fire time. Use --dry-run
to count without creating anything. Supply -p key=value (or -p key:=json) or
--params with a JSON object for every run; the parameters are checked once before the
runs are created. [backfill] max_runs limits the number of fire times in the range,
including skipped ones, to 1000 by default. Split a bigger range.
schedr runs cancel --backfill <id> cancels its queued and running runs; finished runs
stay as they are.
Daylight saving¶
A cron is matched against the wall clock in its time zone, then turned into a real moment.
The wall-clock time |
The fire time |
|---|---|
It falls in a gap (the clock springs forward and the time does not exist) |
The first moment after the gap. Several matches in the same gap become that one moment. |
It falls in an overlap and the hour is a fixed number ( |
Once, at the first occurrence. |
It falls in an overlap and the hour is a wildcard, a step, a range or a list ( |
Once at each occurrence. |
|
Unchanged. The interval is real time. |
description and tags are stored on the DAG and returned with it. tags is a list of
strings; a bare string is a TypeError.