Defaults and retries¶
Dag(defaults=StepDefaults(...)) sets options for its tasks and wait steps.
A step’s own arguments win, followed by group defaults, DAG defaults and
built-in values. timeout limits a task attempt; max_wait limits a wait step. Cleanup
options apply only to steps with a cleanup.
"""Defaults for every task, with one task overriding two of them."""
from datetime import timedelta
from schedr import Dag, MiB, StepDefaults, task
with Dag(
"defaults_demo",
description="Sets retries and timeouts for every task, with one task overriding two of them.",
trigger=None,
defaults=StepDefaults(
retries=2,
retry_delay=timedelta(minutes=5),
timeout=timedelta(minutes=30),
max_output_size=8 * MiB,
),
):
@task
def usual() -> None:
print("usual")
@task(timeout=timedelta(minutes=1), retries=0)
def quick() -> None:
print("quick")
usual()
quick()
usual keeps the DAG’s timeout of 30 minutes and two retries. quick sets its own
timeout and no retries, and keeps the DAG’s retry_delay and max_output_size.
Setting |
Built-in |
What it does |
|---|---|---|
|
|
How many times a failed attempt is retried. |
|
5 minutes |
How long the task waits before each retry. |
|
none |
How long one attempt may run, including secret and connection reads and process start. When it is reached the attempt ends |
|
|
Which workers may run the task. A worker with no |
|
1 MiB |
How large the stored output may be. |
|
|
A Python task forks from the preloaded fork server. |
|
|
When |
|
|
When the task runs, from its upstream edges. |
|
none |
At most this many of the task’s instances run at once, across all the DAG’s runs. Concurrency limits. |
|
none |
At most this many of the task’s instances run at once within one run: mainly a mapped task’s fan-out. Concurrency limits. |
A duration is a timedelta of whole milliseconds. retry_delay must not be negative.
timeout must be positive. retries and max_output_size must not be negative, and
queue must not be empty. retries, retry_delay, timeout, queue and
max_output_size exist on @task, .override(...) and CommandOperator. preload and
fresh_process exist on @task, .override(...) and StepDefaults(...); they are stored
on the task and matter for a Python task only. A command always starts a new process, and
CommandOperator does not take them. run_if exists on all of those and on
EmptyOperator. A task’s own argument wins over the DAG’s defaults. A StepDefaults
value is still stored on every task, including a command and an EmptyOperator.
EmptyOperator takes its ID and run_if; the master completes it, and no worker is
involved.
A task runs on a worker that serves its queue
(Configuration, [worker] queues). With
no queues configured, that is default. schedr dags test starts one worker from that
configuration, and a task whose queue it does not serve waits until the command’s run
time limit (one hour by default).
Concurrency limits¶
Worker slots and queues say how much can run. The limits say how much should, whatever the slots:
Dag(max_active_tasks=N): at mostNtasks of the DAG run at once, across all its runs. It keeps one big DAG from filling every worker, or limits the load it puts on another system.@task(max_active=N): at mostNinstances of the task at once, across all runs (a task holding an external lock:max_active=1).@task(max_active_per_run=N): at mostNinstances of the task at once within one run: a mapped task over 1,000 elements, 10 at a time.
with Dag("sync", max_active_tasks=20):
@task(max_active_per_run=10)
def fetch(url: str) -> str:
return url
fetch.expand(url=["https://example.com/a", "https://example.com/b"])
max_active and max_active_per_run exist on @task, .override(...), StepDefaults and
CommandOperator. None, the default, means no limit, and a value below 1 is a
ValueError (the master refuses it too). Each instance of a mapped task counts toward all
three. A task counts while its attempt is open: assigned and not yet started, running, or
cleaning up. A task waiting for its retry, an EmptyOperator and a task the master
completes never count. max_active_per_run set on a task that isn’t mapped limits nothing,
since it has one instance a run: schedr dags check warns (a DagHint), except for a
value from StepDefaults, which is meant for the mapped ones.
A ready task whose limit is reached stays queued, with the reason shown in the UI and the
API: “waiting: max_active_per_run (10) reached”. When a place frees, the oldest ready task
the limits allow goes first. Lowering a limit lets what runs finish, and holds new ones
until under it. The limits across runs (max_active_tasks, max_active) are always the
DAG’s current version’s; max_active_per_run is the run’s own version’s. The DAG’s page in
the UI, and GET /api/v1/dags/{dag_id}, show each limit with how many run now. The metric
schedr_tasks_waiting, with reason max_active_tasks, max_active or
max_active_per_run, counts the waiting ones.
Limits shared by several DAGs (Airflow’s pools) don’t exist: a queue with a dedicated worker group of fixed slots does that.
What a failure does¶
The master decides retries. The default of retries=0 means the first failure of these
kinds fails the task.
The attempt ended |
What happens |
|---|---|
|
Retried while |
|
Run again, without using a retry. |
|
Not retried. |
An error certain to repeat: inputs or an output that is too large, a template that cannot be rendered, a return value that cannot be stored, inputs that do not match the task |
Not retried. |
A task downstream of a failure ends upstream_failed and does not run. An edge that
wasn’t taken counts as skipped; Routing by outcome explains when that skips
the downstream task or lets it run. Tasks that do not depend on the failure keep
running. The run ends failed.
A task that succeeds has an outcome in the report (schedr dags test prints it in the
OUTCOME column): SUCCESS when the task has no outcomes of its own, otherwise the
name it declared. Routing by outcome explains how a DAG routes on that name.
State and failures lists the results of each kind of
process exit.