Skip to content

Commit

Permalink
Create latest log dir symlink as relative link (#36019)
Browse files Browse the repository at this point in the history
So `logs/**/*` is self-contained and the symlink doesn't break for
exporting, build artifact, etc

(cherry picked from commit 17e9172)
  • Loading branch information
Kache authored and ephraimbuddy committed Jan 11, 2024
1 parent 6e31c0f commit fcd2357
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
7 changes: 4 additions & 3 deletions airflow/utils/log/file_processor_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
4 changes: 2 additions & 2 deletions tests/utils/log/test_file_processor_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit fcd2357

Please sign in to comment.