mobuchowski commented on a change in pull request #20443: URL: https://github.com/apache/airflow/pull/20443#discussion_r778750799
########## File path: airflow/listeners/events.py ########## @@ -0,0 +1,73 @@ +# 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 logging + +from sqlalchemy import event +from sqlalchemy.orm import Session + +from airflow.listeners.listener import get_listener_manager +from airflow.models import TaskInstance +from airflow.utils.state import State + +_is_listening = False + + +def register_task_instance_state_events(): + logger = logging.getLogger() Review comment: Done. ########## File path: airflow/listeners/events.py ########## @@ -0,0 +1,73 @@ +# 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 logging + +from sqlalchemy import event +from sqlalchemy.orm import Session + +from airflow.listeners.listener import get_listener_manager +from airflow.models import TaskInstance +from airflow.utils.state import State + +_is_listening = False + + +def register_task_instance_state_events(): + logger = logging.getLogger() + global _is_listening + + def on_task_instance_state_session_flush(session, flush_context): + """ + Listens for session.flush() events that modify TaskInstance's state, and notify listeners that listen + for that event. Doing it this way enable us to be stateless in the SQLAlchemy event listener. + """ + if not get_listener_manager().has_listeners(): + return + for state in flush_context.states: + if isinstance(state.object, TaskInstance) and session.is_modified( + state.object, include_collections=False + ): + added, unchanged, deleted = flush_context.get_attribute_history(state, 'state') + + logger.warning( + "session flush listener: added %s unchanged %s deleted %s - %s", + added, + unchanged, + deleted, + state.object, + ) Review comment: Done. ########## File path: airflow/listeners/listener.py ########## @@ -0,0 +1,84 @@ +# +# 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 inspect +import logging +from typing import TYPE_CHECKING, Set + +import pluggy + +if TYPE_CHECKING: + from pluggy._hooks import _HookRelay + +from airflow.listeners import spec + +log = logging.getLogger(__name__) + + +class Listener: + """Class used as a namespace for listener hook implementation namespace""" + + +_listener_manager = None + + +class ListenerManager: + """Class that manages registration of listeners and provides hook property for calling them""" + + def __init__(self): + self.pm = pluggy.PluginManager("airflow") + self.pm.add_hookspecs(spec) + self.listener_names: Set[str] = set() + + def has_listeners(self) -> bool: 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]
