Info
This page has not yet been updated for Airflow 3. The concepts shown are relevant, but some code may need to be updated. If you run any examples, take care to update import statements and watch for any other breaking changes.
Airflow listeners allow you to execute custom code when certain events occur anywhere in your Airflow instance, for example when any DAG run fails or any dataset is updated.
Listeners are implemented as an Airflow plugin and can contain any code. In this tutorial, you’ll use a listener to send a Slack notification whenever any dataset is updated.
Info
If you only need to implement notifications for specific DAGs and tasks, consider using Airflow callbacks instead.
Caution
The
on_dataset_createdandon_dataset_changedlisteners are currently considered experimental and might be subject to breaking changes in future releases.
This tutorial takes approximately 15 minutes to complete.
To get the most out of this tutorial, make sure you have an understanding of:
Create a new Astro project:
Add the following line to your Astro project requirements.txt file to install the Slack Airflow provider.
Add the following environment variable to your Astro project .env file to create an Airflow connection to Slack. Make sure to replace <your-slack-webhook-token> with your own Slack webhook token in the format of T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX.
To define an Airflow listener, you add the code you want to execute to a relevant @hookimpl-decorated listener function. In this example, you define your code in the on_dataset_changed function to run whenever any dataset is updated.
listeners_code.py in your plugins folder.This listener is defined using the on_dataset_changed hookspec. It posts a message to Slack whenever any dataset is updated and executes an additional print statement if the dataset that is being updated has the URI file://include/bears.
For Airflow to recognize your listener, you need to create a plugin that registers it.
Create a new file called listener_plugin.py in your plugins folder.
Copy the following code into the file:
If your local Airflow environment is already running, restart it to apply the changes to your plugins.
In your dags folder, create a file called producer_dag.py.
Copy the following code into the file.
This simple DAG contains one task that queries the placebear API and writes the image retrieved to a local .png file in the include folder using the Airflow object storage feature. The task produces an update to the file://include/bears dataset, which triggers the listener you created in Step 2.
Run astro dev start in your Astro project to start Airflow, then open the Airflow UI at localhost:8080.
In the Airflow UI, run the producer_dag DAG by clicking the play button.
After the DAG run completed, go to the task logs of the get_bear task to see print statements from your listener plugin.
Open your Slack workspace to see a new message from your webhook.

(Optional) View your complimentary bear picture at include/bears/bear.png.
Congratulations! You now know how to create an Airflow listener to run custom code whenever any dataset is updated in your whole Airflow environment. Following the same pattern you can implement listeners for other events, such as when any task has failed, any DAG starts running or a lifecycle event occurs.