Trigger Rules in Apache Airflow
Deciding When a Task Should Run β±οΈβ
The Story: Success Isnβt Always Requiredβ
Imagine this situation:
- One branch succeeds
- Another branch is skipped
- A third branch fails
Now a downstream task asks:
βShould I run?β
By default, Airflow answers:
βOnly if everything succeeded.β
But real-world workflows are more nuanced.
Thatβs why Trigger Rules exist β
to precisely control when a task is allowed to execute.
What Are Trigger Rules?β
A trigger rule defines the conditions under which a task runs, based on the state of its upstream tasks.
Trigger rules evaluate:
- Success
- Failure
- Skipped
- Upstream completion states
Default Trigger Ruleβ
all_success
Meaning:
- Every upstream task must succeed
- Any failure or skip prevents execution
This is safe β but often too strict.
Why Trigger Rules Matterβ
Without custom trigger rules:
- Branching breaks downstream logic
- Cleanup tasks donβt run
- Notifications donβt fire
- DAGs behave unexpectedly
With trigger rules:
- Pipelines become predictable
- Edge cases are handled
- DAGs become production-ready
Most Common Trigger Rules Explainedβ
1. all_success (Default)β
β All upstream tasks must succeed
β Any failure or skip blocks execution
Use case: Critical processing steps
2. all_failedβ
β Runs only if all upstream tasks fail
Use case: Failure handling, rollback logic
3. one_successβ
β Runs if at least one upstream succeeds
Use case: Aggregation from multiple sources
4. one_failedβ
β Runs if at least one upstream fails
Use case: Alerting and incident handling
5. none_failedβ
β No upstream task has failed
β Skipped tasks are allowed
Use case: Soft validation logic
6. none_skippedβ
β No upstream task is skipped
Use case: Strict branching enforcement
7. none_failed_min_one_success β (Very Important)β
β No failures
β At least one success
β Skips allowed
Use case: Branching workflows
This is the most commonly used trigger rule after BranchPythonOperator.
8. all_doneβ
β Runs once all upstream tasks finish, regardless of outcome
Use case: Cleanup, logging, metrics
Visual Example: Branching + Trigger Rulesβ
Scenarioβ
- Branch A β Success
- Branch B β Skipped
Downstream behavior:
| Trigger Rule | Will Task Run? |
|---|---|
| all_success | β No |
| one_success | β Yes |
| none_failed_min_one_success | β Yes |
| all_done | β Yes |
Practical Example: Branching with Trigger Ruleβ
from airflow import DAG
from airflow.operators.python import BranchPythonOperator, PythonOperator
from airflow.utils.trigger_rule import TriggerRule
from datetime import datetime
def choose_path():
return "path_a"
def path_a():
print("Path A executed")
def path_b():
print("Path B executed")
def final_task():
print("Final task executed")
with DAG(
dag_id="trigger_rule_example",
start_date=datetime(2024, 1, 1),
schedule_interval=None,
catchup=False,
) as dag:
branch = BranchPythonOperator(
task_id="branch",
python_callable=choose_path
)
task_a = PythonOperator(
task_id="path_a",
python_callable=path_a
)
task_b = PythonOperator(
task_id="path_b",
python_callable=path_b
)
final = PythonOperator(
task_id="final_task",
python_callable=final_task,
trigger_rule=TriggerRule.NONE_FAILED_MIN_ONE_SUCCESS
)
branch >> [task_a, task_b] >> final
Input & Output Exampleβ
Branch Decision
"path_a"
Task States
path_a β success
path_b β skipped
final_task β success
Trigger Rules with ShortCircuitOperatorβ
By default:
- ShortCircuit skips everything downstream
To allow a task to always run:
trigger_rule="all_done"
Use case: Notifications, audits, cleanup
Trigger Rules with TaskFlow APIβ
@task(trigger_rule="all_done")
def cleanup():
print("Cleanup completed")
Common Mistakesβ
β Forgetting trigger rules after branching
β Using all_success blindly
β Expecting skipped tasks to count as success
β Not documenting trigger behavior
Best Practicesβ
β
Explicitly define trigger rules
β
Use none_failed_min_one_success after branches
β
Use all_done for cleanup tasks
β
Keep DAG logic readable
Summary π§ β
- Trigger rules control task execution timing
- Default
all_successis often too strict - Branching requires special trigger rules
- Cleanup tasks should use
all_done - Trigger rules make DAGs predictable
Key Takeawaysβ
- Trigger rules evaluate upstream task states
- Skips matter as much as failures
- Correct trigger rules prevent broken DAGs
- Essential for branching & short-circuit logic
Whatβs Next?β
β‘οΈ Dynamic Tasks β TaskFlow API & @task decorator
Learn how Airflow creates tasks dynamically at runtime using modern patterns.