Add file size conversion and enhance Civitai job processing

- Introduced the `file_size_bytes` function to convert Civitai model sizes from kilobytes to bytes, improving data handling.
- Updated the `seed_civitai` function to include file size in job definitions, enhancing model processing efficiency.
- Enhanced the `should_skip` function to utilize expected size for faster decision-making during job processing.
- Added tests for new functionality, ensuring robustness in file size handling and job processing logic.
This commit is contained in:
Leonid Pershin
2026-08-21 10:52:44 +03:00
parent 6cdd6ecfa1
commit ef743a6e6d
6 changed files with 260 additions and 14 deletions
@@ -11,6 +11,7 @@ import base64
import hashlib
import json
import os
import re
import socket
import struct
import subprocess
@@ -28,6 +29,7 @@ DLBACKEND = DATA / "dlbackend"
COMFY_ROOT = DLBACKEND / "ComfyUI"
COMFY_VENV = COMFY_ROOT / "venv" / "bin" / "python"
SETTINGS = DATA / "Data" / "Settings.fds"
BACKENDS_FDS = DATA / "Data" / "Backends.fds"
# Comfy clone + torch can take 2040+ min on a cold disk.
INSTALL_TIMEOUT = float(os.environ.get("GPU_RENT_COMFY_INSTALL_TIMEOUT") or "3600")
@@ -255,6 +257,87 @@ def recover_errored_backends(*, wait_sec: float = 180.0) -> str:
return last
def sanitize_backends_fds() -> bool:
"""Repair ExtraArgs: \\x --flag (FDS empty + appended flag). Return True if changed."""
path = DATA / "Data" / "Backends.fds"
if not path.is_file():
return False
try:
text = path.read_text(encoding="utf-8")
except OSError:
return False
new, n = re.subn(r"^(\s*ExtraArgs:\s*)\\x(\s+)", r"\1", text, flags=re.M)
if not n:
return False
path.write_text(new, encoding="utf-8")
print(f"sanitized Backends.fds ExtraArgs \\x corruption ({n} line(s))", flush=True)
return True
def restart_swarmui_local() -> None:
print("systemctl restart swarmui (после fix Backends.fds)", flush=True)
try:
subprocess.run(
["sudo", "-n", "systemctl", "restart", "swarmui"],
check=False,
timeout=120,
)
except (OSError, subprocess.TimeoutExpired) as exc:
print(f"WARN restart swarmui: {exc}", flush=True)
wait_http(time.time() + 180)
def list_backends(sid: str) -> dict:
try:
return post(
"/API/ListBackends",
{"session_id": sid, "nonreal": False, "full_data": False},
timeout=30.0,
)
except Exception as exc:
return {"error": str(exc)}
def add_comfy_selfstart(sid: str) -> dict:
return post(
"/API/AddNewBackend",
{"session_id": sid, "type_id": "comfyui_selfstart"},
timeout=60.0,
)
def recover_empty_backends() -> str:
"""When API says empty but venv/IsInstalled exist — fix FDS and/or AddNewBackend."""
changed = sanitize_backends_fds()
if changed:
restart_swarmui_local()
bstat, _ = backend_status_detail()
if bstat not in ("empty", "unknown") and not bstat.startswith("error:"):
print(f"after sanitize: backend_status={bstat}", flush=True)
return bstat
sid = get_session(time.time() + 60)
backends = list_backends(sid)
if isinstance(backends, dict) and any(
isinstance(v, dict) and v.get("type") for v in backends.values()
):
bstat, _ = backend_status_detail()
return bstat
print("ListBackends empty — AddNewBackend comfyui_selfstart", flush=True)
try:
result = add_comfy_selfstart(sid)
print(f"AddNewBackend: {result}", flush=True)
except Exception as exc:
print(f"AddNewBackend FAIL: {exc}", flush=True)
return "empty"
# Default StartScript may be dlbackend/comfy/...; our tree is dlbackend/ComfyUI.
time.sleep(2)
bstat, _ = backend_status_detail()
return bstat
def run_diagnostics() -> None:
"""Best-effort: prefer uploaded swarm_diag.py, else journalctl snippet."""
diag = Path("/tmp/gpu-rent-swarm_diag.py")