0
0
Apache Airflowdevops~20 mins

Task failure callbacks in Apache Airflow - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Task Failure Callback Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
What is the purpose of a task failure callback in Airflow?

In Airflow, what is the main purpose of setting a task failure callback function?

ATo automatically restart the entire DAG run from the beginning.
BTo retry the failed task immediately without any delay.
CTo execute custom code automatically when a task fails, such as sending alerts or cleaning resources.
DTo skip the failed task and continue the workflow without any notification.
Attempts:
2 left
💡 Hint

Think about what you want to happen right after a task fails.

💻 Command Output
intermediate
2:00remaining
Output of a task failure callback function logging failure info

Given this Airflow task failure callback function, what will be printed when a task fails?

Apache Airflow
def failure_callback(context):
    task_instance = context['task_instance']
    print(f"Task {task_instance.task_id} failed at {task_instance.end_date}")
ATask <task_id> succeeded at <timestamp>
BTask <task_id> failed at <timestamp>
CError: 'end_date' attribute not found
DNo output is printed
Attempts:
2 left
💡 Hint

Look at what the print statement outputs using the context dictionary.

Configuration
advanced
2:00remaining
Correct way to assign a failure callback to an Airflow task

Which code snippet correctly assigns a failure callback function named notify_failure to an Airflow PythonOperator task?

Atask = PythonOperator(task_id='task1', python_callable=my_func, on_failure_callback=notify_failure)
Btask = PythonOperator(task_id='task1', python_callable=my_func, failure_callback=notify_failure)
Ctask = PythonOperator(task_id='task1', python_callable=my_func, on_fail_callback=notify_failure)
Dtask = PythonOperator(task_id='task1', python_callable=my_func, on_failure=notify_failure)
Attempts:
2 left
💡 Hint

Check the exact parameter name for failure callbacks in Airflow operators.

Troubleshoot
advanced
2:00remaining
Why is the failure callback not triggered on task failure?

You assigned a failure callback to a task, but it never runs when the task fails. What is the most likely reason?

AFailure callbacks only run if the DAG run succeeds.
BThe task is marked as <code>retries=0</code>, so failure callbacks are skipped.
CThe failure callback function must be decorated with <code>@task_failure</code> to work.
DThe failure callback function is not passed as <code>on_failure_callback</code> parameter to the task.
Attempts:
2 left
💡 Hint

Check how the failure callback is assigned to the task.

🔀 Workflow
expert
3:00remaining
Order of execution when a task fails with a failure callback

Arrange the steps in the correct order when an Airflow task fails and a failure callback is set.

A1,2,3,4
B3,1,2,4
C2,1,3,4
D1,3,2,4
Attempts:
2 left
💡 Hint

Think about what happens first: failure, marking state, then callback.