amoghrajesh commented on code in PR #67118: URL: https://github.com/apache/airflow/pull/67118#discussion_r3288444516
########## task-sdk/src/airflow/sdk/bases/resumablemixin.py: ########## @@ -0,0 +1,139 @@ +# 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. +from __future__ import annotations + +from typing import TYPE_CHECKING, Any + +if TYPE_CHECKING: + from airflow.sdk.definitions.context import Context + from airflow.sdk.types import Logger + + +class ResumableJobMixin: + """ + Mixin for operators that submit one long-running job to an external system and poll for completion. + + On retry, reads the persisted external ID from task_state and reconnects to the existing job + instead of resubmitting from scratch. Reconnect can mean different for different kind of jobs. + + Usage: call ``execute_resumable(context)`` from the operator's ``execute()`` when reconnection + is supported. + + Subclasses must implement the methods specific to their external system. The mixin owns + only ``execute_resumable()`` and the task_state read/write logic. + + Example:: + + class MyOperator(ResumableJobMixin, BaseOperator): + external_id_key = "my_job_id" + + def execute(self, context): + return self.execute_resumable(context) + + def submit_job(self, context) -> str: + return self.hook.submit(...) + + def get_job_status(self, external_id: str) -> str: + return self.hook.get_status(external_id) + + def is_job_active(self, status: str) -> bool: + return status in ("RUNNING", "PENDING") Review Comment: The status strings `("RUNNING", "PENDING", etc.)` are external system states not Airflow's `TaskInstanceState`. `TaskInstanceState` has values like running, success, failed which are Airflow task states, not remote job states. An enum in the mixin would force every provider to translate their native states into it, adding a layer with no real benefit. The current contract — two boolean methods `is_job_active()` and `is_job_succeeded()` — is deliberately looser. The mixin does not need to know what states are possible, it just asks "is this still running?" and the subclass answers with whatever logic fits that system. Stays composable across providers. -- 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]
