Spaces:
Sleeping
Sleeping
File size: 923 Bytes
936f0bf | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | 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)
|