diff --git a/airflow/www/static/js/api/useTaskLog.ts b/airflow/www/static/js/api/useTaskLog.ts index 1c8b999de81cf..580c5e0ab4995 100644 --- a/airflow/www/static/js/api/useTaskLog.ts +++ b/airflow/www/static/js/api/useTaskLog.ts @@ -20,15 +20,19 @@ import axios, { AxiosResponse } from 'axios'; import { useQuery } from 'react-query'; import { useAutoRefresh } from 'src/context/autorefresh'; -import type { API } from 'src/types'; +import type { API, TaskInstance } from 'src/types'; import { getMetaValue } from 'src/utils'; const taskLogApi = getMetaValue('task_log_api'); +interface Props extends API.GetLogVariables { + state?: TaskInstance['state']; +} + const useTaskLog = ({ - dagId, dagRunId, taskId, taskTryNumber, mapIndex, fullContent, -}: API.GetLogVariables) => { + dagId, dagRunId, taskId, taskTryNumber, mapIndex, fullContent, state, +}: Props) => { let url: string = ''; if (taskLogApi) { url = taskLogApi.replace('_DAG_RUN_ID_', dagRunId).replace('_TASK_ID_', taskId).replace(/-1$/, taskTryNumber.toString()); @@ -36,12 +40,21 @@ const useTaskLog = ({ const { isRefreshOn } = useAutoRefresh(); + // Only refresh is the state is pending + const isStatePending = state === 'deferred' + || state === 'scheduled' + || state === 'running' + || state === 'up_for_reschedule' + || state === 'up_for_retry' + || state === 'queued' + || state === 'restarting'; + return useQuery( - ['taskLogs', dagId, dagRunId, taskId, mapIndex, taskTryNumber, fullContent], + ['taskLogs', dagId, dagRunId, taskId, mapIndex, taskTryNumber, fullContent, state], () => axios.get(url, { headers: { Accept: 'text/plain' }, params: { map_index: mapIndex, full_content: fullContent } }), { placeholderData: '', - refetchInterval: isRefreshOn && (autoRefreshInterval || 1) * 1000, + refetchInterval: isStatePending && isRefreshOn && (autoRefreshInterval || 1) * 1000, }, ); }; diff --git a/airflow/www/static/js/dag/details/taskInstance/Logs/index.tsx b/airflow/www/static/js/dag/details/taskInstance/Logs/index.tsx index 8b35c3bc3a44f..ec6249cb27916 100644 --- a/airflow/www/static/js/dag/details/taskInstance/Logs/index.tsx +++ b/airflow/www/static/js/dag/details/taskInstance/Logs/index.tsx @@ -87,6 +87,7 @@ interface Props { mapIndex?: TaskInstance['mapIndex']; executionDate: DagRun['executionDate']; tryNumber: TaskInstance['tryNumber']; + state?: TaskInstance['state']; } const Logs = ({ @@ -96,6 +97,7 @@ const Logs = ({ mapIndex, executionDate, tryNumber, + state, }: Props) => { const [internalIndexes, externalIndexes] = getLinkIndexes(tryNumber); const [selectedTryNumber, setSelectedTryNumber] = useState(); @@ -105,7 +107,6 @@ const Logs = ({ const [fileSourceFilters, setFileSourceFilters] = useState>([]); const { timezone } = useTimezone(); - // const taskTryNumber = selectedTryNumber || tryNumber || 1; const { data, isSuccess } = useTaskLog({ dagId, @@ -114,6 +115,7 @@ const Logs = ({ mapIndex, taskTryNumber, fullContent: shouldRequestFullContent, + state, }); const params = new URLSearchParamsWrapper({ diff --git a/airflow/www/static/js/dag/details/taskInstance/index.tsx b/airflow/www/static/js/dag/details/taskInstance/index.tsx index 91d70f246181f..4917fafd3525d 100644 --- a/airflow/www/static/js/dag/details/taskInstance/index.tsx +++ b/airflow/www/static/js/dag/details/taskInstance/index.tsx @@ -124,7 +124,7 @@ const TaskInstance = ({ operator={operator} /> )} - + Details @@ -183,6 +183,7 @@ const TaskInstance = ({ mapIndex={mapIndex} executionDate={executionDate} tryNumber={instance?.tryNumber} + state={instance?.state} /> )}