diff --git a/utils/temp.py b/utils/temp.py index eb894809..3e6669d6 100644 --- a/utils/temp.py +++ b/utils/temp.py @@ -1,7 +1,11 @@ """Utilities for creating temporary directories.""" +import utils.logger as logger +from pathlib import Path import shutil import tempfile +import time + @@ -14,5 +18,16 @@ def create_temp_dir(): def delete_temp_dir(temp_dir: str): """Delete a temporary directory.""" - - shutil.rmtree(temp_dir, ignore_errors=True) \ No newline at end of file + shutil.rmtree(temp_dir, ignore_errors=True) + + + +def clean_tmp(tmp_clean_age_seconds: int = 3600): + for path in Path(tempfile.gettempdir()).glob("tmp*"): + now = time.time() + modified_time = path.stat().st_mtime + age = now - modified_time + + if age > tmp_clean_age_seconds: + logger.debug(f"Deleting {path}") + shutil.rmtree(path, ignore_errors=True) diff --git a/validator/.env.example b/validator/.env.example index f5f29e58..8f3937bd 100644 --- a/validator/.env.example +++ b/validator/.env.example @@ -33,4 +33,9 @@ INCLUDE_SOLUTIONS=False +CLEAN_TMP_AGE_SECONDS=3600 +CLEAN_TMP_INTERVAL_SECONDS=3600 + + + UPDATE_AUTOMATICALLY=True \ No newline at end of file diff --git a/validator/config.py b/validator/config.py index fb9dccf3..45414369 100644 --- a/validator/config.py +++ b/validator/config.py @@ -142,6 +142,18 @@ +CLEAN_TMP_AGE_SECONDS = os.getenv("CLEAN_TMP_AGE_SECONDS") +if not CLEAN_TMP_AGE_SECONDS: + CLEAN_TMP_AGE_SECONDS = 3600 +CLEAN_TMP_AGE_SECONDS = int(CLEAN_TMP_AGE_SECONDS) + +CLEAN_TMP_INTERVAL_SECONDS = os.getenv("CLEAN_TMP_INTERVAL_SECONDS") +if not CLEAN_TMP_INTERVAL_SECONDS: + CLEAN_TMP_INTERVAL_SECONDS = 3600 +CLEAN_TMP_INTERVAL_SECONDS = int(CLEAN_TMP_INTERVAL_SECONDS) + + + # Print out the configuration logger.info("=== Validator Configuration ===") logger.info(f"Network ID: {NETUID}") @@ -163,6 +175,9 @@ logger.info(f"Set Weights Interval: {SET_WEIGHTS_INTERVAL_SECONDS} second(s)") logger.info(f"Request Evaluation Interval: {REQUEST_EVALUATION_INTERVAL_SECONDS} second(s)") logger.info("-------------------------------") +logger.info(f"Clean tmp age: {CLEAN_TMP_AGE_SECONDS} second(s)") +logger.info(f"Clean tmp interval: {CLEAN_TMP_INTERVAL_SECONDS} second(s)") +logger.info("-------------------------------") if SIMULATE_EVALUATION_RUNS: logger.warning("Simulating Evaluation Runs!") else: diff --git a/validator/main.py b/validator/main.py index baf7c702..53d40c5c 100644 --- a/validator/main.py +++ b/validator/main.py @@ -81,7 +81,11 @@ async def set_weights_loop(): await asyncio.sleep(config.SET_WEIGHTS_INTERVAL_SECONDS) - +async def clean_tmp_loop(): + logger.info("Starting clean tmp loop...") + while True: + clean_tmp(config.CLEAN_TMP_AGE_SECONDS) + await asyncio.sleep(config.CLEAN_TMP_INTERVAL_SECONDS) # Sends an update-evaluation-run request to the Ridges platform. The extra # parameter is for fields that are not sent in all requests, such as agent_logs @@ -361,6 +365,7 @@ async def main(): # Start the send heartbeat loop asyncio.create_task(send_heartbeat_loop()) + asyncio.create_task(clean_tmp_loop()) if config.MODE == "validator": # Start the set weights loop