refactor(A1): remove Flamenco, simplify render pipeline to Celery-only
- Remove flamenco-manager and flamenco-worker from docker-compose.yml - Delete flamenco_client.py, flamenco_tasks.py, docker_scaler.py - Simplify render_dispatcher.py to Celery-only (removes ~300 lines) - Remove Flamenco beat schedule from celery_app.py - Clean admin.py: remove flamenco settings, endpoints, threejs validation - Clean orders.py cancel-render: Celery revoke only - Clean worker.py: remove flamenco_job_id from activity response - Migration 032: cancel lingering flamenco jobs, remove flamenco settings - PLAN.md: mark all decisions confirmed, status IN UMSETZUNG Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,177 +0,0 @@
|
||||
"""Scale Flamenco worker containers via the Docker socket.
|
||||
|
||||
Uses the Docker Python SDK (docker>=6.1.0) to list, start, and stop containers.
|
||||
Requires /var/run/docker.sock to be mounted into the backend container.
|
||||
"""
|
||||
import os
|
||||
import logging
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
COMPOSE_PROJECT = os.getenv("COMPOSE_PROJECT_NAME", "schaefflerautomat")
|
||||
SERVICE_NAME = "flamenco-worker"
|
||||
|
||||
|
||||
def _get_client():
|
||||
import docker
|
||||
return docker.from_env()
|
||||
|
||||
|
||||
def get_worker_containers(client=None):
|
||||
"""Return all flamenco-worker containers (running + stopped) sorted by name."""
|
||||
if client is None:
|
||||
client = _get_client()
|
||||
return sorted(
|
||||
client.containers.list(
|
||||
all=True,
|
||||
filters={
|
||||
"label": [
|
||||
f"com.docker.compose.project={COMPOSE_PROJECT}",
|
||||
f"com.docker.compose.service={SERVICE_NAME}",
|
||||
]
|
||||
},
|
||||
),
|
||||
key=lambda c: c.name,
|
||||
)
|
||||
|
||||
|
||||
def get_running_worker_count(client=None) -> int:
|
||||
"""Return how many flamenco-worker containers are currently running."""
|
||||
try:
|
||||
if client is None:
|
||||
client = _get_client()
|
||||
containers = get_worker_containers(client)
|
||||
return sum(1 for c in containers if c.status == "running")
|
||||
except Exception as exc:
|
||||
log.warning("docker_scaler: could not read worker count: %s", exc)
|
||||
return -1
|
||||
|
||||
|
||||
def scale_workers(target: int) -> dict:
|
||||
"""Scale flamenco-worker containers to *target* count.
|
||||
|
||||
Returns a dict with keys:
|
||||
previous – containers running before
|
||||
current – containers running after
|
||||
delta – change (negative = stopped, positive = started)
|
||||
message – human-readable summary
|
||||
"""
|
||||
import docker
|
||||
from docker.types import Mount
|
||||
|
||||
client = _get_client()
|
||||
|
||||
all_workers = get_worker_containers(client)
|
||||
running = [c for c in all_workers if c.status == "running"]
|
||||
previous = len(running)
|
||||
|
||||
if target == previous:
|
||||
return {"previous": previous, "current": previous, "delta": 0,
|
||||
"message": f"Already at {previous} worker(s) — no change"}
|
||||
|
||||
# ── Scale down ────────────────────────────────────────────────────────────
|
||||
if target < previous:
|
||||
# Stop highest-numbered containers first to minimise disruption
|
||||
to_stop = sorted(running, key=lambda c: c.name, reverse=True)[: previous - target]
|
||||
for c in to_stop:
|
||||
log.info("docker_scaler: stopping %s", c.name)
|
||||
c.stop(timeout=20)
|
||||
c.remove()
|
||||
return {
|
||||
"previous": previous,
|
||||
"current": target,
|
||||
"delta": target - previous,
|
||||
"message": f"Stopped {len(to_stop)} worker(s): {[c.name for c in to_stop]}",
|
||||
}
|
||||
|
||||
# ── Scale up ──────────────────────────────────────────────────────────────
|
||||
template = running[0] if running else (all_workers[0] if all_workers else None)
|
||||
if template is None:
|
||||
raise RuntimeError(
|
||||
"No existing flamenco-worker container found to clone configuration from. "
|
||||
"Ensure at least one worker container exists (even if stopped)."
|
||||
)
|
||||
|
||||
attrs = template.attrs
|
||||
image = attrs["Config"]["Image"]
|
||||
env = attrs["Config"].get("Env") or []
|
||||
|
||||
# Reconstruct mounts from the template container
|
||||
mounts = []
|
||||
for m in (attrs.get("Mounts") or []):
|
||||
mount_type = m.get("Type", "bind")
|
||||
source = m.get("Name", "") if mount_type == "volume" else m.get("Source", "")
|
||||
mounts.append(
|
||||
Mount(
|
||||
target=m["Destination"],
|
||||
source=source,
|
||||
type=mount_type,
|
||||
read_only=not m.get("RW", True),
|
||||
)
|
||||
)
|
||||
|
||||
# Reconstruct GPU device requests (nvidia)
|
||||
device_requests = None
|
||||
raw_dr = (attrs.get("HostConfig") or {}).get("DeviceRequests") or []
|
||||
if raw_dr:
|
||||
device_requests = []
|
||||
for dr in raw_dr:
|
||||
device_requests.append(
|
||||
docker.types.DeviceRequest(
|
||||
driver=dr.get("Driver", ""),
|
||||
count=dr.get("Count", -1),
|
||||
device_ids=dr.get("DeviceIDs") or [],
|
||||
capabilities=dr.get("Capabilities") or [],
|
||||
options=dr.get("Options") or {},
|
||||
)
|
||||
)
|
||||
|
||||
# Network(s) the template is connected to
|
||||
network_names = list(
|
||||
(attrs.get("NetworkSettings") or {}).get("Networks", {}).keys()
|
||||
)
|
||||
|
||||
restart_policy_name = (
|
||||
(attrs.get("HostConfig") or {})
|
||||
.get("RestartPolicy", {})
|
||||
.get("Name", "unless-stopped")
|
||||
) or "unless-stopped"
|
||||
|
||||
started = []
|
||||
for i in range(previous + 1, target + 1):
|
||||
new_name = f"{COMPOSE_PROJECT}-{SERVICE_NAME}-{i}"
|
||||
labels = {
|
||||
"com.docker.compose.project": COMPOSE_PROJECT,
|
||||
"com.docker.compose.service": SERVICE_NAME,
|
||||
"com.docker.compose.container-number": str(i),
|
||||
}
|
||||
|
||||
log.info("docker_scaler: creating %s from image %s", new_name, image)
|
||||
container = client.containers.create(
|
||||
image=image,
|
||||
name=new_name,
|
||||
environment=env,
|
||||
labels=labels,
|
||||
mounts=mounts,
|
||||
restart_policy={"Name": restart_policy_name},
|
||||
device_requests=device_requests,
|
||||
)
|
||||
|
||||
for net_name in network_names:
|
||||
try:
|
||||
net = client.networks.get(net_name)
|
||||
net.connect(container)
|
||||
log.info("docker_scaler: connected %s to network %s", new_name, net_name)
|
||||
except Exception as exc:
|
||||
log.warning("docker_scaler: could not connect to network %s: %s", net_name, exc)
|
||||
|
||||
container.start()
|
||||
started.append(new_name)
|
||||
log.info("docker_scaler: started %s", new_name)
|
||||
|
||||
return {
|
||||
"previous": previous,
|
||||
"current": target,
|
||||
"delta": target - previous,
|
||||
"message": f"Started {len(started)} new worker(s): {started}",
|
||||
}
|
||||
@@ -1,121 +0,0 @@
|
||||
"""Flamenco Manager REST API client.
|
||||
|
||||
Uses httpx (sync) for compatibility with Celery tasks and FastAPI endpoints.
|
||||
"""
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
import httpx
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
DEFAULT_TIMEOUT = 10.0
|
||||
|
||||
|
||||
class FlamencoClient:
|
||||
"""Thin wrapper around the Flamenco Manager v3 REST API."""
|
||||
|
||||
def __init__(self, manager_url: str):
|
||||
self.base_url = manager_url.rstrip("/")
|
||||
|
||||
def _url(self, path: str) -> str:
|
||||
return f"{self.base_url}{path}"
|
||||
|
||||
# ── Job management ──────────────────────────────────────────────────────
|
||||
|
||||
def submit_job(
|
||||
self,
|
||||
name: str,
|
||||
job_type: str,
|
||||
settings: dict[str, Any],
|
||||
metadata: dict[str, str] | None = None,
|
||||
priority: int = 50,
|
||||
) -> dict:
|
||||
"""Submit a new render job to Flamenco Manager.
|
||||
|
||||
Returns the created job dict (includes 'id').
|
||||
"""
|
||||
payload = {
|
||||
"name": name,
|
||||
"type": job_type,
|
||||
"submitter_platform": "linux",
|
||||
"settings": settings,
|
||||
"metadata": metadata or {},
|
||||
"priority": priority,
|
||||
}
|
||||
resp = httpx.post(
|
||||
self._url("/api/v3/jobs"),
|
||||
json=payload,
|
||||
timeout=DEFAULT_TIMEOUT,
|
||||
)
|
||||
resp.raise_for_status()
|
||||
return resp.json()
|
||||
|
||||
def get_job(self, job_id: str) -> dict:
|
||||
"""Get job details by ID."""
|
||||
resp = httpx.get(
|
||||
self._url(f"/api/v3/jobs/{job_id}"),
|
||||
timeout=DEFAULT_TIMEOUT,
|
||||
)
|
||||
resp.raise_for_status()
|
||||
return resp.json()
|
||||
|
||||
def cancel_job(self, job_id: str) -> None:
|
||||
"""Request cancellation of a job."""
|
||||
resp = httpx.post(
|
||||
self._url(f"/api/v3/jobs/{job_id}/setstatus"),
|
||||
json={"status": "cancel-requested"},
|
||||
timeout=DEFAULT_TIMEOUT,
|
||||
)
|
||||
resp.raise_for_status()
|
||||
|
||||
# ── Workers ─────────────────────────────────────────────────────────────
|
||||
|
||||
def list_workers(self) -> list[dict]:
|
||||
"""List all registered workers."""
|
||||
resp = httpx.get(
|
||||
self._url("/api/v3/worker-mgt/workers"),
|
||||
timeout=DEFAULT_TIMEOUT,
|
||||
)
|
||||
resp.raise_for_status()
|
||||
data = resp.json()
|
||||
return data.get("workers", data) if isinstance(data, dict) else data
|
||||
|
||||
# ── Farm status ─────────────────────────────────────────────────────────
|
||||
|
||||
def get_farm_status(self) -> dict:
|
||||
"""Get overall farm status from the Manager."""
|
||||
resp = httpx.get(
|
||||
self._url("/api/v3/configuration"),
|
||||
timeout=DEFAULT_TIMEOUT,
|
||||
)
|
||||
resp.raise_for_status()
|
||||
return resp.json()
|
||||
|
||||
def health_check(self) -> dict:
|
||||
"""Check if the Flamenco Manager is reachable and return version info."""
|
||||
try:
|
||||
resp = httpx.get(
|
||||
self._url("/api/v3/version"),
|
||||
timeout=5.0,
|
||||
)
|
||||
resp.raise_for_status()
|
||||
data = resp.json()
|
||||
return {
|
||||
"available": True,
|
||||
"version": data.get("version", "unknown"),
|
||||
"name": data.get("name", "Flamenco"),
|
||||
}
|
||||
except Exception as exc:
|
||||
logger.warning(f"Flamenco health check failed: {exc}")
|
||||
return {
|
||||
"available": False,
|
||||
"version": None,
|
||||
"name": None,
|
||||
"error": str(exc)[:200],
|
||||
}
|
||||
|
||||
|
||||
def get_flamenco_client(manager_url: str) -> FlamencoClient:
|
||||
"""Factory that creates a FlamencoClient from a manager URL."""
|
||||
return FlamencoClient(manager_url)
|
||||
@@ -1,12 +1,7 @@
|
||||
"""Render dispatcher — routes render jobs to Celery or Flamenco.
|
||||
"""Render dispatcher — routes render jobs to Celery.
|
||||
|
||||
Backend selection priority:
|
||||
1. OutputType.render_backend per-type override ("celery" / "flamenco")
|
||||
2. OutputType.is_animation — animations default to Flamenco
|
||||
3. System setting render_backend — global default ("celery" / "flamenco" / "auto")
|
||||
4. "auto" mode: stills → Celery, animations → Flamenco
|
||||
All renders run via Celery workers (Flamenco removed in v2 refactor).
|
||||
"""
|
||||
import json
|
||||
import logging
|
||||
from datetime import datetime
|
||||
|
||||
@@ -14,7 +9,6 @@ from sqlalchemy import select, update as sql_update
|
||||
from sqlalchemy.orm import Session, joinedload
|
||||
|
||||
from app.models.order_line import OrderLine
|
||||
from app.models.output_type import OutputType
|
||||
from app.models.product import Product
|
||||
from app.models.system_setting import SystemSetting
|
||||
|
||||
@@ -29,113 +23,11 @@ def _load_setting(session: Session, key: str, default: str = "") -> str:
|
||||
return row.value if row else default
|
||||
|
||||
|
||||
def resolve_backend(output_type: OutputType | None, system_backend: str) -> str:
|
||||
"""Determine which backend to use for a given output type.
|
||||
|
||||
Returns "celery" or "flamenco".
|
||||
"""
|
||||
if output_type is None:
|
||||
return "celery"
|
||||
|
||||
# Priority 1: explicit per-type override
|
||||
ot_backend = output_type.render_backend
|
||||
if ot_backend in ("celery", "flamenco"):
|
||||
return ot_backend
|
||||
|
||||
# Priority 2+3: is_animation + system setting
|
||||
if system_backend in ("celery", "flamenco"):
|
||||
return system_backend
|
||||
|
||||
# Priority 4: auto mode — animations → Flamenco, stills → Celery
|
||||
if output_type.is_animation:
|
||||
return "flamenco"
|
||||
return "celery"
|
||||
|
||||
|
||||
def build_flamenco_job_settings(
|
||||
output_type: OutputType,
|
||||
product: Product,
|
||||
step_path: str,
|
||||
output_dir: str,
|
||||
system_settings: dict[str, str],
|
||||
lighting_only: bool = False,
|
||||
shadow_catcher: bool = False,
|
||||
camera_orbit: bool = True,
|
||||
cycles_device: str = "auto",
|
||||
rotation_x: float = 0.0,
|
||||
rotation_y: float = 0.0,
|
||||
rotation_z: float = 0.0,
|
||||
) -> dict:
|
||||
"""Build Flamenco job settings from output type and product metadata."""
|
||||
render_settings = output_type.render_settings or {}
|
||||
engine = render_settings.get("engine", system_settings.get("blender_engine", "cycles"))
|
||||
samples_key = f"blender_{engine}_samples"
|
||||
samples = render_settings.get("samples", int(system_settings.get(samples_key, "256")))
|
||||
stl_quality = render_settings.get("stl_quality", system_settings.get("stl_quality", "low"))
|
||||
width = render_settings.get("width", 1920 if output_type.is_animation else 1024)
|
||||
height = render_settings.get("height", 1080 if output_type.is_animation else 1024)
|
||||
|
||||
part_colors = {}
|
||||
part_names_ordered = []
|
||||
if product.cad_file and product.cad_file.parsed_objects:
|
||||
part_names_ordered = product.cad_file.parsed_objects.get("objects", [])
|
||||
materials_source = product.cad_part_materials
|
||||
if materials_source:
|
||||
from app.services.step_processor import build_part_colors
|
||||
part_colors = build_part_colors(part_names_ordered, materials_source)
|
||||
|
||||
transparent_bg = bool(output_type.transparent_bg) if hasattr(output_type, 'transparent_bg') else False
|
||||
|
||||
settings = {
|
||||
"step_path": step_path,
|
||||
"engine": engine,
|
||||
"samples": samples,
|
||||
"stl_quality": stl_quality,
|
||||
"width": width,
|
||||
"height": height,
|
||||
"part_colors_json": json.dumps(part_colors),
|
||||
"transparent_bg": transparent_bg,
|
||||
"template_path": "",
|
||||
"target_collection": "Product",
|
||||
"material_library_path": "",
|
||||
"material_map_json": "{}",
|
||||
"part_names_ordered_json": json.dumps(part_names_ordered),
|
||||
"lighting_only": lighting_only,
|
||||
"shadow_catcher": shadow_catcher,
|
||||
"cycles_device": cycles_device,
|
||||
"rotation_x": rotation_x,
|
||||
"rotation_y": rotation_y,
|
||||
"rotation_z": rotation_z,
|
||||
}
|
||||
|
||||
for dk in ('noise_threshold', 'denoiser', 'denoising_input_passes',
|
||||
'denoising_prefilter', 'denoising_quality', 'denoising_use_gpu'):
|
||||
settings[dk] = str(render_settings.get(dk, ""))
|
||||
|
||||
if output_type.is_animation:
|
||||
# Turntable-specific settings
|
||||
output_name = render_settings.get("output_name", "turntable")
|
||||
settings["output_dir"] = output_dir
|
||||
settings["output_name"] = output_name
|
||||
settings["frame_count"] = render_settings.get("frame_count", 120)
|
||||
settings["fps"] = render_settings.get("fps", 30)
|
||||
settings["turntable_degrees"] = render_settings.get("turntable_degrees", 360)
|
||||
settings["turntable_axis"] = render_settings.get("turntable_axis", "world_z")
|
||||
settings["bg_color"] = render_settings.get("bg_color", "")
|
||||
settings["camera_orbit"] = camera_orbit
|
||||
else:
|
||||
# Still-specific settings
|
||||
ext = output_type.output_format or "png"
|
||||
settings["output_path"] = f"{output_dir}/render.{ext}"
|
||||
|
||||
return settings
|
||||
|
||||
|
||||
def dispatch_render(order_line_id: str) -> dict:
|
||||
"""Route a render job to Celery or Flamenco based on configuration.
|
||||
"""Dispatch a render job to Celery.
|
||||
|
||||
Must be called from a sync context (Celery task or sync wrapper).
|
||||
Returns {"backend": "celery"|"flamenco", "job_ref": str}.
|
||||
Returns {"backend": "celery", "job_ref": str}.
|
||||
"""
|
||||
from app.config import settings as app_settings
|
||||
from app.services.render_log import emit, clear
|
||||
@@ -179,196 +71,26 @@ def dispatch_render(order_line_id: str) -> dict:
|
||||
|
||||
cad_name = line.product.cad_file.original_name if line.product.cad_file else "?"
|
||||
emit(order_line_id, f"CAD file: {cad_name}")
|
||||
emit(order_line_id, "Dispatching to Celery render worker")
|
||||
|
||||
# Load system settings
|
||||
system_backend = _load_setting(session, "render_backend", "celery")
|
||||
flamenco_url = _load_setting(session, "flamenco_manager_url", "http://flamenco-manager:8080")
|
||||
|
||||
backend = resolve_backend(line.output_type, system_backend)
|
||||
emit(order_line_id, f"Resolved backend: {backend}")
|
||||
|
||||
# Mark as processing
|
||||
now = datetime.utcnow()
|
||||
session.execute(
|
||||
sql_update(OrderLine)
|
||||
.where(OrderLine.id == line.id)
|
||||
.values(
|
||||
render_status="processing",
|
||||
render_backend_used=backend,
|
||||
render_backend_used="celery",
|
||||
render_started_at=now,
|
||||
)
|
||||
)
|
||||
session.commit()
|
||||
|
||||
if backend == "flamenco":
|
||||
emit(order_line_id, f"Submitting job to Flamenco Manager ({flamenco_url})")
|
||||
result = _dispatch_flamenco(session, line, flamenco_url)
|
||||
if result.get("error"):
|
||||
emit(order_line_id, f"Flamenco submit failed: {result['error']}", "error")
|
||||
else:
|
||||
emit(order_line_id, f"Flamenco job submitted: {result.get('job_ref', '?')}")
|
||||
return result
|
||||
else:
|
||||
emit(order_line_id, "Dispatching to Celery render worker")
|
||||
return _dispatch_celery(order_line_id)
|
||||
|
||||
engine_db.dispose()
|
||||
return _dispatch_celery(order_line_id)
|
||||
|
||||
|
||||
def _dispatch_celery(order_line_id: str) -> dict:
|
||||
"""Dispatch to the existing Celery render task."""
|
||||
"""Dispatch to the Celery render task."""
|
||||
from app.tasks.step_tasks import render_order_line_task
|
||||
result = render_order_line_task.delay(order_line_id)
|
||||
return {"backend": "celery", "job_ref": result.id}
|
||||
|
||||
|
||||
def _dispatch_flamenco(session: Session, line: OrderLine, flamenco_url: str) -> dict:
|
||||
"""Submit a job to Flamenco Manager."""
|
||||
import re
|
||||
from app.services.flamenco_client import get_flamenco_client
|
||||
|
||||
# Load all needed system settings
|
||||
all_keys = ["blender_engine", "blender_cycles_samples", "blender_eevee_samples", "stl_quality", "cycles_device"]
|
||||
sys_settings = {}
|
||||
for key in all_keys:
|
||||
sys_settings[key] = _load_setting(session, key, "")
|
||||
|
||||
output_type = line.output_type
|
||||
product = line.product
|
||||
cad_file = product.cad_file
|
||||
|
||||
# Load render_position for rotation values
|
||||
rotation_x = rotation_y = rotation_z = 0.0
|
||||
if line.render_position_id:
|
||||
from app.models.render_position import ProductRenderPosition
|
||||
rp = session.get(ProductRenderPosition, line.render_position_id)
|
||||
if rp:
|
||||
rotation_x, rotation_y, rotation_z = rp.rotation_x, rp.rotation_y, rp.rotation_z
|
||||
|
||||
# Flamenco mounts the uploads volume at /shared, backend uses /app/uploads
|
||||
raw_path = cad_file.stored_path if cad_file else ""
|
||||
step_path = raw_path.replace("/app/uploads/", "/shared/") if raw_path else ""
|
||||
output_dir = f"/shared/renders/{line.id}"
|
||||
|
||||
job_type = "schaeffler-turntable" if (output_type and output_type.is_animation) else "schaeffler-still"
|
||||
|
||||
# Resolve render template + material library BEFORE building job settings
|
||||
# (template.lighting_only is needed by build_flamenco_job_settings)
|
||||
from app.services.template_service import resolve_template, get_material_library_path
|
||||
|
||||
category_key = product.category_key if product else None
|
||||
ot_id = str(line.output_type_id) if line.output_type_id else None
|
||||
template = resolve_template(category_key=category_key, output_type_id=ot_id)
|
||||
material_library = get_material_library_path()
|
||||
|
||||
# Resolve cycles_device: per-output-type override wins, fall back to system setting
|
||||
ot_cycles_device = output_type.cycles_device if output_type else None
|
||||
effective_cycles_device = ot_cycles_device or sys_settings.get("cycles_device", "gpu") or "gpu"
|
||||
|
||||
settings = build_flamenco_job_settings(
|
||||
output_type=output_type,
|
||||
product=product,
|
||||
step_path=step_path,
|
||||
output_dir=output_dir,
|
||||
system_settings=sys_settings,
|
||||
lighting_only=bool(template.lighting_only) if template else False,
|
||||
shadow_catcher=bool(template.shadow_catcher_enabled) if template else False,
|
||||
camera_orbit=bool(template.camera_orbit) if template else True,
|
||||
cycles_device=effective_cycles_device,
|
||||
rotation_x=rotation_x,
|
||||
rotation_y=rotation_y,
|
||||
rotation_z=rotation_z,
|
||||
)
|
||||
|
||||
if template:
|
||||
# Remap path for Flamenco shared volume
|
||||
tmpl_path = template.blend_file_path.replace("/app/uploads/", "/shared/")
|
||||
settings["template_path"] = tmpl_path
|
||||
settings["target_collection"] = template.target_collection
|
||||
logger.info(
|
||||
f"Flamenco job: using render template '{template.name}' "
|
||||
f"(id={template.id}, path={tmpl_path}, collection={template.target_collection})"
|
||||
)
|
||||
else:
|
||||
logger.info(
|
||||
f"Flamenco job: no render template found for "
|
||||
f"category_key={category_key!r}, output_type_id={ot_id!r} — using factory settings"
|
||||
)
|
||||
|
||||
# Material library + material map: send whenever library exists and product
|
||||
# has material assignments — works with or without a render template.
|
||||
# When a template is present, only apply if material_replace_enabled is set.
|
||||
materials_source = product.cad_part_materials
|
||||
use_materials = bool(material_library and materials_source)
|
||||
if template and not template.material_replace_enabled:
|
||||
use_materials = False
|
||||
|
||||
if use_materials:
|
||||
mat_lib_path = material_library.replace("/app/uploads/", "/shared/")
|
||||
settings["material_library_path"] = mat_lib_path
|
||||
mat_map = {
|
||||
m["part_name"]: m["material"]
|
||||
for m in materials_source
|
||||
if m.get("part_name") and m.get("material")
|
||||
}
|
||||
# Resolve raw material names to SCHAEFFLER library names via aliases
|
||||
from app.services.material_service import resolve_material_map
|
||||
mat_map = resolve_material_map(mat_map)
|
||||
settings["material_map_json"] = json.dumps(mat_map)
|
||||
|
||||
# Output naming: meaningful filename instead of generic render.ext
|
||||
def _sanitize(s: str) -> str:
|
||||
return re.sub(r'[^\w\-.]', '_', s.strip())[:100]
|
||||
|
||||
product_name = product.name or product.pim_id or "product"
|
||||
ot_name = output_type.name if output_type else "render"
|
||||
|
||||
if not (output_type and output_type.is_animation):
|
||||
ext = output_type.output_format or "png" if output_type else "png"
|
||||
filename = f"{_sanitize(product_name)}_{_sanitize(ot_name)}.{ext}"
|
||||
settings["output_path"] = f"{output_dir}/{filename}"
|
||||
|
||||
metadata = {
|
||||
"order_line_id": str(line.id),
|
||||
"order_id": str(line.order_id),
|
||||
"product_name": product.name or "",
|
||||
"output_type": output_type.name if output_type else "",
|
||||
"category": product.category_key or "",
|
||||
}
|
||||
|
||||
job_name = f"{product.name or product.pim_id} - {output_type.name if output_type else 'render'}"
|
||||
|
||||
try:
|
||||
client = get_flamenco_client(flamenco_url)
|
||||
job = client.submit_job(
|
||||
name=job_name[:200],
|
||||
job_type=job_type,
|
||||
settings=settings,
|
||||
metadata=metadata,
|
||||
)
|
||||
job_id = job.get("id", "")
|
||||
|
||||
# Save flamenco_job_id
|
||||
session.execute(
|
||||
sql_update(OrderLine)
|
||||
.where(OrderLine.id == line.id)
|
||||
.values(flamenco_job_id=job_id)
|
||||
)
|
||||
session.commit()
|
||||
|
||||
logger.info(f"Flamenco job submitted: {job_id} for OrderLine {line.id}")
|
||||
return {"backend": "flamenco", "job_ref": job_id}
|
||||
|
||||
except Exception as exc:
|
||||
logger.error(f"Flamenco submit failed for OrderLine {line.id}: {exc}")
|
||||
session.execute(
|
||||
sql_update(OrderLine)
|
||||
.where(OrderLine.id == line.id)
|
||||
.values(
|
||||
render_status="failed",
|
||||
render_completed_at=datetime.utcnow(),
|
||||
render_log={"error": f"Flamenco submit failed: {str(exc)[:500]}"},
|
||||
)
|
||||
)
|
||||
session.commit()
|
||||
return {"backend": "flamenco", "job_ref": "", "error": str(exc)}
|
||||
|
||||
Reference in New Issue
Block a user