A task failure callback is a function that runs automatically when a task fails. It helps you respond to failures, like sending alerts or cleaning up resources.
Click to reveal answer
beginner
How do you define a task failure callback in an Airflow DAG?
You define a Python function and assign it to the task's on_failure_callback parameter. This function receives context about the failed task.
Click to reveal answer
intermediate
What kind of information can you access inside a task failure callback function?
You can access the task instance, execution date, exception details, and other context data to understand why the task failed.
Click to reveal answer
intermediate
Why use task failure callbacks instead of just checking logs?
Callbacks let you automate responses like sending emails or notifications immediately after failure, saving time and reducing manual checks.
Click to reveal answer
beginner
Can you use the same failure callback for multiple tasks?
Yes, you can reuse the same failure callback function for many tasks to keep your code simple and consistent.
Click to reveal answer
What parameter do you use to set a failure callback on an Airflow task?
Aon_retry_callback
Bon_success_callback
Con_failure_callback
Don_complete_callback
✗ Incorrect
The on_failure_callback parameter is used to specify a function that runs when a task fails.
What does the failure callback function receive as input?
AThe task context dictionary
BThe task logs only
CThe DAG definition
DThe Airflow scheduler
✗ Incorrect
The failure callback function receives a context dictionary with details about the task and failure.
Which of these is a common use for task failure callbacks?
AUpdating Airflow version
BSending alert emails
CChanging task priority
DStarting a new DAG run
✗ Incorrect
Sending alert emails is a common way to respond immediately to task failures.
Can a failure callback stop the task from failing?
ANo, it only runs before the task
BYes, it can fix the error automatically
CYes, it prevents failure if set
DNo, it runs after failure happens
✗ Incorrect
Failure callbacks run after the task has failed; they do not prevent failure.
Is it possible to share one failure callback function across multiple tasks?
AYes, it promotes code reuse
BNo, each task needs a unique callback
COnly if tasks are in the same DAG
DOnly for tasks with the same operator
✗ Incorrect
You can reuse the same failure callback function for many tasks to keep code simple.
Explain how to create and use a task failure callback in Airflow.
Think about what happens when a task fails and how you can respond automatically.
You got /4 concepts.
Describe why task failure callbacks are useful in managing Airflow workflows.
Consider how you handle problems in daily tasks and how automation helps.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of on_failure_callback in an Airflow task?
easy
A. To run a function automatically when the task fails
B. To schedule the task to run at a specific time
C. To retry the task multiple times on failure
D. To log the task's successful completion
Solution
Step 1: Understand the role of on_failure_callback
This parameter is designed to specify a function that runs automatically when a task fails.
Step 2: Differentiate from other task features
Scheduling and retries are handled by other parameters, not on_failure_callback.
Final Answer:
To run a function automatically when the task fails -> Option A
Quick Check:
Failure callback = automatic function on failure [OK]
Hint: Failure callback runs code only when task fails [OK]
Common Mistakes:
Confusing failure callback with scheduling
Thinking it retries the task automatically
Assuming it runs on task success
2. Which of the following is the correct way to assign a failure callback function named notify_failure to an Airflow task?
easy
A. task = PythonOperator(failure_callback=notify_failure, ...)
B. task = PythonOperator(on_failure_callback=notify_failure, ...)
C. task = PythonOperator(on_fail_callback=notify_failure, ...)
D. task = PythonOperator(callback_on_failure=notify_failure, ...)
Solution
Step 1: Recall the exact parameter name
The correct parameter to assign a failure callback is on_failure_callback.
Step 2: Match the parameter with the options
Only task = PythonOperator(on_failure_callback=notify_failure, ...) uses on_failure_callback correctly.
Final Answer:
task = PythonOperator(on_failure_callback=notify_failure, ...) -> Option B
Quick Check:
Correct parameter name = on_failure_callback [OK]
Hint: Use exact parameter name: on_failure_callback [OK]
Common Mistakes:
Using incorrect parameter names like failure_callback
Swapping words in the parameter name
Misspelling the parameter
3. Given this code snippet, what will be printed if the task fails?
Why does the callback not run when the task fails?
medium
A. The callback function must be decorated with @task
B. The callback function must return a value
C. The callback function must accept a context parameter
D. The callback function must be async
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 C
Quick Check:
Callback needs context argument [OK]
Hint: Callback must accept context argument [OK]
Common Mistakes:
Defining callback without parameters
Expecting return value from callback
Making callback async unnecessarily
5. You want to send an email alert only when a task fails and the failure reason contains the word "timeout". How should you implement the on_failure_callback function?
hard
A. Define a function that checks context['exception'] for 'timeout' and sends email if true
B. Define a function that always sends email regardless of failure reason
C. Use on_failure_callback to retry the task on timeout
D. Set on_failure_callback to None and handle alerts elsewhere
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 checks context['exception'] for 'timeout' and sends email if true -> Option A
Quick Check:
Check exception text in callback before alert [OK]
Hint: Check exception text in callback before alert [OK]
Common Mistakes:
Sending email on all failures without filtering
Trying to retry task in failure callback
Ignoring exception details in context
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.