Add anipy-cli download fallback
Add an optional config toggle that retries failed ani-cli downloads with anipy-cli, expose fallback runtime status in the config page, and update release docs and tests for the new behavior.
This commit is contained in:
+133
-47
@@ -13,12 +13,14 @@ import time
|
||||
import re
|
||||
|
||||
from app_support import (
|
||||
ANIPY_CLI,
|
||||
MAX_LOG_LINES,
|
||||
PROJECT_ROOT,
|
||||
QUEUE_PATH,
|
||||
STATE_DB_PATH,
|
||||
WORKER_SHUTDOWN_TIMEOUT_SECONDS,
|
||||
build_job,
|
||||
cli_executable_exists,
|
||||
command_for_job,
|
||||
debug_log,
|
||||
finalize_library_files,
|
||||
@@ -1373,6 +1375,36 @@ class DownloadQueue:
|
||||
self._save_job_locked(job)
|
||||
self._cleanup_staging_dir(job)
|
||||
|
||||
def _command_attempts(self, job, staging_dir):
|
||||
config = normalize_config(self.config_getter() or {})
|
||||
attempts = [
|
||||
{
|
||||
"backend": "ani-cli",
|
||||
"command": command_for_job(job, backend="ani-cli"),
|
||||
"env": {
|
||||
**os.environ.copy(),
|
||||
"ANI_CLI_DOWNLOAD_DIR": str(staging_dir),
|
||||
"ANI_CLI_MODE": job["mode"],
|
||||
"ANI_CLI_QUALITY": job["quality"],
|
||||
"TERM": os.environ.get("TERM", "xterm-256color"),
|
||||
},
|
||||
}
|
||||
]
|
||||
fallback_enabled = bool(config.get("anipy_cli_fallback_enabled"))
|
||||
fallback_available = cli_executable_exists(ANIPY_CLI)
|
||||
if fallback_enabled and fallback_available:
|
||||
attempts.append(
|
||||
{
|
||||
"backend": "anipy-cli",
|
||||
"command": command_for_job(job, backend="anipy-cli", download_path=staging_dir),
|
||||
"env": {
|
||||
**os.environ.copy(),
|
||||
"TERM": os.environ.get("TERM", "xterm-256color"),
|
||||
},
|
||||
}
|
||||
)
|
||||
return attempts, fallback_enabled, fallback_available
|
||||
|
||||
def _run(self):
|
||||
while not self.stop_event.is_set():
|
||||
job = self._next_pending()
|
||||
@@ -1383,79 +1415,129 @@ class DownloadQueue:
|
||||
self._run_job(job)
|
||||
|
||||
def _run_job(self, job):
|
||||
command = command_for_job(job)
|
||||
staging_dir = job_staging_dir(job)
|
||||
target_dir = job_output_dir(job)
|
||||
env = os.environ.copy()
|
||||
env.update(
|
||||
{
|
||||
"ANI_CLI_DOWNLOAD_DIR": str(staging_dir),
|
||||
"ANI_CLI_MODE": job["mode"],
|
||||
"ANI_CLI_QUALITY": job["quality"],
|
||||
"TERM": env.get("TERM", "xterm-256color"),
|
||||
}
|
||||
)
|
||||
staging_dir.mkdir(parents=True, exist_ok=True)
|
||||
target_dir.mkdir(parents=True, exist_ok=True)
|
||||
attempts, fallback_enabled, fallback_available = self._command_attempts(job, staging_dir)
|
||||
primary_command = attempts[0]["command"]
|
||||
|
||||
with self.lock:
|
||||
job["status"] = "running"
|
||||
job["started_at"] = now_iso()
|
||||
job["updated_at"] = job["started_at"]
|
||||
job["command"] = printable_command(command)
|
||||
job["command"] = printable_command(primary_command)
|
||||
job["staging_dir"] = str(staging_dir)
|
||||
job["target_dir"] = str(target_dir)
|
||||
job["download_backend"] = "ani-cli"
|
||||
job["fallback_used"] = False
|
||||
job["log"] = [
|
||||
f"Starting: {job['command']}",
|
||||
f"Staging in: {job['staging_dir']}",
|
||||
f"Final folder: {job['target_dir']}",
|
||||
]
|
||||
if fallback_enabled and fallback_available:
|
||||
job["log"].append("anipy-cli fallback is enabled and will retry automatically if ani-cli fails.")
|
||||
elif fallback_enabled:
|
||||
job["log"].append("anipy-cli fallback is enabled, but anipy-cli is not installed or not executable.")
|
||||
job["cancel_requested"] = False
|
||||
job["_dirty_log_lines"] = 0
|
||||
self._save_job_locked(job)
|
||||
|
||||
try:
|
||||
process = subprocess.Popen(
|
||||
command,
|
||||
cwd=str(PROJECT_ROOT),
|
||||
env=env,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.STDOUT,
|
||||
text=True,
|
||||
bufsize=1,
|
||||
start_new_session=True,
|
||||
)
|
||||
except Exception as exc:
|
||||
self._fail_job_startup(job, exc)
|
||||
return
|
||||
with self.lock:
|
||||
self.current_process = process
|
||||
self.current_job_id = job["id"]
|
||||
self.current_job = job
|
||||
job["pid"] = process.pid
|
||||
self._save_job_locked(job)
|
||||
|
||||
try:
|
||||
assert process.stdout is not None
|
||||
for line in process.stdout:
|
||||
self._append_log(job, line)
|
||||
exit_code = process.wait()
|
||||
exit_code = 1
|
||||
successful_backend = None
|
||||
finalize_error = None
|
||||
moved_files = []
|
||||
watchlist_sync_error = None
|
||||
synced_watchlist_item = None
|
||||
cleanup_staging = False
|
||||
if exit_code == 0 and not job.get("cancel_requested"):
|
||||
for index, attempt in enumerate(attempts):
|
||||
backend = attempt["backend"]
|
||||
command = attempt["command"]
|
||||
should_fallback = index + 1 < len(attempts)
|
||||
with self.lock:
|
||||
job["download_backend"] = backend
|
||||
job["command"] = printable_command(command)
|
||||
if index > 0:
|
||||
job["fallback_used"] = True
|
||||
job["pid"] = None
|
||||
job["updated_at"] = now_iso()
|
||||
if index > 0:
|
||||
job.setdefault("log", []).append(f"Retrying with {backend}: {job['command']}")
|
||||
self._save_job_locked(job)
|
||||
try:
|
||||
moved_files = finalize_library_files(job)
|
||||
process = subprocess.Popen(
|
||||
command,
|
||||
cwd=str(PROJECT_ROOT),
|
||||
env=attempt["env"],
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.STDOUT,
|
||||
text=True,
|
||||
bufsize=1,
|
||||
start_new_session=True,
|
||||
)
|
||||
except Exception as exc:
|
||||
finalize_error = exc
|
||||
else:
|
||||
if self.watchlist_sync_fn is not None:
|
||||
try:
|
||||
synced_watchlist_item = self.watchlist_sync_fn(job)
|
||||
except Exception as exc:
|
||||
watchlist_sync_error = exc
|
||||
if should_fallback:
|
||||
self._append_log(job, f"ani-cli could not start: {exc}")
|
||||
self._append_log(job, "Retrying with anipy-cli fallback.")
|
||||
self._cleanup_staging_dir(job)
|
||||
staging_dir.mkdir(parents=True, exist_ok=True)
|
||||
continue
|
||||
self._fail_job_startup(job, exc)
|
||||
return
|
||||
with self.lock:
|
||||
self.current_process = process
|
||||
self.current_job_id = job["id"]
|
||||
self.current_job = job
|
||||
job["pid"] = process.pid
|
||||
self._save_job_locked(job)
|
||||
|
||||
assert process.stdout is not None
|
||||
for line in process.stdout:
|
||||
self._append_log(job, line)
|
||||
exit_code = process.wait()
|
||||
with self.lock:
|
||||
job["pid"] = None
|
||||
self.current_process = None
|
||||
self.current_job_id = None
|
||||
self.current_job = None
|
||||
self._save_job_locked(job)
|
||||
|
||||
if job.get("cancel_requested"):
|
||||
break
|
||||
|
||||
if backend == "ani-cli" and exit_code != 0 and should_fallback:
|
||||
self._append_log(job, f"ani-cli failed with exit code {exit_code}.")
|
||||
self._append_log(job, "Retrying with anipy-cli fallback.")
|
||||
self._cleanup_staging_dir(job)
|
||||
staging_dir.mkdir(parents=True, exist_ok=True)
|
||||
continue
|
||||
|
||||
if exit_code == 0:
|
||||
try:
|
||||
moved_files = finalize_library_files(job)
|
||||
except Exception as exc:
|
||||
finalize_error = exc
|
||||
no_files_downloaded = "No downloaded files were found in the staging folder" in str(exc)
|
||||
if backend == "ani-cli" and no_files_downloaded and should_fallback:
|
||||
self._append_log(
|
||||
job,
|
||||
"ani-cli exited without downloading any files. Retrying with anipy-cli fallback.",
|
||||
)
|
||||
self._cleanup_staging_dir(job)
|
||||
staging_dir.mkdir(parents=True, exist_ok=True)
|
||||
finalize_error = None
|
||||
continue
|
||||
else:
|
||||
successful_backend = backend
|
||||
if self.watchlist_sync_fn is not None:
|
||||
try:
|
||||
synced_watchlist_item = self.watchlist_sync_fn(job)
|
||||
except Exception as exc:
|
||||
watchlist_sync_error = exc
|
||||
break
|
||||
break
|
||||
|
||||
with self.lock:
|
||||
canceled = job.get("cancel_requested")
|
||||
@@ -1471,13 +1553,15 @@ class DownloadQueue:
|
||||
message = str(finalize_error).strip()
|
||||
if "No downloaded files were found in the staging folder" in message:
|
||||
job.setdefault("log", []).append(
|
||||
"ani-cli exited without downloading any files. The selected result or episodes may not be downloadable."
|
||||
f"{job.get('download_backend') or 'Downloader'} exited without downloading any files. The selected result or episodes may not be downloadable."
|
||||
)
|
||||
else:
|
||||
job.setdefault("log", []).append(f"Library rename failed: {finalize_error}")
|
||||
cleanup_staging = True
|
||||
elif exit_code == 0:
|
||||
job["status"] = "done"
|
||||
if successful_backend == "anipy-cli":
|
||||
job.setdefault("log", []).append("Fallback download completed with anipy-cli.")
|
||||
for path in moved_files:
|
||||
job.setdefault("log", []).append(f"Saved: {path}")
|
||||
job.setdefault("log", []).append("Download completed.")
|
||||
@@ -1490,7 +1574,9 @@ class DownloadQueue:
|
||||
job.setdefault("log", []).append("Watchlist download state synced.")
|
||||
else:
|
||||
job["status"] = "failed"
|
||||
job.setdefault("log", []).append(f"Download failed with exit code {exit_code}.")
|
||||
job.setdefault("log", []).append(
|
||||
f"{job.get('download_backend') or 'Download'} failed with exit code {exit_code}."
|
||||
)
|
||||
cleanup_staging = True
|
||||
if canceled:
|
||||
cleanup_staging = True
|
||||
|
||||
Reference in New Issue
Block a user