98 lines
3.4 KiB
Python
98 lines
3.4 KiB
Python
"""Worker de conversion: traite les films en file, une conversion à la fois."""
|
|
|
|
import asyncio
|
|
import traceback
|
|
from pathlib import Path
|
|
|
|
from sqlalchemy import select
|
|
|
|
from app.conf_store import read_config
|
|
from app.config import settings
|
|
from app.converter import container_iso_path, conversion_paths, extract_mkv, import_mkv
|
|
from app.database import SessionLocal
|
|
from app.models import IsoMovie
|
|
from app.naming import movie_file_name
|
|
from app.radarr import RadarrClient
|
|
|
|
|
|
async def _process(movie_id: int) -> None:
|
|
with SessionLocal() as db:
|
|
movie = db.get(IsoMovie, movie_id)
|
|
if movie is None or movie.status != "queued":
|
|
return
|
|
paths = conversion_paths(movie)
|
|
step = "converting"
|
|
try:
|
|
movie.status = "converting"
|
|
movie.last_error = None
|
|
db.commit()
|
|
# Reprise: si le MKV existe deja (echec precedent apres l'extraction),
|
|
# on ne relance pas MakeMKV et on reprend a l'import.
|
|
if not paths.mkv_path.exists():
|
|
await asyncio.to_thread(extract_mkv, movie, paths.mkv_path)
|
|
step = "importing"
|
|
movie.status = "importing"
|
|
db.commit()
|
|
target_name = await _target_name(movie)
|
|
await asyncio.to_thread(
|
|
import_mkv,
|
|
paths.mkv_path,
|
|
paths.destination_folder,
|
|
target_name,
|
|
[container_iso_path(movie)],
|
|
)
|
|
# Le passage en "completed" (ISO supprimee) se fait manuellement
|
|
# via l'endpoint de validation apres verification par l'utilisateur.
|
|
movie.status = "validating"
|
|
movie.failed_step = None
|
|
except Exception as error: # noqa: BLE001 - le worker doit survivre a toute erreur
|
|
movie.status = "failed"
|
|
movie.failed_step = step
|
|
movie.last_error = f"{error}"
|
|
traceback.print_exc()
|
|
db.commit()
|
|
|
|
|
|
async def _target_name(movie: IsoMovie) -> str:
|
|
"""Nom du lien selon la configuration Media Management de Radarr.
|
|
|
|
En cas d'erreur (Radarr non configuré, API injoignable), on retombe sur le
|
|
nom du MKV produit par MakeMKV plutôt que de faire échouer la conversion.
|
|
"""
|
|
fallback = Path(conversion_paths(movie).mkv_path).name
|
|
cfg = read_config()
|
|
if not cfg.radarr.url or not cfg.radarr.api_key:
|
|
return fallback
|
|
try:
|
|
client = RadarrClient(cfg.radarr.url, cfg.radarr.api_key)
|
|
radarr_movie = await client.movie(movie.radarr_movie_id)
|
|
naming = await client.naming()
|
|
name = movie_file_name(
|
|
naming, radarr_movie, radarr_movie.get("movieFile") or {}, fallback
|
|
)
|
|
return name if name.lower().endswith(".mkv") else f"{name}.mkv"
|
|
except Exception: # noqa: BLE001 - le nommage ne doit pas bloquer la conversion
|
|
traceback.print_exc()
|
|
return fallback
|
|
|
|
|
|
async def _process_next() -> bool:
|
|
with SessionLocal() as db:
|
|
movie_id = db.scalar(
|
|
select(IsoMovie.id)
|
|
.where(IsoMovie.status == "queued")
|
|
.order_by(IsoMovie.id)
|
|
.limit(1)
|
|
)
|
|
if movie_id is None:
|
|
return False
|
|
await _process(movie_id)
|
|
return True
|
|
|
|
|
|
async def conversion_worker() -> None:
|
|
while True:
|
|
processed = await _process_next()
|
|
if not processed:
|
|
await asyncio.sleep(settings.worker_interval)
|