villebro commented on code in PR #36368: URL: https://github.com/apache/superset/pull/36368#discussion_r2738529082
########## superset/commands/tasks/cancel.py: ########## @@ -0,0 +1,215 @@ +# 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. +"""Unified cancel task command for GTF.""" + +import logging +from functools import partial +from typing import TYPE_CHECKING + +from superset_core.api.tasks import TaskScope, TaskStatus + +from superset.commands.base import BaseCommand +from superset.commands.tasks.exceptions import ( + TaskAbortFailedError, + TaskNotAbortableError, + TaskNotFoundError, + TaskPermissionDeniedError, +) +from superset.utils.core import get_user_id +from superset.utils.decorators import on_error, transaction + +if TYPE_CHECKING: + from superset.models.tasks import Task + +logger = logging.getLogger(__name__) + + +class CancelTaskCommand(BaseCommand): + """ + Unified command to cancel a task. + + Behavior: + - For private tasks or single-subscriber tasks: aborts the task + - For shared tasks with multiple subscribers (non-admin): unsubscribes user + - For shared tasks with force=True (admin only): aborts for all subscribers + + The term "cancel" is user-facing; internally this may abort or unsubscribe. + """ + + def __init__(self, task_uuid: str, force: bool = False): + """ + Initialize the cancel command. + + :param task_uuid: UUID of the task to cancel + :param force: If True, force abort even with multiple subscribers (admin only) + """ + self._task_uuid = task_uuid + self._force = force + self._model: "Task" | None = None + self._is_admin: bool = False + self._action_taken: str = ( + "cancelled" # Will be set to 'aborted' or 'unsubscribed' + ) + + @transaction(on_error=partial(on_error, reraise=TaskAbortFailedError)) + def run(self) -> "Task": + """ + Execute the cancel command. + + :returns: The updated task model + """ + self.validate() + assert self._model Review Comment: This is also a fairly typical (and convenient) convention in the codebase to establish it isn't `None`. -- 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]
