Add queue download progress
This commit is contained in:
@@ -1254,6 +1254,7 @@ class DownloadQueue:
|
||||
job["finished_at"] = None
|
||||
job["updated_at"] = now_iso()
|
||||
job["cancel_requested"] = False
|
||||
job.pop("progress", None)
|
||||
job["log"] = []
|
||||
self._save_job_locked(job)
|
||||
return job
|
||||
@@ -1421,6 +1422,29 @@ class DownloadQueue:
|
||||
return self._job_from_row(row)
|
||||
raise KeyError("Job not found")
|
||||
|
||||
def _progress_from_line(self, text):
|
||||
if not str(text or "").startswith("KAIZOKU_PROGRESS "):
|
||||
return None
|
||||
payload = str(text).split(" ", 1)[1]
|
||||
try:
|
||||
progress = json.loads(payload)
|
||||
except json.JSONDecodeError:
|
||||
return None
|
||||
if not isinstance(progress, dict):
|
||||
return None
|
||||
try:
|
||||
progress["percent"] = max(0, min(100, int(float(progress.get("percent") or 0))))
|
||||
except (TypeError, ValueError):
|
||||
progress["percent"] = 0
|
||||
for key in ("episode_index", "episode_total", "segment", "segment_total"):
|
||||
if progress.get(key) in (None, ""):
|
||||
continue
|
||||
try:
|
||||
progress[key] = int(float(progress[key]))
|
||||
except (TypeError, ValueError):
|
||||
progress.pop(key, None)
|
||||
return progress
|
||||
|
||||
def _next_pending(self):
|
||||
with self.lock:
|
||||
with self._connect() as conn:
|
||||
@@ -1435,8 +1459,15 @@ class DownloadQueue:
|
||||
text = strip_control(line).strip()
|
||||
if not text:
|
||||
return
|
||||
progress = self._progress_from_line(text)
|
||||
if progress is not None:
|
||||
text = str(progress.get("message") or "").strip()
|
||||
if not text:
|
||||
text = f"Download progress: {progress.get('percent', 0)}%."
|
||||
self._stdout_log(job, text)
|
||||
with self.lock:
|
||||
if progress is not None:
|
||||
job["progress"] = progress
|
||||
job.setdefault("log", []).append(text)
|
||||
job["log"] = job["log"][-MAX_LOG_LINES:]
|
||||
job["updated_at"] = now_iso()
|
||||
@@ -1590,6 +1621,11 @@ class DownloadQueue:
|
||||
job["target_dir"] = str(target_dir)
|
||||
job["download_backend"] = attempts[0]["backend"]
|
||||
job["fallback_used"] = False
|
||||
job["progress"] = {
|
||||
"phase": "queued",
|
||||
"message": "Download queued.",
|
||||
"percent": 0,
|
||||
}
|
||||
job["log"] = [
|
||||
f"Starting: {job['command']}",
|
||||
f"Staging in: {job['staging_dir']}",
|
||||
@@ -1710,9 +1746,11 @@ class DownloadQueue:
|
||||
job["updated_at"] = job["finished_at"]
|
||||
if canceled:
|
||||
job["status"] = "canceled"
|
||||
job.setdefault("progress", {})["message"] = "Download canceled."
|
||||
job.setdefault("log", []).append("Canceled.")
|
||||
elif finalize_error:
|
||||
job["status"] = "failed"
|
||||
job.setdefault("progress", {})["message"] = "Download failed during library finalization."
|
||||
message = str(finalize_error).strip()
|
||||
if "No downloaded files were found in the staging folder" in message:
|
||||
job.setdefault("log", []).append(
|
||||
@@ -1723,6 +1761,12 @@ class DownloadQueue:
|
||||
cleanup_staging = True
|
||||
elif exit_code == 0:
|
||||
job["status"] = "done"
|
||||
job["progress"] = {
|
||||
**(job.get("progress") or {}),
|
||||
"phase": "done",
|
||||
"message": "Download completed.",
|
||||
"percent": 100,
|
||||
}
|
||||
if successful_backend and successful_backend != attempts[0]["backend"]:
|
||||
job.setdefault("log", []).append(f"Fallback download completed with {successful_backend}.")
|
||||
for path in moved_files:
|
||||
@@ -1737,6 +1781,7 @@ class DownloadQueue:
|
||||
job.setdefault("log", []).append("Watchlist download state synced.")
|
||||
else:
|
||||
job["status"] = "failed"
|
||||
job.setdefault("progress", {})["message"] = "Download failed."
|
||||
job.setdefault("log", []).append(
|
||||
f"{job.get('download_backend') or 'Download'} failed with exit code {exit_code}."
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user