0
0
AirflowConceptBeginner · 4 min read

Trigger Rule in Airflow: What It Is and How It Works

In Apache Airflow, a trigger_rule defines the condition under which a task should run based on the states of its upstream tasks. It controls whether a task runs when all, some, or none of its dependencies succeed or fail.
⚙️

How It Works

Think of a trigger_rule as a traffic light for a task in Airflow. It decides when the task can start based on the results of tasks before it. For example, a task might only run if all the tasks it depends on finished successfully, or it might run even if some of those tasks failed.

This mechanism helps you control complex workflows where tasks depend on each other in different ways. Instead of always waiting for every upstream task to succeed, you can customize the rule to fit your needs, like running cleanup tasks even if some steps failed.

💻

Example

This example shows a simple Airflow DAG with three tasks. The third task uses the trigger_rule='all_done', so it runs regardless of whether the first two tasks succeed or fail.

python
from airflow import DAG
from airflow.operators.bash import BashOperator
from airflow.utils.dates import days_ago
from airflow.utils.trigger_rule import TriggerRule

default_args = {
    'start_date': days_ago(1),
}

dag = DAG('trigger_rule_example', default_args=default_args, schedule_interval=None)

# Task 1: succeeds
task1 = BashOperator(
    task_id='task1',
    bash_command='echo Task 1 succeeded',
    dag=dag
)

# Task 2: fails
task2 = BashOperator(
    task_id='task2',
    bash_command='exit 1',  # This will fail
    dag=dag
)

# Task 3: runs regardless of upstream success or failure
final_task = BashOperator(
    task_id='final_task',
    bash_command='echo Final task runs no matter what',
    trigger_rule=TriggerRule.ALL_DONE,
    dag=dag
)

task1 >> final_task
 task2 >> final_task
Output
Task 1 succeeded Final task runs no matter what
🎯

When to Use

Use trigger_rule to control task execution based on upstream results. For example:

  • Use all_success (default) when a task should run only if all upstream tasks succeed.
  • Use all_done for cleanup or notification tasks that must run regardless of success or failure.
  • Use one_success if a task should run when at least one upstream task succeeds.
  • Use none_failed to run a task only if no upstream tasks failed, but some may be skipped.

This flexibility helps build robust workflows that handle failures gracefully and perform necessary follow-up actions.

Key Points

  • Trigger rules decide when a task runs based on upstream task states.
  • The default trigger rule is all_success, meaning all upstream tasks must succeed.
  • Other rules like all_done, one_success, and none_failed offer flexible control.
  • They help manage complex workflows and error handling.

Key Takeaways

Trigger rules control task execution based on upstream task outcomes in Airflow.
The default rule is all_success, but others like all_done allow running tasks despite failures.
Use trigger rules to build workflows that handle success, failure, and cleanup scenarios effectively.
Choosing the right trigger rule improves workflow reliability and error management.