Spaces:
Sleeping
Sleeping
| from __future__ import annotations | |
| import asyncio | |
| import shutil | |
| import time | |
| from pathlib import Path | |
| import structlog | |
| from app.core.config import settings | |
| logger = structlog.get_logger(__name__) | |
| async def cleanup_loop() -> None: | |
| output_dir = Path(settings.OUTPUT_DIR) | |
| while True: | |
| await asyncio.sleep(600) | |
| await _sweep(output_dir) | |
| async def _sweep(output_dir: Path) -> None: | |
| if not output_dir.exists(): | |
| return | |
| now = time.time() | |
| removed = 0 | |
| for job_dir in output_dir.iterdir(): | |
| if not job_dir.is_dir(): | |
| continue | |
| if now - job_dir.stat().st_mtime > settings.OUTPUT_TTL_SECONDS: | |
| try: | |
| shutil.rmtree(job_dir) | |
| removed += 1 | |
| except Exception: | |
| logger.exception("cleanup_error", path=str(job_dir)) | |
| if removed: | |
| logger.info("cleanup_complete", removed_jobs=removed) | |