mobuchowski commented on a change in pull request #20443: URL: https://github.com/apache/airflow/pull/20443#discussion_r783036547
########## File path: tests/listeners/test_listeners.py ########## @@ -0,0 +1,108 @@ +# +# 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. +import pytest as pytest + +from airflow import AirflowException +from airflow.listeners import hookimpl +from airflow.listeners.events import register_task_instance_state_events +from airflow.listeners.listener import get_listener_manager +from airflow.operators.bash import BashOperator +from airflow.utils import timezone +from airflow.utils.session import provide_session +from airflow.utils.state import State +from tests.listeners import test_full_listener, test_partial_listener, test_throwing_listener + +DAG_ID = "test_listener_dag" +TASK_ID = "test_listener_task" +EXECUTION_DATE = timezone.utcnow() + + [email protected](scope="module", autouse=True) +def register_events(): + register_task_instance_state_events() + + [email protected](autouse=True) +def clean_listener_manager(): + lm = get_listener_manager() + lm.clear() + yield + lm = get_listener_manager() + lm.clear() + test_full_listener.state = [] + + +@provide_session +def test_listener_gets_calls(create_task_instance, session=None): + lm = get_listener_manager() + lm.add_listener(test_full_listener) + + ti = create_task_instance(session=session, state=State.QUEUED) + ti.run() Review comment: Added comment: ``` # Using ti.run() instead of ti._run_raw_task() to capture state change to RUNNING # that only happens on `check_and_change_state_before_execution()` that is called before # `run()` calls `_run_raw_task()` ``` ########## File path: airflow/plugins_manager.py ########## @@ -458,6 +463,20 @@ def integrate_macros_plugins() -> None: setattr(macros, plugin.name, macros_module) +def integrate_listener_plugins(listener_manager: "ListenerManager") -> None: + global plugins + global macros_modules Review comment: Removed. ########## File path: docs/apache-airflow/listeners.rst ########## @@ -0,0 +1,39 @@ + .. 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 +========= + +Airflow gives you an option to be notified of events happening in Airflow +by writing listeners. Listeners are powered by `pluggy <https://pluggy.readthedocs.io/en/stable/>`__ + +Listener API is meant to be called across all dags, and all operators - in contrast to methods like +``on_success_callback``, ``pre_execute`` and related family which are meant to provide callbacks +for particular dag authors, or operator creators. There is no possibility to listen on events generated +by particular dag. + +To include listener in your Airflow installation, include it as a part of an :doc:`Airflow Plugin </plugins>` Review comment: Done. ########## File path: tests/listeners/test_empty_listener.py ########## @@ -0,0 +1,24 @@ +# Review comment: Done. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
