From 228c328c7728f8cbd5f94212131f747ae793b114 Mon Sep 17 00:00:00 2001 From: Daniel Standish <15932138+dstandish@users.noreply.github.com> Date: Sun, 12 Feb 2023 13:40:03 -0800 Subject: [PATCH 1/2] Fix log tailing issues with legacy log view Probably we should just chop this view in favor of grid view logging which is the future. But this fixes rendering issues raised here https://github.com/apache/airflow/pull/29447#issuecomment-1424981137. What we do, is in log tailing context (which apparently isn't used in grid, and that's why I did not see this in developing trigger logging) we don't add the messages to the log content. So, whenever log_pos is in metadata we don't add messages. It means the messages could be a bit stale but that seems ok. Refreshing the page could fix that. Longer term, we could update the API so that log content is just content and the messages are themselves returned in the metadata dict. That's probably the "right" solution ultimately. But can be saved for another day. Also resolve the "cannot load lazy instance" issue when invoking the reader logic from this different context. --- airflow/utils/log/file_task_handler.py | 3 ++- airflow/www/views.py | 10 +++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/airflow/utils/log/file_task_handler.py b/airflow/utils/log/file_task_handler.py index 1496b31fac682..81058cf2e55bb 100644 --- a/airflow/utils/log/file_task_handler.py +++ b/airflow/utils/log/file_task_handler.py @@ -340,7 +340,8 @@ def _read( if metadata and "log_pos" in metadata: previous_chars = metadata["log_pos"] logs = logs[previous_chars:] # Cut off previously passed log test as new tail - return messages + logs, {"end_of_log": end_of_log, "log_pos": log_pos} + out_message = logs if "log_pos" in metadata else messages + logs + return out_message, {"end_of_log": end_of_log, "log_pos": log_pos} @staticmethod def _get_pod_namespace(ti: TaskInstance): diff --git a/airflow/www/views.py b/airflow/www/views.py index 2ee931372701e..543708b909f92 100644 --- a/airflow/www/views.py +++ b/airflow/www/views.py @@ -1527,7 +1527,15 @@ def get_logs_with_metadata(self, session=None): ti = ( session.query(models.TaskInstance) - .filter_by(dag_id=dag_id, task_id=task_id, execution_date=execution_date, map_index=map_index) + .filter( + TaskInstance.task_id == task_id, + TaskInstance.dag_id == dag_id, + TaskInstance.execution_date == execution_date, + TaskInstance.map_index == map_index, + ) + .join(TaskInstance.dag_run) + .options(joinedload("trigger")) + .options(joinedload("trigger.triggerer_job")) .first() ) From de08a29a94ba2ba88c48c8f87072a2abc31c7430 Mon Sep 17 00:00:00 2001 From: Daniel Standish <15932138+dstandish@users.noreply.github.com> Date: Tue, 14 Feb 2023 21:32:07 -0800 Subject: [PATCH 2/2] staticchecks --- airflow/utils/log/file_task_handler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/airflow/utils/log/file_task_handler.py b/airflow/utils/log/file_task_handler.py index 81058cf2e55bb..bf620e9be2556 100644 --- a/airflow/utils/log/file_task_handler.py +++ b/airflow/utils/log/file_task_handler.py @@ -340,7 +340,7 @@ def _read( if metadata and "log_pos" in metadata: previous_chars = metadata["log_pos"] logs = logs[previous_chars:] # Cut off previously passed log test as new tail - out_message = logs if "log_pos" in metadata else messages + logs + out_message = logs if "log_pos" in (metadata or {}) else messages + logs return out_message, {"end_of_log": end_of_log, "log_pos": log_pos} @staticmethod