from __future__ import annotations import shutil import tempfile import time from pathlib import Path from ocr_studio.config import JOB_TTL_SEC _PREFIX = "ocr_job_" def cleanup_stale_jobs(max_age_sec: int = JOB_TTL_SEC) -> None: root = Path(tempfile.gettempdir()) now = time.time() for path in root.glob(f"{_PREFIX}*"): try: age = now - path.stat().st_mtime if path.is_dir() and age > max_age_sec: shutil.rmtree(path, ignore_errors=True) except OSError: continue def new_job_dir() -> Path: cleanup_stale_jobs() return Path(tempfile.mkdtemp(prefix=_PREFIX))