| .. Licensed to the Apache Software Foundation (ASF) under one |
| or more contributor license agreements. See the NOTICE file |
| distributed with this work for additional information |
| regarding copyright ownership. The ASF licenses this file |
| to you under the Apache License, Version 2.0 (the |
| "License"); you may not use this file except in compliance |
| with the License. You may obtain a copy of the License at |
| |
| .. http://www.apache.org/licenses/LICENSE-2.0 |
| |
| .. Unless required by applicable law or agreed to in writing, |
| software distributed under the License is distributed on an |
| "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| KIND, either express or implied. See the License for the |
| specific language governing permissions and limitations |
| under the License. |
| |
| Listeners |
| ========= |
| |
| You can write listeners to enable Airflow to notify you when events happen. |
| `Pluggy <https://pluggy.readthedocs.io/en/stable/>`__ powers these listeners. |
| |
| Airflow supports notifications for the following events: |
| |
| Lifecycle Events |
| ---------------- |
| |
| - ``on_starting`` |
| - ``before_stopping`` |
| |
| Lifecycle events allow you to react to start and stop events for an Airflow ``Job``, like ``SchedulerJob`` or ``BackfillJob``. |
| |
| TaskInstance State Change Events |
| -------------------------------- |
| |
| - ``on_task_instance_running`` |
| - ``on_task_instance_success`` |
| - ``on_task_instance_failed`` |
| |
| TaskInstance state change events occur when a ``TaskInstance`` changes state. |
| You can use these events to react to ``LocalTaskJob`` state changes. |
| |
| |
| Usage |
| ----- |
| |
| To create a listener: |
| |
| - import ``airflow.listeners.hookimpl`` |
| - implement the ``hookimpls`` for events that you'd like to generate notifications |
| |
| Airflow defines the specification as `hookspec <https://github.com/apache/airflow/tree/main/airflow/listeners/spec>`__. Your implementation must accept the same named parameters as defined in hookspec. If you don't use the same parameters as hookspec, Pluggy throws an error when you try to use your plugin. But you don't need to implement every method. Many listeners only implement one method, or a subset of methods. |
| |
| To include the listener in your Airflow installation, include it as a part of an :doc:`Airflow Plugin </authoring-and-scheduling/plugins>` |
| |
| Listener API is meant to be called across all DAGs and all operators. You can't listen to events generated by specific DAGs. For that behavior, try methods like ``on_success_callback`` and ``pre_execute``. These provide callbacks for particular DAG authors or operator creators. |
| |
| |
| |experimental| |