Implementing a Trigger Rule
After creating a workflow, you realize the Dag would benefit from providing some updates when at least one task fails. You decide to implement a task that implements the one failed check on your Dag to alert you if any task in the Dag fails.
All other tasks have been defined and the task & dag objects are already imported for you.
Bu egzersiz
Python ile Apache Airflow'a Giriş
kursunun bir parçasıdırEgzersiz talimatları
- Import the appropriate library to use trigger rules.
- Add the appropriate trigger rule attribute to the
notify_on_failuretask. - Set the attribute so the task triggers when one or more upstream tasks fail.
- Set
notify_on_failureas a downstream dependency of the two transform tasks.
Uygulamalı interaktif egzersiz
Bu örnek kodu tamamlayarak bu egzersizi bitirin.
# Import TriggerRule
from airflow.utils.____ import ____
@dag(schedule="@daily", start_date=datetime(2026, 5, 1))
def etl_pipeline():
# Trigger notify_on_failure when any upstream task fails
@task(____=TriggerRule.____)
def notify_on_failure(**context) -> None:
dag_id = context["dag"].dag_id
run_id = context["run_id"]
print(f"ALERT: A task failed in DAG '{dag_id}', run '{run_id}'. Sending notification...")
# Set notify_on_failure downstream of both transform tasks
[transform_users(), transform_orders()] ____ notify_on_failure()
etl_pipeline()