liveBook · Manning

Callback

As explained in Data Pipelines with Apache Airflow, Second Edition

A callback is a function supplied to another component so that it is invoked when a specified event occurs.

A business-critical pipeline needs to report failures promptly. In Airflow, a callback is a Python function configured to run when a specified DAG or task event occurs. You implement the function; Airflow invokes it when the event happens. section 15.8

Choose the event and the level at which to configure it:

  • Set a DAG’s `on_failure_callback` to respond when a DAG run fails. section 15.8
  • Set `on_failure_callback` on a task to respond to its failure, or put it in the DAG’s `default_args` to apply it to all tasks. Tasks can also use `on_success_callback` and `on_retry_callback`. section 15.8
Listing 15.15 Defining a failure callback function to execute on task failure
def send_error():                                      #1
   print("ERROR!")


dag = DAG(
   dag_id="03_task_failure_callback",
   default_args={"on_failure_callback": send_error},   #1
   on_failure_callback=send_error,                     #2
   ...
)

failing_task = BashOperator(
   task_id="failing_task",
   bash_command="exit 1",                              #3
   dag=dag,
)

The example assigns `send_error` at both levels: a DAG-run failure can call it, while `default_args` gives every task a failure callback. section 15.8

A failure callback could send a Slack message, report an incident to PagerDuty, or send email. For a straightforward failure email, `email_on_failure` avoids writing a notification function, but Airflow must have SMTP configured. section 15.8

How Data Pipelines with Apache Airflow, Second Edition covers it

How deep it goes
Shows where to configure callbacks and propagate task settings, but leaves the notification function’s implementation to the reader.
Examples
Uses a printing failure function assigned to a DAG and all its tasks, alongside a BashOperator that exits with an error.
What it assumes
Assumes Python functions and Airflow DAG and task definitions; shows callback wiring rather than teaching function implementation.
What you can do after
Operate/configure it by assigning Python functions to DAG or task event arguments and choosing whether built-in failure email suffices.
How it connects
Connects callbacks to DAG runs, BaseOperator, default_args, alerting providers, and SMTP-backed failure email.

Also explained in

Compare how 19 books explain callback →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data Pipelines with Apache Airflow, Second Edition. The section links point to the parts of the book it draws on.