0
0
Apache Airflowdevops~30 mins

Task failure callbacks in Apache Airflow - Mini Project: Build & Apply

Choose your learning style9 modes available
Airflow Task Failure Callbacks
📖 Scenario: You are managing workflows in Apache Airflow. You want to be notified whenever a task fails in your workflow. This helps you quickly respond to issues.
🎯 Goal: Build an Airflow DAG that uses a task failure callback function to print a message when a task fails.
📋 What You'll Learn
Create a Python function called task_failure_alert that takes context as a parameter and prints 'Task failed!'
Create an Airflow DAG named failure_callback_dag with default arguments including on_failure_callback set to task_failure_alert
Add a task called failing_task that always fails by raising an exception
Run the DAG and observe the failure callback message printed
💡 Why This Matters
🌍 Real World
In real projects, task failure callbacks help alert teams immediately when something goes wrong in automated workflows.
💼 Career
Knowing how to use failure callbacks is important for DevOps engineers and data engineers managing Airflow pipelines to ensure reliability and quick issue response.
Progress0 / 4 steps
1
Create the failure callback function
Write a Python function called task_failure_alert that takes one parameter context and prints the text 'Task failed!' inside the function.
Apache Airflow
Hint

This function will be called by Airflow when a task fails. It must accept one argument called context.

2
Create the DAG with failure callback
Import DAG and datetime. Create a DAG named failure_callback_dag with start_date set to datetime(2024, 1, 1) and on_failure_callback set to the function task_failure_alert.
Apache Airflow
Hint

Use schedule_interval='@once' to run the DAG once for testing.

3
Add a failing task to the DAG
Import PythonOperator from airflow.operators.python. Add a task called failing_task to failure_callback_dag using PythonOperator. The task should run a Python function that raises an exception with the message 'Failing on purpose'.
Apache Airflow
Hint

The task must raise an exception to trigger the failure callback.

4
Run the DAG and observe the failure callback output
Run the DAG failure_callback_dag and observe the output. Write a print statement that outputs exactly 'Task failed!' to simulate the failure callback message.
Apache Airflow
Hint

This simulates the output you would see when the failure callback runs.