amoghrajesh commented on code in PR #67118: URL: https://github.com/apache/airflow/pull/67118#discussion_r3272747570
########## task-sdk/src/airflow/sdk/bases/resumablemixin.py: ########## @@ -0,0 +1,135 @@ +# 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 + +import structlog + +if TYPE_CHECKING: + from airflow.sdk.definitions.context import Context + +log = structlog.get_logger(__name__) + + +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") + + def is_job_succeeded(self, status: str) -> bool: + return status == "SUCCEEDED" + + def poll_until_complete(self, external_id: str, context: Context) -> None: + self.hook.poll(external_id) + + def get_job_result(self, external_id: str, context: Context) -> Any: + return None + """ + + external_id_key: str = "remote_job_id" Review Comment: The deferrable path need not use task_state at all, it can just persist state via the trigger row, so the key-sharing concern might not even be real. The only item worth preserving is that `external_id_key` shouldnt be renamed on a deployed operator since that breaks in flight retries with a stored key. I will add a short one liner for that -- 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]
