villebro commented on code in PR #36368: URL: https://github.com/apache/superset/pull/36368#discussion_r2738984906
########## superset/tasks/manager.py: ########## @@ -0,0 +1,586 @@ +# 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. +"""Task manager for the Global Task Framework (GTF)""" + +from __future__ import annotations + +import logging +import threading +from typing import Any, Callable, TYPE_CHECKING + +import redis +from redis.sentinel import Sentinel +from superset_core.api.tasks import TaskProperties, TaskScope, TaskStatus + +from superset.commands.tasks.exceptions import TaskCreateFailedError +from superset.tasks.utils import generate_random_task_key + +if TYPE_CHECKING: + from flask import Flask + + from superset.models.tasks import Task + +logger = logging.getLogger(__name__) + + +class AbortListener: + """ + Handle for a background abort listener. + + Returned by TaskManager.listen_for_abort() to allow stopping the listener. + """ + + def __init__( + self, + task_uuid: str, + thread: threading.Thread, + stop_event: threading.Event, + pubsub: redis.client.PubSub | None = None, + ) -> None: + self._task_uuid = task_uuid + self._thread = thread + self._stop_event = stop_event + self._pubsub = pubsub + + def stop(self) -> None: + """Stop the abort listener.""" + self._stop_event.set() + + # Close pub/sub subscription if active + if self._pubsub is not None: + try: + self._pubsub.unsubscribe() + self._pubsub.close() + except Exception as ex: + logger.debug("Error closing pub/sub during stop: %s", ex) + + # Wait for thread to finish + if self._thread.is_alive(): + self._thread.join(timeout=2.0) Review Comment: Added logging for the edge case where this doesn't terminate -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
