amoghrajesh commented on code in PR #76833: URL: https://github.com/apache/airflow/pull/76833#discussion_r4560336873
########## airflow-core/src/airflow/ui/src/pages/TaskInstance/Logs/DurableBanner.tsx: ########## @@ -0,0 +1,106 @@ +/*! + * 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. + */ +import { Box } from "@chakra-ui/react"; +import { useTranslation } from "react-i18next"; + +import { useTaskStateStoreServiceGetTaskStateStore } from "openapi/queries"; +import type { TaskInstanceResponse } from "openapi/requests"; + +import { Alert } from "src/system-components"; + +import { useTaskInstanceCoordinates } from "src/hooks/useTaskInstanceCoordinates"; +import { isStatePending, useAutoRefresh } from "src/utils"; +import { renderDuration } from "src/utils/datetimeUtils"; + +// Written by ResumableJobMixin in the Task SDK. +const DURABLE_RECORD_KEY = "__airflow_durable__"; + +type DurableTry = { + readonly decision: string; + readonly external_id: unknown; + readonly saved_seconds: number | null; + readonly try_number: number | null; +}; + +const DECISIONS_THAT_SAVE = new Set(["already_succeeded", "reconnected"]); + +export const DurableBanner = ({ + taskInstance, + tryNumber, +}: { + readonly taskInstance?: Pick< + TaskInstanceResponse, + "dag_id" | "dag_run_id" | "map_index" | "state" | "task_id" + >; + readonly tryNumber?: number; +}) => { + const { t: translate } = useTranslation("dag"); + const coordinates = useTaskInstanceCoordinates(); + const refetchInterval = useAutoRefresh({ dagId: taskInstance?.dag_id }); + + const { data } = useTaskStateStoreServiceGetTaskStateStore( + { + ...coordinates, + dagId: taskInstance?.dag_id ?? "", + dagRunId: taskInstance?.dag_run_id ?? "", + key: DURABLE_RECORD_KEY, + mapIndex: taskInstance?.map_index ?? -1, + taskId: taskInstance?.task_id ?? "", + }, + undefined, + { + enabled: taskInstance !== undefined, + refetchInterval: isStatePending(taskInstance?.state) && refetchInterval, + // Most tasks are not durable, so the key is usually missing. + retry: false, + }, + ); + + const tries = (data?.value as { tries?: Array<DurableTry> } | undefined)?.tries ?? []; + const entry = tries.find( + (attempt) => attempt.try_number === tryNumber && DECISIONS_THAT_SAVE.has(attempt.decision), + ); + + if (entry === undefined) { + return undefined; + } Review Comment: Oh yeah good call. I thought of this more. I made a quick fix here (long term to follow up, see below) The banner now only fetches on retries, since a first try can never reconnect or reuse a result. The proper fix is an `is_durable` flag on the serialized task and the task API. I'll do that in a follow-up PR, which i will raise paralelly. -- 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]
