amoghrajesh commented on code in PR #76833:
URL: https://github.com/apache/airflow/pull/76833#discussion_r4629434302


##########
task-sdk/src/airflow/sdk/bases/resumablejobmixin.py:
##########
@@ -225,6 +254,51 @@ def persist_job_id(self, context: Context, external_id: 
JsonValue) -> None:
             external_id=external_id,
         )
 
+    def _record_try(
+        self,
+        context: Context,
+        decision: DurableDecision,
+        external_id: JsonValue,
+        *,
+        job_finished: bool = False,
+        previous_external_id: JsonValue = None,
+    ) -> None:
+        """Record this try for the logs banner; a finished reconnect stores 
the job's run time as saved."""
+        task_state_store = context.get("task_state_store")
+        if task_state_store is None:
+            return
+        try:
+            now = datetime.now(tz=UTC)
+            stored = task_state_store.get(DURABLE_RECORD_KEY)
+            record: dict[str, Any] = stored if isinstance(stored, dict) else {}
+            if decision in (DurableDecision.SUBMITTED, 
DurableDecision.RESUBMITTED):
+                record["submitted_at"] = now.isoformat()

Review Comment:
   [some more comments from kaxil and 
brent](https://github.com/apache/airflow/pull/76833/commits/1a6f85b9e6d46d518960de16ce644c7b26ae378e)



##########
task-sdk/src/airflow/sdk/bases/resumablejobmixin.py:
##########
@@ -132,12 +146,27 @@ def execute_resumable(self, context: Context) -> Any:
 
         decision = self.get_resume_decision(context)
         if decision.action == "reconnect":
-            return self.poll_until_complete(decision.external_id, context)
+            self._record_try(context, DurableDecision.RECONNECTED, 
decision.external_id)
+            result = self.poll_until_complete(decision.external_id, context)
+            self._record_try(context, DurableDecision.RECONNECTED, 
decision.external_id, job_finished=True)
+            return result
         if decision.action == "already_succeeded":
+            self._record_try(context, DurableDecision.ALREADY_SUCCEEDED, 
decision.external_id)
             return self.get_job_result(decision.external_id, context)
 
+        # A "submit" decision covers both a first submit and one after a 
failed job; only the
+        # latter still has an id stored, which the banner shows as the job 
that was replaced.
+        task_state_store = context.get("task_state_store")
+        failed_id = task_state_store.get(self.external_id_key) if 
task_state_store is not None else None

Review Comment:
   [some more comments from kaxil and 
brent](https://github.com/apache/airflow/pull/76833/commits/1a6f85b9e6d46d518960de16ce644c7b26ae378e)



##########
task-sdk/src/airflow/sdk/bases/resumablejobmixin.py:
##########
@@ -132,12 +146,27 @@ def execute_resumable(self, context: Context) -> Any:
 
         decision = self.get_resume_decision(context)
         if decision.action == "reconnect":
-            return self.poll_until_complete(decision.external_id, context)
+            self._record_try(context, DurableDecision.RECONNECTED, 
decision.external_id)
+            result = self.poll_until_complete(decision.external_id, context)
+            self._record_try(context, DurableDecision.RECONNECTED, 
decision.external_id, job_finished=True)

Review Comment:
   [some more comments from kaxil and 
brent](https://github.com/apache/airflow/pull/76833/commits/1a6f85b9e6d46d518960de16ce644c7b26ae378e)



##########
airflow-core/src/airflow/api_fastapi/core_api/routes/ui/durable_execution.py:
##########
@@ -0,0 +1,59 @@
+# 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
+
+import json
+
+from fastapi import Depends
+from sqlalchemy import select
+
+from airflow.api_fastapi.auth.managers.models.resource_details import 
DagAccessEntity
+from airflow.api_fastapi.common.db.common import SessionDep
+from airflow.api_fastapi.common.router import AirflowRouter
+from airflow.api_fastapi.core_api.datamodels.ui.durable_execution import 
DurableExecutionResponse
+from airflow.api_fastapi.core_api.security import requires_access_dag
+from airflow.api_fastapi.core_api.services.public.task_coordinates import 
TaskScopeDep
+from airflow.models.task_state_store import TaskStateStoreModel
+
+# Must match DURABLE_RECORD_KEY in the task SDK, which writes the record.
+DURABLE_RECORD_KEY = "__airflow_durable__"
+
+durable_execution_router = AirflowRouter(
+    tags=["Durable Execution"],
+    
prefix="/dags/{dag_id}/dagRuns/{dag_run_id}/taskInstances/{task_id}/durable",
+)
+
+
+@durable_execution_router.get(
+    "",
+    dependencies=[Depends(requires_access_dag(method="GET", 
access_entity=DagAccessEntity.TASK_INSTANCE))],
+)
+def get_durable_execution(scope: TaskScopeDep, session: SessionDep) -> 
DurableExecutionResponse:
+    """Get the durable execution history of a task instance; empty when the 
task is not durable."""
+    value = session.scalar(
+        select(TaskStateStoreModel.value).where(
+            TaskStateStoreModel.dag_id == scope.dag_id,
+            TaskStateStoreModel.run_id == scope.run_id,
+            TaskStateStoreModel.task_id == scope.task_id,
+            TaskStateStoreModel.region_id == scope.region_id,
+            TaskStateStoreModel.region_index == scope.region_index,
+            TaskStateStoreModel.key == DURABLE_RECORD_KEY,
+        )
+    )
+    if value is None:
+        return DurableExecutionResponse(tries=[])
+    return DurableExecutionResponse.model_validate(json.loads(value))

Review Comment:
   [some more comments from kaxil and 
brent](https://github.com/apache/airflow/pull/76833/commits/1a6f85b9e6d46d518960de16ce644c7b26ae378e)



-- 
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]

Reply via email to