clean Temp files after download and on server start

This commit is contained in:
2026-02-22 17:32:40 +01:00
parent dd45494717
commit 1885fed4bd
4 changed files with 84 additions and 294 deletions

View File

@@ -126,7 +126,29 @@ async def lifespan(_application: FastAPI):
startup_error = None
try:
logger.info("Starting FastAPI application...")
# Clean up any leftover temp download files from a previous run
try:
import shutil as _shutil
_temp_dir = Path(__file__).resolve().parents[2] / "Temp"
if _temp_dir.exists():
_removed = 0
for _item in _temp_dir.iterdir():
try:
if _item.is_file():
_item.unlink()
elif _item.is_dir():
_shutil.rmtree(_item)
_removed += 1
except OSError as _exc:
logger.warning("Could not remove temp item %s: %s", _item, _exc)
logger.info("Cleaned %d item(s) from Temp folder on startup", _removed)
else:
_temp_dir.mkdir(parents=True, exist_ok=True)
logger.debug("Created Temp folder: %s", _temp_dir)
except Exception as _exc:
logger.warning("Failed to clean Temp folder on startup: %s", _exc)
# Initialize database first (required for other services)
try:
from src.server.database.connection import init_db