Simon Strandgaard
commited on
Commit
·
7247691
1
Parent(s):
3d88dc7
start_purge_scheduler() added
Browse files- src/purge/purge_old_runs.py +24 -1
src/purge/purge_old_runs.py
CHANGED
|
@@ -2,6 +2,8 @@ import logging
|
|
| 2 |
import os
|
| 3 |
import shutil
|
| 4 |
import datetime
|
|
|
|
|
|
|
| 5 |
|
| 6 |
logger = logging.getLogger(__name__)
|
| 7 |
|
|
@@ -9,7 +11,6 @@ def purge_old_runs(run_dir: str, max_age_hours: float = 1.0) -> None:
|
|
| 9 |
"""
|
| 10 |
Deletes runs in the specified run_dir older than max_age_hours.
|
| 11 |
"""
|
| 12 |
-
# raise exception if the run_dir is not an absolute path
|
| 13 |
if not os.path.isabs(run_dir):
|
| 14 |
raise ValueError(f"run_dir must be an absolute path: {run_dir}")
|
| 15 |
|
|
@@ -33,3 +34,25 @@ def purge_old_runs(run_dir: str, max_age_hours: float = 1.0) -> None:
|
|
| 33 |
|
| 34 |
except Exception as e:
|
| 35 |
logger.error(f"Error processing {run_id} in {run_dir}: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import os
|
| 3 |
import shutil
|
| 4 |
import datetime
|
| 5 |
+
import threading
|
| 6 |
+
import time
|
| 7 |
|
| 8 |
logger = logging.getLogger(__name__)
|
| 9 |
|
|
|
|
| 11 |
"""
|
| 12 |
Deletes runs in the specified run_dir older than max_age_hours.
|
| 13 |
"""
|
|
|
|
| 14 |
if not os.path.isabs(run_dir):
|
| 15 |
raise ValueError(f"run_dir must be an absolute path: {run_dir}")
|
| 16 |
|
|
|
|
| 34 |
|
| 35 |
except Exception as e:
|
| 36 |
logger.error(f"Error processing {run_id} in {run_dir}: {e}")
|
| 37 |
+
|
| 38 |
+
def start_purge_scheduler(run_dir: str, purge_interval_seconds: float=3600) -> None:
|
| 39 |
+
"""
|
| 40 |
+
Start the purge scheduler in a background thread.
|
| 41 |
+
"""
|
| 42 |
+
logger.info(f"Starting purge scheduler for {run_dir} every {purge_interval_seconds} seconds...")
|
| 43 |
+
|
| 44 |
+
if not os.path.isabs(run_dir):
|
| 45 |
+
raise ValueError(f"run_dir must be an absolute path: {run_dir}")
|
| 46 |
+
|
| 47 |
+
def purge_scheduler():
|
| 48 |
+
"""
|
| 49 |
+
Schedules the purge_old_runs function to run periodically.
|
| 50 |
+
"""
|
| 51 |
+
while True:
|
| 52 |
+
logger.info("Running purge...")
|
| 53 |
+
print("Running purge...")
|
| 54 |
+
purge_old_runs(run_dir)
|
| 55 |
+
time.sleep(purge_interval_seconds)
|
| 56 |
+
|
| 57 |
+
purge_thread = threading.Thread(target=purge_scheduler, daemon=True)
|
| 58 |
+
purge_thread.start()
|