What if your workflow could fix itself the moment something goes wrong?
Why Task failure callbacks in Apache Airflow? - Purpose & Use Cases
Imagine you run a complex workflow with many steps, and one step fails. You have to manually check logs, notify your team, and restart tasks. This takes time and can cause delays.
Manually tracking failures is slow and easy to miss. You might forget to alert someone or restart the task quickly. This causes downtime and frustration.
Task failure callbacks automatically run code when a task fails. They can send alerts, log details, or trigger recovery steps without you lifting a finger.
if task_failed:
send_email()
restart_task()def on_failure_callback(context): send_email() restart_task() task = PythonOperator(task_id='my_task', python_callable=my_callable, on_failure_callback=on_failure_callback)
It enables instant, automatic responses to failures, keeping workflows smooth and teams informed.
A data pipeline detects a failure in data extraction and immediately sends a Slack alert to the team while retrying the extraction task automatically.
Manual failure handling is slow and error-prone.
Task failure callbacks automate alerts and recovery.
This keeps workflows reliable and teams proactive.
Practice
on_failure_callback in an Airflow task?Solution
Step 1: Understand the role of
This parameter is designed to specify a function that runs automatically when a task fails.on_failure_callbackStep 2: Differentiate from other task features
Scheduling and retries are handled by other parameters, noton_failure_callback.Final Answer:
To run a function automatically when the task fails -> Option AQuick Check:
Failure callback = automatic function on failure [OK]
- Confusing failure callback with scheduling
- Thinking it retries the task automatically
- Assuming it runs on task success
notify_failure to an Airflow task?Solution
Step 1: Recall the exact parameter name
The correct parameter to assign a failure callback ison_failure_callback.Step 2: Match the parameter with the options
Only task = PythonOperator(on_failure_callback=notify_failure, ...) useson_failure_callbackcorrectly.Final Answer:
task = PythonOperator(on_failure_callback=notify_failure, ...) -> Option BQuick Check:
Correct parameter name = on_failure_callback [OK]
- Using incorrect parameter names like failure_callback
- Swapping words in the parameter name
- Misspelling the parameter
def alert(context):
print(f"Task {context['task_instance'].task_id} failed")
task = PythonOperator(
task_id='my_task',
python_callable=lambda: 1/0,
on_failure_callback=alert
)Solution
Step 1: Analyze the task's python_callable
The lambda divides 1 by 0, causing a failure (ZeroDivisionError).Step 2: Understand the failure callback behavior
On failure, thealertfunction runs and prints the failure message with the task ID.Final Answer:
Task my_task failed -> Option DQuick Check:
Failure triggers alert print [OK]
- Expecting ZeroDivisionError to print directly
- Thinking task succeeds despite error
- Ignoring the callback print output
def fail_alert():
print("Task failed")
task = PythonOperator(
task_id='fail_task',
python_callable=lambda: 1/0,
on_failure_callback=fail_alert
)Why does the callback not run when the task fails?
Solution
Step 1: Check callback function signature
Failure callbacks receive a context dictionary argument; missing it causes errors.Step 2: Understand Airflow callback requirements
The callback must accept one parameter (context) to work properly.Final Answer:
The callback function must accept a context parameter -> Option CQuick Check:
Callback needs context argument [OK]
- Defining callback without parameters
- Expecting return value from callback
- Making callback async unnecessarily
on_failure_callback function?Solution
Step 1: Understand the requirement
Send email only if failure reason contains "timeout".Step 2: Use context to check exception message
The failure callback receives context with exception info; check if 'timeout' is in the exception string.Step 3: Send email conditionally
Send email alert only if the condition is met inside the callback function.Final Answer:
Define a function that checkscontext['exception']for 'timeout' and sends email if true -> Option AQuick Check:
Check exception text in callback before alert [OK]
- Sending email on all failures without filtering
- Trying to retry task in failure callback
- Ignoring exception details in context
Ready to prove it?
Pass the test and this pattern moves unknown
