From 64fa39ac17c9f12b2a827be4c3aac10bf7859363 Mon Sep 17 00:00:00 2001 From: Kevin C Date: Fri, 1 Dec 2023 09:47:16 -0800 Subject: [PATCH] Create latest log dir symlink as relative link So `logs/**/*` is self-contained and the symlink doesn't break for exporting, build artifact, etc --- airflow/utils/log/file_processor_handler.py | 7 ++++--- tests/utils/log/test_file_processor_handler.py | 4 ++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/airflow/utils/log/file_processor_handler.py b/airflow/utils/log/file_processor_handler.py index 95c4ff5960e1e..eade2eadfafec 100644 --- a/airflow/utils/log/file_processor_handler.py +++ b/airflow/utils/log/file_processor_handler.py @@ -117,18 +117,19 @@ def _symlink_latest_log_directory(self): log_directory = self._get_log_directory() latest_log_directory_path = os.path.join(self.base_log_folder, "latest") if os.path.isdir(log_directory): + rel_link_target = Path(log_directory).relative_to(Path(latest_log_directory_path).parent) try: # if symlink exists but is stale, update it if os.path.islink(latest_log_directory_path): - if os.readlink(latest_log_directory_path) != log_directory: + if os.path.realpath(latest_log_directory_path) != log_directory: os.unlink(latest_log_directory_path) - os.symlink(log_directory, latest_log_directory_path) + os.symlink(rel_link_target, latest_log_directory_path) elif os.path.isdir(latest_log_directory_path) or os.path.isfile(latest_log_directory_path): logging.warning( "%s already exists as a dir/file. Skip creating symlink.", latest_log_directory_path ) else: - os.symlink(log_directory, latest_log_directory_path) + os.symlink(rel_link_target, latest_log_directory_path) except OSError: logging.warning("OSError while attempting to symlink the latest log directory") diff --git a/tests/utils/log/test_file_processor_handler.py b/tests/utils/log/test_file_processor_handler.py index e11ff35b0cf01..f50fc619ac5bd 100644 --- a/tests/utils/log/test_file_processor_handler.py +++ b/tests/utils/log/test_file_processor_handler.py @@ -80,13 +80,13 @@ def test_symlink_latest_log_directory(self): with time_machine.travel(date1, tick=False): handler.set_context(filename=os.path.join(self.dag_dir, "log1")) assert os.path.islink(link) - assert os.path.basename(os.readlink(link)) == date1 + assert os.path.basename(os.path.realpath(link)) == date1 assert os.path.exists(os.path.join(link, "log1")) with time_machine.travel(date2, tick=False): handler.set_context(filename=os.path.join(self.dag_dir, "log2")) assert os.path.islink(link) - assert os.path.basename(os.readlink(link)) == date2 + assert os.path.basename(os.path.realpath(link)) == date2 assert os.path.exists(os.path.join(link, "log2")) def test_symlink_latest_log_directory_exists(self):