fix(logs): Final robust fix for LogPruner path resolution and empty log pruning
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import os
|
||||
import shutil
|
||||
import sys
|
||||
from datetime import datetime, timedelta
|
||||
from src.log_registry import LogRegistry
|
||||
|
||||
@@ -37,6 +38,10 @@ class LogPruner:
|
||||
return
|
||||
# Get sessions that are old and not whitelisted from the registry
|
||||
old_sessions_to_check = self.log_registry.get_old_non_whitelisted_sessions(cutoff_time)
|
||||
|
||||
# Project root is two levels up from logs/sessions
|
||||
project_root = os.path.dirname(os.path.dirname(os.path.abspath(self.logs_dir)))
|
||||
|
||||
# Prune sessions if their size is less than threshold
|
||||
for session_info in old_sessions_to_check:
|
||||
session_id = session_info['session_id']
|
||||
@@ -44,15 +49,15 @@ class LogPruner:
|
||||
if not session_path:
|
||||
continue
|
||||
|
||||
# Resolve path
|
||||
resolved_path = session_path
|
||||
if not os.path.isabs(resolved_path):
|
||||
if resolved_path.startswith('logs' + os.sep) or resolved_path.startswith('logs/'):
|
||||
# Resolve relative to the project root (two levels up from self.logs_dir)
|
||||
project_root = os.path.dirname(os.path.dirname(os.path.abspath(self.logs_dir)))
|
||||
resolved_path = os.path.join(project_root, resolved_path)
|
||||
# Robust path resolution
|
||||
if os.path.isabs(session_path):
|
||||
resolved_path = session_path
|
||||
else:
|
||||
# Always resolve relative to project root
|
||||
resolved_path = os.path.abspath(os.path.join(project_root, session_path))
|
||||
|
||||
if not os.path.isdir(resolved_path):
|
||||
sys.stderr.write(f"[LogPruner] Skipping {session_id}: directory not found at {resolved_path}\n")
|
||||
continue
|
||||
|
||||
# Calculate total size of files in the directory
|
||||
@@ -61,15 +66,20 @@ class LogPruner:
|
||||
for entry in os.scandir(resolved_path):
|
||||
if entry.is_file():
|
||||
total_size += entry.stat().st_size
|
||||
except OSError:
|
||||
except OSError as e:
|
||||
sys.stderr.write(f"[LogPruner] Error scanning {resolved_path}: {e}\n")
|
||||
continue
|
||||
# Prune if the total size is less than threshold
|
||||
if total_size < (min_size_kb * 1024):
|
||||
|
||||
# Prune if the total size is less than threshold
|
||||
# (The user requested always removing empty logs, get_old_non_whitelisted_sessions already handles the 'empty' check)
|
||||
if total_size < (min_size_kb * 1024) or total_size == 0:
|
||||
try:
|
||||
sys.stderr.write(f"[LogPruner] Removing {session_id} at {resolved_path} (Size: {total_size} bytes)\n")
|
||||
shutil.rmtree(resolved_path)
|
||||
# Also remove from registry to keep it in sync
|
||||
if session_id in self.log_registry.data:
|
||||
del self.log_registry.data[session_id]
|
||||
except OSError:
|
||||
pass
|
||||
except OSError as e:
|
||||
sys.stderr.write(f"[LogPruner] Error removing {resolved_path}: {e}\n")
|
||||
|
||||
self.log_registry.save_registry()
|
||||
|
||||
Reference in New Issue
Block a user