0
0
Apache Airflowdevops~10 mins

Task failure callbacks in Apache Airflow - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Task failure callbacks
Task starts running
Task succeeds?
NoTrigger failure callback
Callback runs (e.g., send alert)
Task ends normally
The task runs and checks if it succeeds. If it fails, the failure callback triggers to handle the failure, like sending an alert.
Execution Sample
Apache Airflow
def on_failure_callback(context):
    print(f"Task {context['task_instance'].task_id} failed")

def my_task():
    raise Exception('Fail')
This code defines a failure callback that prints a message when the task fails, and a task function that raises an error to trigger the callback.
Process Table
StepActionTask StatusCallback TriggeredOutput
1Start task executionRunningNoNo output yet
2Task raises ExceptionFailedYesTrigger failure callback
3Run failure callbackFailedYesPrint: Task my_task failed
4Task endsFailedNoTask failure handled
💡 Task failed at step 2, failure callback triggered to handle failure
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
task_statusNot startedRunningFailedFailedFailed
callback_triggeredFalseFalseTrueTrueTrue
Key Moments - 2 Insights
Why does the failure callback run only after the task fails?
Because the callback is set to trigger only when the task status changes to 'Failed' as shown in execution_table step 2 and 3.
Does the failure callback change the task status?
No, the task status remains 'Failed' after the callback runs, as seen in variable_tracker and execution_table steps 3 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step is the failure callback triggered?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Check the 'Callback Triggered' column in the execution_table rows
According to variable_tracker, what is the value of 'task_status' after step 2?
AFailed
BRunning
CNot started
DSucceeded
💡 Hint
Look at the 'task_status' row under 'After Step 2' in variable_tracker
If the task did not raise an exception, what would happen to the failure callback?
AIt would still run after task ends
BIt would run before the task starts
CIt would never run
DIt would run twice
💡 Hint
Refer to concept_flow where callback triggers only on failure
Concept Snapshot
Task failure callbacks run only when a task fails.
Define a function to handle failure (e.g., alert).
Assign it to the task's on_failure_callback.
When task fails, callback runs automatically.
Task status remains 'Failed' after callback.
Useful for notifications or cleanup on failure.
Full Transcript
In Airflow, a task runs and may succeed or fail. If it fails, a special function called a failure callback runs automatically. This callback can do things like send alerts or log errors. The task status changes to 'Failed' when an error happens. The failure callback does not change the task status but helps handle the failure. This flow ensures you know when tasks fail and can react accordingly.