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

@@ -21,6 +21,31 @@ from yt_dlp.utils import DownloadCancelled
from ..interfaces.providers import Providers
from .base_provider import Loader
def _cleanup_temp_file(temp_path: str) -> None:
"""Clean up a temp file and any associated partial download files.
Removes the temp file itself and any yt-dlp partial files
(e.g. ``<name>.part``) that may have been left behind.
Args:
temp_path: Absolute or relative path to the temp file.
"""
paths_to_remove = [temp_path]
# yt-dlp writes partial fragments to <file>.part
paths_to_remove.extend(
str(p) for p in Path(temp_path).parent.glob(
Path(temp_path).name + ".*"
)
)
for path in paths_to_remove:
if os.path.exists(path):
try:
os.remove(path)
logging.debug(f"Removed temp file: {path}")
except OSError as exc:
logging.warning(f"Failed to remove temp file {path}: {exc}")
# Imported shared provider configuration
from .provider_config import (
ANIWORLD_HEADERS,
@@ -345,17 +370,20 @@ class AniworldLoader(Loader):
f"Broken pipe error with provider {provider}: {e}. "
f"This usually means the stream connection was closed."
)
_cleanup_temp_file(temp_path)
continue
except Exception as e:
logging.error(
f"YoutubeDL download failed with provider {provider}: "
f"{type(e).__name__}: {e}"
)
_cleanup_temp_file(temp_path)
continue
break
# If we get here, all providers failed
logging.error("All download providers failed")
_cleanup_temp_file(temp_path)
self.clear_cache()
return False

View File

@@ -43,6 +43,33 @@ from .provider_config import (
)
def _cleanup_temp_file(
temp_path: str,
logger: Optional[logging.Logger] = None,
) -> None:
"""Remove a temp file and any associated yt-dlp partial files.
Args:
temp_path: Path to the primary temp file.
logger: Optional logger for diagnostic messages.
"""
_log = logger or logging.getLogger(__name__)
candidates = [temp_path]
# yt-dlp creates fragment files like <file>.part
candidates.extend(
str(p) for p in Path(temp_path).parent.glob(
Path(temp_path).name + ".*"
)
)
for path in candidates:
if os.path.exists(path):
try:
os.remove(path)
_log.debug(f"Removed temp file: {path}")
except OSError as exc:
_log.warning(f"Failed to remove temp file {path}: {exc}")
class EnhancedAniWorldLoader(Loader):
"""Aniworld provider with retry and recovery strategies.
@@ -596,9 +623,13 @@ class EnhancedAniWorldLoader(Loader):
except Exception as e:
self.logger.warning(f"Provider {provider_name} failed: {e}")
# Clean up any partial temp files left by this failed attempt
_cleanup_temp_file(temp_path, self.logger)
self.download_stats['retried_downloads'] += 1
continue
# All providers failed make sure no temp remnants are left behind
_cleanup_temp_file(temp_path, self.logger)
return False
def _perform_ytdl_download(

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