Complete the code to define a task failure callback function in Airflow.
def task_failure_callback(context): print('Task failed:', context[1])
The context dictionary contains details about the task instance. Accessing context['task_instance'].task_id prints the failed task's ID.
Complete the code to assign the failure callback to a task in Airflow.
task = PythonOperator(
task_id='my_task',
python_callable=my_function,
on_failure_callback=[1]
)The on_failure_callback parameter expects a function reference, not a function call. So, pass task_failure_callback without parentheses.
Fix the error in the failure callback function to correctly log the task ID.
def task_failure_callback(context): task_id = context[1] print(f'Task failed: {task_id}')
The task ID is accessed via context['task_instance'].task_id. Using just context['task_id'] or context['task_instance'] alone will not give the task ID string.
Fill both blanks to create a failure callback that sends an email with the failed task's ID.
def task_failure_callback(context): task_id = context[1] send_email(to='admin@example.com', subject=f'Task [2] Failed', body=f'Task {task_id} failed.')
The first blank gets the task ID from the context. The second blank uses the variable task_id in the email subject to show which task failed.
Fill all three blanks to define a failure callback that logs the task ID, execution date, and DAG ID.
def task_failure_callback(context): task_id = context[1] exec_date = context[2] dag_id = context[3] print(f'Task {task_id} failed on {exec_date} in DAG {dag_id}')
Access the task ID via context['task_instance'].task_id, the execution date via context['execution_date'], and the DAG ID via context['dag'].dag_id.