Skip to content

Commit 83681be

Browse files
committed
cleanup old logs improvements
1 parent 963012c commit 83681be

6 files changed

Lines changed: 57 additions & 4 deletions

File tree

hasadna_k8s/storage/cleanup_old_logs.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,37 @@ def delete_old_files(root_path, cutoff_date, dry_run=True):
1515
if dry_run:
1616
print(f'remove: {file_path}')
1717
else:
18-
os.remove(file_path)
18+
try:
19+
os.remove(file_path)
20+
except:
21+
print(f'WARNING: could not remove file {file_path}')
1922
if not os.listdir(root) and root != root_path:
2023
if dry_run:
2124
print(f'rmtree: {root}')
2225
else:
23-
shutil.rmtree(root)
26+
try:
27+
shutil.rmtree(root)
28+
except:
29+
print(f'WARNING: could not remove directory {root}')
30+
31+
32+
def find_log_paths_airflow_logs(root):
33+
if os.path.basename(root) == 'logs':
34+
parent = os.path.dirname(root)
35+
if os.path.exists(f'{parent}/airflow.cfg'):
36+
return True
37+
return False
2438

2539

2640
def find_log_paths(root_path, log_path_prefixes):
2741
for root, dirs, files in os.walk(root_path):
2842
for log_path_prefix in log_path_prefixes:
29-
if root.endswith(log_path_prefix):
30-
yield root
43+
if log_path_prefix == 'airflow:logs':
44+
if find_log_paths_airflow_logs(root):
45+
yield root
46+
else:
47+
if root.endswith(log_path_prefix):
48+
yield root
3149

3250

3351
def main(path, dry_run=True, log_path_prefixes=None):

tests/old_logs_paths/airflow-home/logs/.gitkeep

Whitespace-only changes.

tests/old_logs_paths/foobar/airflow.cfg

Whitespace-only changes.

tests/old_logs_paths/foobar/logs/.gitkeep

Whitespace-only changes.

tests/old_logs_paths/somethingelse/.gitkeep

Whitespace-only changes.

tests/test_cleanup_old_logs.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import os
2+
from glob import glob
3+
from hasadna_k8s.storage.cleanup_old_logs import find_log_paths, main as cleanup_old_logs
4+
5+
6+
def test_find_log_paths():
7+
base = os.path.dirname(__file__)
8+
assert list(find_log_paths(os.path.dirname(__file__), ["airflow-home/logs", "airflow:logs"])) == [
9+
f'{base}/old_logs_paths/foobar/logs',
10+
f'{base}/old_logs_paths/airflow-home/logs',
11+
]
12+
13+
14+
def test_cleanup_old_logs():
15+
base = os.path.dirname(__file__)
16+
for f in glob(f'{base}/old_logs_paths/**/.gitkeep', recursive=True):
17+
os.utime(f, None)
18+
for fn in [
19+
f'{base}/old_logs_paths/airflow-home/logs/old_log.log',
20+
f'{base}/old_logs_paths/foobar/logs/old_log.log',
21+
f'{base}/old_logs_paths/somethingelse/old_log.log',
22+
]:
23+
with open(fn, 'w') as f:
24+
f.write('test log content')
25+
# Set modification time to 40 days ago
26+
old_time = os.path.getmtime(fn) - (40 * 24 * 60 * 60)
27+
os.utime(fn, (old_time, old_time))
28+
cleanup_old_logs(base, dry_run=False, log_path_prefixes=["airflow-home/logs", "airflow:logs"])
29+
for fn in [
30+
f'{base}/old_logs_paths/airflow-home/logs/old_log.log',
31+
f'{base}/old_logs_paths/foobar/logs/old_log.log',
32+
]:
33+
assert not os.path.exists(fn)
34+
assert os.path.exists(f'{base}/old_logs_paths/somethingelse/old_log.log')
35+
os.remove(f'{base}/old_logs_paths/somethingelse/old_log.log')

0 commit comments

Comments
 (0)