chore: snapshot workflow migration progress
This commit is contained in:
@@ -13,8 +13,25 @@ from app.core.pipeline_logger import PipelineLogger
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
def _usd_cache_hit_refresh_reason(cad_file, usd_asset, usd_render_path) -> str | None:
|
||||
"""Reuse the runtime freshness checks before accepting a USD cache hit."""
|
||||
from app.domains.rendering.workflow_runtime_services import _usd_master_refresh_reason
|
||||
|
||||
return _usd_master_refresh_reason(
|
||||
cad_file,
|
||||
usd_asset=usd_asset,
|
||||
usd_render_path=usd_render_path,
|
||||
)
|
||||
|
||||
|
||||
@celery_app.task(bind=True, name="app.tasks.step_tasks.generate_gltf_geometry_task", queue="asset_pipeline", max_retries=1)
|
||||
def generate_gltf_geometry_task(self, cad_file_id: str):
|
||||
def generate_gltf_geometry_task(
|
||||
self,
|
||||
cad_file_id: str,
|
||||
workflow_run_id: str | None = None,
|
||||
workflow_node_id: str | None = None,
|
||||
**_: object,
|
||||
):
|
||||
"""Export a geometry GLB directly from STEP via OCC (no STL intermediary).
|
||||
|
||||
Pipeline:
|
||||
@@ -94,10 +111,10 @@ def generate_gltf_geometry_task(self, cad_file_id: str):
|
||||
_current_hash = _compute_step_hash(str(step_path_str))
|
||||
_cache_hit_asset_id = None
|
||||
|
||||
# Composite cache key includes deflection settings so changing them invalidates cache
|
||||
# v3: removed BRepBuilderAPI_Transform, writer handles mm→m from STEP unit metadata
|
||||
# Composite cache key includes deflection settings so changing them invalidates cache.
|
||||
# v5: occurrence-aware part-key stamping for repeated leaf meshes changed.
|
||||
effective_cache_key = (
|
||||
f"v3:{_current_hash}:{linear_deflection}:{angular_deflection}:{tessellation_engine}"
|
||||
f"v5:{_current_hash}:{linear_deflection}:{angular_deflection}:{tessellation_engine}"
|
||||
if _current_hash else None
|
||||
)
|
||||
|
||||
@@ -112,6 +129,9 @@ def generate_gltf_geometry_task(self, cad_file_id: str):
|
||||
if stored_key == effective_cache_key:
|
||||
_asset_disk_path = _Path(app_settings.upload_dir) / existing_geo.storage_key
|
||||
if _asset_disk_path.exists():
|
||||
if cad_file.gltf_path != str(_asset_disk_path):
|
||||
cad_file.gltf_path = str(_asset_disk_path)
|
||||
session.commit()
|
||||
logger.info("[CACHE] cache key match — skipping geometry GLB tessellation for %s", cad_file_id)
|
||||
pl.step_done("export_glb_geometry", result={"cached": True, "asset_id": str(existing_geo.id)})
|
||||
_cache_hit_asset_id = str(existing_geo.id)
|
||||
@@ -133,6 +153,20 @@ def generate_gltf_geometry_task(self, cad_file_id: str):
|
||||
generate_usd_master_task.delay(cad_file_id)
|
||||
except Exception:
|
||||
logger.debug("Could not queue generate_usd_master_task from cache-hit path (non-fatal)")
|
||||
try:
|
||||
from app.domains.rendering.tasks import _update_workflow_run_status
|
||||
|
||||
_update_workflow_run_status(
|
||||
cad_file_id,
|
||||
"completed",
|
||||
workflow_run_id=workflow_run_id,
|
||||
workflow_node_id=workflow_node_id,
|
||||
)
|
||||
except Exception:
|
||||
logger.exception(
|
||||
"Failed to update workflow state for cached GLB export %s",
|
||||
cad_file_id,
|
||||
)
|
||||
return {"cached": True, "asset_id": _cache_hit_asset_id}
|
||||
|
||||
step = _Path(step_path_str)
|
||||
@@ -219,6 +253,9 @@ def generate_gltf_geometry_task(self, cad_file_id: str):
|
||||
existing.render_config = {"cache_key": effective_cache_key}
|
||||
if product_id:
|
||||
existing.product_id = _uuid.UUID(product_id)
|
||||
cad_file = _sess.get(CadFile, _uuid.UUID(cad_file_id))
|
||||
if cad_file is not None:
|
||||
cad_file.gltf_path = str(output_path)
|
||||
_sess.commit()
|
||||
asset_id = str(existing.id)
|
||||
else:
|
||||
@@ -232,12 +269,26 @@ def generate_gltf_geometry_task(self, cad_file_id: str):
|
||||
render_config={"cache_key": effective_cache_key},
|
||||
)
|
||||
_sess.add(asset)
|
||||
cad_file = _sess.get(CadFile, _uuid.UUID(cad_file_id))
|
||||
if cad_file is not None:
|
||||
cad_file.gltf_path = str(output_path)
|
||||
_sess.commit()
|
||||
asset_id = str(asset.id)
|
||||
_eng2.dispose()
|
||||
|
||||
pl.step_done("export_glb_geometry", result={"glb_path": str(output_path), "asset_id": asset_id})
|
||||
logger.info("generate_gltf_geometry_task: MediaAsset %s created for cad %s", asset_id, cad_file_id)
|
||||
try:
|
||||
from app.domains.rendering.tasks import _update_workflow_run_status
|
||||
|
||||
_update_workflow_run_status(
|
||||
cad_file_id,
|
||||
"completed",
|
||||
workflow_run_id=workflow_run_id,
|
||||
workflow_node_id=workflow_node_id,
|
||||
)
|
||||
except Exception:
|
||||
logger.exception("Failed to update workflow state for GLB export %s", cad_file_id)
|
||||
|
||||
# Auto-chain USD master export so the canonical scene is always up to date
|
||||
try:
|
||||
@@ -346,6 +397,33 @@ def generate_usd_master_task(self, cad_file_id: str) -> dict:
|
||||
angular_deflection = float(sys_settings.get("render_angular_deflection", "0.05"))
|
||||
sharp_threshold = float(sys_settings.get("sharp_edge_threshold", "20.0"))
|
||||
|
||||
scripts_dir = _Path(_os.environ.get("RENDER_SCRIPTS_DIR", "/render-scripts"))
|
||||
script_path = scripts_dir / "export_step_to_usd.py"
|
||||
materials_helper_path = scripts_dir / "_blender_materials.py"
|
||||
|
||||
if not script_path.exists():
|
||||
err = f"export_step_to_usd.py not found at {script_path}"
|
||||
pl.step_error("usd_master", err, None)
|
||||
raise RuntimeError(err)
|
||||
|
||||
# Cache must include the active render-script revision. Otherwise
|
||||
# material resolution fixes never invalidate previously generated USD masters.
|
||||
script_fingerprint = "unknown"
|
||||
try:
|
||||
import hashlib as _hashlib_script
|
||||
|
||||
_script_hash = _hashlib_script.sha256()
|
||||
for candidate in (script_path, materials_helper_path):
|
||||
if not candidate.exists():
|
||||
continue
|
||||
_script_hash.update(candidate.read_bytes())
|
||||
script_fingerprint = _script_hash.hexdigest()[:12]
|
||||
except Exception as exc:
|
||||
logger.warning(
|
||||
"[USD_MASTER] failed to fingerprint render scripts, falling back to legacy cache key: %s",
|
||||
exc,
|
||||
)
|
||||
|
||||
# Hash-based cache check: skip tessellation if file and settings haven't changed
|
||||
from app.domains.products.cache_service import compute_step_hash as _compute_step_hash_usd
|
||||
_current_hash_usd = _compute_step_hash_usd(str(step_path))
|
||||
@@ -357,7 +435,7 @@ def generate_usd_master_task(self, cad_file_id: str) -> dict:
|
||||
_json.dumps(material_map, sort_keys=True).encode()
|
||||
).hexdigest()[:12] if material_map else "none"
|
||||
effective_cache_key = (
|
||||
f"{_current_hash_usd}:{linear_deflection}:{angular_deflection}:{sharp_threshold}:{_mat_hash}"
|
||||
f"{_current_hash_usd}:{linear_deflection}:{angular_deflection}:{sharp_threshold}:{_mat_hash}:{script_fingerprint}"
|
||||
if _current_hash_usd else None
|
||||
)
|
||||
|
||||
@@ -372,9 +450,21 @@ def generate_usd_master_task(self, cad_file_id: str) -> dict:
|
||||
if stored_key == effective_cache_key:
|
||||
_usd_disk_path = _Path(app_settings.upload_dir) / existing_usd.storage_key
|
||||
if _usd_disk_path.exists():
|
||||
logger.info("[CACHE] cache key match — skipping USD master tessellation for %s", cad_file_id)
|
||||
pl.step_done("usd_master", result={"cached": True, "asset_id": str(existing_usd.id)})
|
||||
_cache_hit_asset_id = str(existing_usd.id)
|
||||
refresh_reason = _usd_cache_hit_refresh_reason(
|
||||
cad_file,
|
||||
existing_usd,
|
||||
_usd_disk_path,
|
||||
)
|
||||
if refresh_reason is None:
|
||||
logger.info("[CACHE] cache key match — skipping USD master tessellation for %s", cad_file_id)
|
||||
pl.step_done("usd_master", result={"cached": True, "asset_id": str(existing_usd.id)})
|
||||
_cache_hit_asset_id = str(existing_usd.id)
|
||||
else:
|
||||
logger.info(
|
||||
"[CACHE] USD cache key matched for %s but asset is stale (%s) — rebuilding",
|
||||
cad_file_id,
|
||||
refresh_reason,
|
||||
)
|
||||
else:
|
||||
logger.info("[CACHE] cache key match but USD asset missing on disk — re-running tessellation for %s", cad_file_id)
|
||||
else:
|
||||
@@ -396,13 +486,6 @@ def generate_usd_master_task(self, cad_file_id: str) -> dict:
|
||||
raise RuntimeError(err)
|
||||
|
||||
output_path = step_path.parent / f"{step_path.stem}_master.usd"
|
||||
scripts_dir = _Path(_os.environ.get("RENDER_SCRIPTS_DIR", "/render-scripts"))
|
||||
script_path = scripts_dir / "export_step_to_usd.py"
|
||||
|
||||
if not script_path.exists():
|
||||
err = f"export_step_to_usd.py not found at {script_path}"
|
||||
pl.step_error("usd_master", err, None)
|
||||
raise RuntimeError(err)
|
||||
|
||||
cmd = [
|
||||
_sys.executable, str(script_path),
|
||||
|
||||
@@ -31,7 +31,13 @@ def _bbox_from_step_cadquery(step_path: str) -> dict | None:
|
||||
|
||||
|
||||
@celery_app.task(bind=True, name="app.tasks.step_tasks.process_step_file", queue="step_processing")
|
||||
def process_step_file(self, cad_file_id: str):
|
||||
def process_step_file(
|
||||
self,
|
||||
cad_file_id: str,
|
||||
workflow_run_id: str | None = None,
|
||||
workflow_node_id: str | None = None,
|
||||
**_: object,
|
||||
):
|
||||
"""Process a STEP file: extract objects, generate thumbnail, convert to glTF.
|
||||
|
||||
After processing completes, auto-populate cad_part_materials from Excel
|
||||
@@ -122,10 +128,24 @@ def process_step_file(self, cad_file_id: str):
|
||||
r.delete(lock_key) # always release on completion or unhandled error
|
||||
|
||||
pl.step_done("process_step_file")
|
||||
try:
|
||||
from app.domains.rendering.tasks import _update_workflow_run_status
|
||||
|
||||
# Queue thumbnail rendering on the dedicated single-concurrency worker
|
||||
from app.domains.pipeline.tasks.render_thumbnail import render_step_thumbnail
|
||||
render_step_thumbnail.delay(cad_file_id)
|
||||
_update_workflow_run_status(
|
||||
cad_file_id,
|
||||
"completed",
|
||||
workflow_run_id=workflow_run_id,
|
||||
workflow_node_id=workflow_node_id,
|
||||
)
|
||||
except Exception:
|
||||
logger.exception("Failed to update workflow state for process_step_file %s", cad_file_id)
|
||||
|
||||
# Legacy flow still auto-queues thumbnail generation here.
|
||||
# Graph-mode workflows dispatch explicit thumbnail save/render nodes instead.
|
||||
if workflow_run_id is None:
|
||||
from app.domains.pipeline.tasks.render_thumbnail import render_step_thumbnail
|
||||
|
||||
render_step_thumbnail.delay(cad_file_id)
|
||||
|
||||
|
||||
def _auto_populate_materials_for_cad(cad_file_id: str, tenant_id: str | None = None) -> None:
|
||||
|
||||
@@ -8,6 +8,7 @@ import logging
|
||||
from datetime import datetime
|
||||
|
||||
from app.tasks.celery_app import celery_app
|
||||
from app.core.render_paths import ensure_group_writable_dir
|
||||
from app.core.task_logs import log_task_event
|
||||
from app.core.pipeline_logger import PipelineLogger
|
||||
|
||||
@@ -149,7 +150,7 @@ def render_order_line_task(self, order_line_id: str):
|
||||
product_name = render_invocation.product_name
|
||||
ot_name = render_invocation.output_type_name
|
||||
output_path = render_invocation.output_path
|
||||
_Path(output_path).parent.mkdir(parents=True, exist_ok=True)
|
||||
ensure_group_writable_dir(_Path(output_path).parent)
|
||||
render_width = render_invocation.width
|
||||
render_height = render_invocation.height
|
||||
render_engine = render_invocation.engine
|
||||
|
||||
@@ -19,6 +19,247 @@ logger = logging.getLogger(__name__)
|
||||
_THUMBNAIL_SAMPLE_CAP = 64
|
||||
|
||||
|
||||
def _resolve_thumbnail_render_context(session, cad) -> dict[str, object]:
|
||||
"""Reuse workflow material/USD resolution for CAD thumbnails when possible."""
|
||||
context: dict[str, object] = {}
|
||||
if not cad:
|
||||
return context
|
||||
|
||||
parsed_objects = cad.parsed_objects if isinstance(cad.parsed_objects, dict) else {}
|
||||
raw_part_names = parsed_objects.get("objects") if isinstance(parsed_objects, dict) else None
|
||||
if isinstance(raw_part_names, list):
|
||||
part_names_ordered = [
|
||||
str(part_name).strip()
|
||||
for part_name in raw_part_names
|
||||
if isinstance(part_name, str) and part_name.strip()
|
||||
]
|
||||
if part_names_ordered:
|
||||
context["part_names_ordered"] = part_names_ordered
|
||||
|
||||
try:
|
||||
from sqlalchemy import select
|
||||
|
||||
from app.core.render_paths import resolve_result_path
|
||||
from app.domains.media.models import MediaAsset, MediaAssetType
|
||||
from app.domains.products.models import Product
|
||||
from app.domains.rendering.workflow_runtime_services import (
|
||||
_build_effective_material_lookup,
|
||||
_usd_master_refresh_reason,
|
||||
)
|
||||
from app.services.material_service import resolve_material_map
|
||||
from app.services.template_service import get_material_library_path_for_session
|
||||
|
||||
product = session.execute(
|
||||
select(Product)
|
||||
.where(Product.cad_file_id == cad.id)
|
||||
.order_by(Product.is_active.desc(), Product.updated_at.desc(), Product.created_at.desc())
|
||||
.limit(1)
|
||||
).scalar_one_or_none()
|
||||
|
||||
material_library_path = get_material_library_path_for_session(session)
|
||||
materials_source = product.cad_part_materials or [] if product else []
|
||||
raw_material_map = _build_effective_material_lookup(cad, materials_source)
|
||||
if material_library_path and raw_material_map:
|
||||
material_map = resolve_material_map(raw_material_map)
|
||||
if material_map:
|
||||
context["material_library_path"] = material_library_path
|
||||
context["material_map"] = material_map
|
||||
|
||||
usd_asset = session.execute(
|
||||
select(MediaAsset)
|
||||
.where(
|
||||
MediaAsset.cad_file_id == cad.id,
|
||||
MediaAsset.asset_type == MediaAssetType.usd_master,
|
||||
)
|
||||
.order_by(MediaAsset.created_at.desc())
|
||||
.limit(1)
|
||||
).scalar_one_or_none()
|
||||
if usd_asset:
|
||||
usd_path = resolve_result_path(usd_asset.storage_key)
|
||||
refresh_reason = _usd_master_refresh_reason(
|
||||
cad,
|
||||
usd_asset=usd_asset,
|
||||
usd_render_path=usd_path,
|
||||
)
|
||||
if refresh_reason is None and usd_path and usd_path.exists():
|
||||
context["usd_path"] = usd_path
|
||||
except Exception:
|
||||
logger.exception("Failed to resolve thumbnail render context for cad %s", getattr(cad, "id", None))
|
||||
|
||||
return context
|
||||
|
||||
|
||||
def _render_thumbnail_core(
|
||||
*,
|
||||
cad_file_id: str,
|
||||
workflow_run_id: str | None = None,
|
||||
workflow_node_id: str | None = None,
|
||||
renderer: str | None = None,
|
||||
render_engine: str | None = None,
|
||||
samples: int | None = None,
|
||||
width: int | None = None,
|
||||
height: int | None = None,
|
||||
transparent_bg: bool | None = None,
|
||||
include_postprocess: bool,
|
||||
queue_legacy_glb_follow_up: bool,
|
||||
) -> None:
|
||||
"""Render a CAD thumbnail with optional legacy post-processing."""
|
||||
pl = PipelineLogger(task_id=None)
|
||||
pl.step_start("render_step_thumbnail", {"cad_file_id": cad_file_id})
|
||||
logger.info("Rendering thumbnail for CAD file: %s", cad_file_id)
|
||||
|
||||
from app.core.tenant_context import resolve_tenant_id_for_cad
|
||||
|
||||
tenant_id = resolve_tenant_id_for_cad(cad_file_id)
|
||||
|
||||
try:
|
||||
from app.models.cad_file import CadFile
|
||||
from app.domains.products.cache_service import compute_step_hash
|
||||
|
||||
with _pipeline_session(tenant_id) as session:
|
||||
cad = session.get(CadFile, cad_file_id)
|
||||
if cad and cad.stored_path and not cad.step_file_hash:
|
||||
cad.step_file_hash = compute_step_hash(cad.stored_path)
|
||||
session.commit()
|
||||
logger.info("Saved step_file_hash for %s: %s…", cad_file_id, cad.step_file_hash[:12])
|
||||
except Exception:
|
||||
logger.warning("step_file_hash computation failed for %s (non-fatal)", cad_file_id)
|
||||
|
||||
render_context: dict[str, object] = {}
|
||||
try:
|
||||
from app.models.cad_file import CadFile
|
||||
|
||||
with _pipeline_session(tenant_id) as session:
|
||||
cad = session.get(CadFile, cad_file_id)
|
||||
render_context = _resolve_thumbnail_render_context(session, cad)
|
||||
except Exception:
|
||||
logger.warning("thumbnail render context resolution failed for %s; using fallback render path", cad_file_id)
|
||||
|
||||
try:
|
||||
from app.services.step_processor import regenerate_cad_thumbnail
|
||||
|
||||
pl.info("render_step_thumbnail", "Calling regenerate_cad_thumbnail")
|
||||
with _capped_thumbnail_samples():
|
||||
success = regenerate_cad_thumbnail(
|
||||
cad_file_id,
|
||||
part_colors={},
|
||||
renderer=renderer,
|
||||
render_engine=render_engine,
|
||||
samples=samples,
|
||||
width=width,
|
||||
height=height,
|
||||
transparent_bg=transparent_bg,
|
||||
**render_context,
|
||||
)
|
||||
if not success:
|
||||
raise RuntimeError("regenerate_cad_thumbnail returned False")
|
||||
except Exception as exc:
|
||||
pl.step_error("render_step_thumbnail", f"Thumbnail render failed: {exc}", exc)
|
||||
logger.error("Thumbnail render failed for %s: %s", cad_file_id, exc)
|
||||
raise
|
||||
|
||||
resolved_tenant_id: str | None = None
|
||||
if include_postprocess:
|
||||
try:
|
||||
from app.models.cad_file import CadFile
|
||||
from app.domains.rendering.workflow_runtime_services import resolve_cad_bbox
|
||||
|
||||
with _pipeline_session(tenant_id) as session:
|
||||
cad = session.get(CadFile, cad_file_id)
|
||||
if not cad:
|
||||
logger.warning("CadFile %s not found in post-render phase", cad_file_id)
|
||||
else:
|
||||
step_path = cad.stored_path
|
||||
attrs = cad.mesh_attributes or {}
|
||||
|
||||
if step_path and not attrs.get("dimensions_mm"):
|
||||
step_file = Path(step_path)
|
||||
glb_path = step_file.parent / f"{step_file.stem}_thumbnail.glb"
|
||||
bbox_data = resolve_cad_bbox(step_path, glb_path=str(glb_path)).bbox_data
|
||||
if bbox_data:
|
||||
cad.mesh_attributes = {**attrs, **bbox_data}
|
||||
attrs = cad.mesh_attributes
|
||||
dims = bbox_data["dimensions_mm"]
|
||||
logger.info(
|
||||
"bbox for %s: %s×%s×%s mm",
|
||||
cad_file_id,
|
||||
dims["x"],
|
||||
dims["y"],
|
||||
dims["z"],
|
||||
)
|
||||
|
||||
if step_path and "sharp_edge_pairs" not in attrs:
|
||||
try:
|
||||
from app.services.step_processor import extract_mesh_edge_data
|
||||
|
||||
edge_data = extract_mesh_edge_data(step_path)
|
||||
if edge_data:
|
||||
cad.mesh_attributes = {**attrs, **edge_data}
|
||||
n_pairs = len(edge_data.get("sharp_edge_pairs", []))
|
||||
logger.info(
|
||||
"Sharp edge data extracted for %s: %s sharp edges",
|
||||
cad_file_id,
|
||||
n_pairs,
|
||||
)
|
||||
except Exception:
|
||||
logger.exception(
|
||||
"Sharp edge extraction failed for %s (non-fatal)",
|
||||
cad_file_id,
|
||||
)
|
||||
|
||||
session.commit()
|
||||
resolved_tenant_id = str(cad.tenant_id) if cad.tenant_id else None
|
||||
except Exception:
|
||||
logger.exception("Post-render processing failed for %s (non-fatal)", cad_file_id)
|
||||
|
||||
try:
|
||||
from app.domains.pipeline.tasks.extract_metadata import _auto_populate_materials_for_cad
|
||||
|
||||
_auto_populate_materials_for_cad(cad_file_id, tenant_id=tenant_id)
|
||||
except Exception:
|
||||
logger.exception(
|
||||
"Auto material population failed for cad_file %s (non-fatal)",
|
||||
cad_file_id,
|
||||
)
|
||||
|
||||
try:
|
||||
if resolved_tenant_id:
|
||||
from app.core.websocket import publish_event_sync
|
||||
|
||||
publish_event_sync(
|
||||
resolved_tenant_id,
|
||||
{
|
||||
"type": "cad_processing_complete",
|
||||
"cad_file_id": cad_file_id,
|
||||
"status": "completed",
|
||||
},
|
||||
)
|
||||
except Exception:
|
||||
logger.debug("WebSocket publish for CAD complete skipped (non-fatal)")
|
||||
|
||||
if queue_legacy_glb_follow_up:
|
||||
try:
|
||||
from app.domains.pipeline.tasks.export_glb import generate_gltf_geometry_task
|
||||
|
||||
generate_gltf_geometry_task.delay(cad_file_id)
|
||||
pl.info("render_step_thumbnail", f"Queued generate_gltf_geometry_task for {cad_file_id}")
|
||||
except Exception:
|
||||
logger.debug("Could not queue generate_gltf_geometry_task (non-fatal)")
|
||||
|
||||
pl.step_done("render_step_thumbnail")
|
||||
try:
|
||||
from app.domains.rendering.tasks import _update_workflow_run_status
|
||||
|
||||
_update_workflow_run_status(
|
||||
cad_file_id,
|
||||
"completed",
|
||||
workflow_run_id=workflow_run_id,
|
||||
workflow_node_id=workflow_node_id,
|
||||
)
|
||||
except Exception:
|
||||
logger.exception("Failed to update workflow state for thumbnail render %s", cad_file_id)
|
||||
|
||||
|
||||
@contextmanager
|
||||
def _capped_thumbnail_samples():
|
||||
"""Temporarily cap render samples for thumbnail renders.
|
||||
@@ -73,123 +314,88 @@ def _pipeline_session(tenant_id: str | None = None):
|
||||
|
||||
|
||||
@celery_app.task(bind=True, name="app.tasks.step_tasks.render_step_thumbnail", queue="asset_pipeline")
|
||||
def render_step_thumbnail(self, cad_file_id: str):
|
||||
def render_step_thumbnail(
|
||||
self,
|
||||
cad_file_id: str,
|
||||
workflow_run_id: str | None = None,
|
||||
workflow_node_id: str | None = None,
|
||||
renderer: str | None = None,
|
||||
render_engine: str | None = None,
|
||||
samples: int | None = None,
|
||||
width: int | None = None,
|
||||
height: int | None = None,
|
||||
transparent_bg: bool | None = None,
|
||||
**_: object,
|
||||
):
|
||||
"""Render the thumbnail for a freshly-processed STEP file.
|
||||
|
||||
Runs on the dedicated asset_pipeline queue (concurrency=1) so the
|
||||
blender-renderer service is never overwhelmed by concurrent requests.
|
||||
On success, also auto-populates materials and marks the CadFile as completed.
|
||||
"""
|
||||
pl = PipelineLogger(task_id=self.request.id)
|
||||
pl.step_start("render_step_thumbnail", {"cad_file_id": cad_file_id})
|
||||
logger.info(f"Rendering thumbnail for CAD file: {cad_file_id}")
|
||||
|
||||
from app.core.tenant_context import resolve_tenant_id_for_cad
|
||||
_tenant_id = resolve_tenant_id_for_cad(cad_file_id)
|
||||
|
||||
# ── Pre-render: compute hash ──────────────────────────────────────────
|
||||
try:
|
||||
from app.models.cad_file import CadFile
|
||||
from app.domains.products.cache_service import compute_step_hash
|
||||
|
||||
with _pipeline_session(_tenant_id) as session:
|
||||
cad = session.get(CadFile, cad_file_id)
|
||||
if cad and cad.stored_path and not cad.step_file_hash:
|
||||
cad.step_file_hash = compute_step_hash(cad.stored_path)
|
||||
session.commit()
|
||||
logger.info(f"Saved step_file_hash for {cad_file_id}: {cad.step_file_hash[:12]}…")
|
||||
except Exception:
|
||||
logger.warning(f"step_file_hash computation failed for {cad_file_id} (non-fatal)")
|
||||
|
||||
# ── Render thumbnail (with capped samples for 512x512) ──────────────
|
||||
try:
|
||||
from app.services.step_processor import regenerate_cad_thumbnail
|
||||
pl.info("render_step_thumbnail", "Calling regenerate_cad_thumbnail")
|
||||
with _capped_thumbnail_samples():
|
||||
success = regenerate_cad_thumbnail(cad_file_id, part_colors={})
|
||||
if not success:
|
||||
raise RuntimeError("regenerate_cad_thumbnail returned False")
|
||||
_render_thumbnail_core(
|
||||
cad_file_id=cad_file_id,
|
||||
workflow_run_id=workflow_run_id,
|
||||
workflow_node_id=workflow_node_id,
|
||||
renderer=renderer,
|
||||
render_engine=render_engine,
|
||||
samples=samples,
|
||||
width=width,
|
||||
height=height,
|
||||
transparent_bg=transparent_bg,
|
||||
include_postprocess=True,
|
||||
queue_legacy_glb_follow_up=workflow_run_id is None,
|
||||
)
|
||||
except Exception as exc:
|
||||
pl.step_error("render_step_thumbnail", f"Thumbnail render failed: {exc}", exc)
|
||||
logger.error(f"Thumbnail render failed for {cad_file_id}: {exc}")
|
||||
raise self.retry(exc=exc, countdown=30, max_retries=2)
|
||||
|
||||
# ── Post-render: bbox + sharp edges + materials (single session) ──────
|
||||
|
||||
@celery_app.task(bind=True, name="app.tasks.step_tasks.render_graph_thumbnail", queue="asset_pipeline")
|
||||
def render_graph_thumbnail(
|
||||
self,
|
||||
cad_file_id: str,
|
||||
workflow_run_id: str | None = None,
|
||||
workflow_node_id: str | None = None,
|
||||
renderer: str | None = None,
|
||||
render_engine: str | None = None,
|
||||
samples: int | None = None,
|
||||
width: int | None = None,
|
||||
height: int | None = None,
|
||||
transparent_bg: bool | None = None,
|
||||
**_: object,
|
||||
):
|
||||
"""Render a CAD thumbnail for graph workflows without legacy follow-up side effects."""
|
||||
try:
|
||||
from app.models.cad_file import CadFile
|
||||
from app.domains.rendering.workflow_runtime_services import resolve_cad_bbox
|
||||
|
||||
with _pipeline_session(_tenant_id) as session:
|
||||
cad = session.get(CadFile, cad_file_id)
|
||||
if not cad:
|
||||
logger.warning(f"CadFile {cad_file_id} not found in post-render phase")
|
||||
else:
|
||||
step_path = cad.stored_path
|
||||
attrs = cad.mesh_attributes or {}
|
||||
|
||||
# Bounding box extraction
|
||||
if step_path and not attrs.get("dimensions_mm"):
|
||||
_step = Path(step_path)
|
||||
_glb = _step.parent / f"{_step.stem}_thumbnail.glb"
|
||||
bbox_data = resolve_cad_bbox(step_path, glb_path=str(_glb)).bbox_data
|
||||
if bbox_data:
|
||||
cad.mesh_attributes = {**attrs, **bbox_data}
|
||||
attrs = cad.mesh_attributes
|
||||
dims = bbox_data["dimensions_mm"]
|
||||
logger.info(f"bbox for {cad_file_id}: {dims['x']}×{dims['y']}×{dims['z']} mm")
|
||||
|
||||
# Sharp edge extraction (PCurve-based, runs on render-worker with OCP)
|
||||
if step_path and "sharp_edge_pairs" not in attrs:
|
||||
try:
|
||||
from app.services.step_processor import extract_mesh_edge_data
|
||||
edge_data = extract_mesh_edge_data(step_path)
|
||||
if edge_data:
|
||||
cad.mesh_attributes = {**attrs, **edge_data}
|
||||
n_pairs = len(edge_data.get("sharp_edge_pairs", []))
|
||||
logger.info(f"Sharp edge data extracted for {cad_file_id}: {n_pairs} sharp edges")
|
||||
except Exception:
|
||||
logger.exception(f"Sharp edge extraction failed for {cad_file_id} (non-fatal)")
|
||||
|
||||
session.commit()
|
||||
|
||||
# WebSocket broadcast
|
||||
_tid = str(cad.tenant_id) if cad.tenant_id else None
|
||||
except Exception:
|
||||
logger.exception(f"Post-render processing failed for {cad_file_id} (non-fatal)")
|
||||
_tid = None
|
||||
|
||||
# Auto-populate materials
|
||||
try:
|
||||
from app.domains.pipeline.tasks.extract_metadata import _auto_populate_materials_for_cad
|
||||
_auto_populate_materials_for_cad(cad_file_id, tenant_id=_tenant_id)
|
||||
except Exception:
|
||||
logger.exception(f"Auto material population failed for cad_file {cad_file_id} (non-fatal)")
|
||||
|
||||
# Broadcast WebSocket event
|
||||
try:
|
||||
if _tid:
|
||||
from app.core.websocket import publish_event_sync
|
||||
publish_event_sync(_tid, {
|
||||
"type": "cad_processing_complete",
|
||||
"cad_file_id": cad_file_id,
|
||||
"status": "completed",
|
||||
})
|
||||
except Exception:
|
||||
logger.debug("WebSocket publish for CAD complete skipped (non-fatal)")
|
||||
|
||||
# Auto-generate geometry GLB
|
||||
try:
|
||||
from app.domains.pipeline.tasks.export_glb import generate_gltf_geometry_task
|
||||
generate_gltf_geometry_task.delay(cad_file_id)
|
||||
pl.info("render_step_thumbnail", f"Queued generate_gltf_geometry_task for {cad_file_id}")
|
||||
except Exception:
|
||||
logger.debug("Could not queue generate_gltf_geometry_task (non-fatal)")
|
||||
|
||||
pl.step_done("render_step_thumbnail")
|
||||
_render_thumbnail_core(
|
||||
cad_file_id=cad_file_id,
|
||||
workflow_run_id=workflow_run_id,
|
||||
workflow_node_id=workflow_node_id,
|
||||
renderer=renderer,
|
||||
render_engine=render_engine,
|
||||
samples=samples,
|
||||
width=width,
|
||||
height=height,
|
||||
transparent_bg=transparent_bg,
|
||||
include_postprocess=False,
|
||||
queue_legacy_glb_follow_up=False,
|
||||
)
|
||||
except Exception as exc:
|
||||
raise self.retry(exc=exc, countdown=30, max_retries=2)
|
||||
|
||||
|
||||
@celery_app.task(bind=True, name="app.tasks.step_tasks.regenerate_thumbnail", queue="asset_pipeline")
|
||||
def regenerate_thumbnail(self, cad_file_id: str, part_colors: dict):
|
||||
def regenerate_thumbnail(
|
||||
self,
|
||||
cad_file_id: str,
|
||||
part_colors: dict,
|
||||
renderer: str | None = None,
|
||||
render_engine: str | None = None,
|
||||
samples: int | None = None,
|
||||
width: int | None = None,
|
||||
height: int | None = None,
|
||||
transparent_bg: bool | None = None,
|
||||
):
|
||||
"""Regenerate thumbnail with per-part colours."""
|
||||
pl = PipelineLogger(task_id=self.request.id)
|
||||
pl.step_start("regenerate_thumbnail", {"cad_file_id": cad_file_id})
|
||||
@@ -200,11 +406,40 @@ def regenerate_thumbnail(self, cad_file_id: str, part_colors: dict):
|
||||
_tenant_id = resolve_tenant_id_for_cad(cad_file_id)
|
||||
|
||||
try:
|
||||
from app.services.step_processor import regenerate_cad_thumbnail
|
||||
from app.services.step_processor import MissingCadResourceError, regenerate_cad_thumbnail
|
||||
|
||||
render_context: dict[str, object] = {}
|
||||
try:
|
||||
from app.models.cad_file import CadFile
|
||||
|
||||
with _pipeline_session(_tenant_id) as session:
|
||||
cad = session.get(CadFile, cad_file_id)
|
||||
render_context = _resolve_thumbnail_render_context(session, cad)
|
||||
except Exception:
|
||||
logger.warning(
|
||||
"thumbnail render context resolution failed for %s during regeneration; using fallback render path",
|
||||
cad_file_id,
|
||||
)
|
||||
|
||||
with _capped_thumbnail_samples():
|
||||
success = regenerate_cad_thumbnail(cad_file_id, part_colors)
|
||||
success = regenerate_cad_thumbnail(
|
||||
cad_file_id,
|
||||
part_colors,
|
||||
renderer=renderer,
|
||||
render_engine=render_engine,
|
||||
samples=samples,
|
||||
width=width,
|
||||
height=height,
|
||||
transparent_bg=transparent_bg,
|
||||
**render_context,
|
||||
)
|
||||
if not success:
|
||||
raise RuntimeError("regenerate_cad_thumbnail returned False")
|
||||
except MissingCadResourceError as exc:
|
||||
pl.warning("regenerate_thumbnail", f"Skipping stale thumbnail regeneration: {exc}")
|
||||
logger.warning("Skipping thumbnail regeneration for %s: %s", cad_file_id, exc)
|
||||
pl.step_done("regenerate_thumbnail")
|
||||
return
|
||||
except Exception as exc:
|
||||
pl.step_error("regenerate_thumbnail", f"Thumbnail regeneration failed: {exc}", exc)
|
||||
logger.error(f"Thumbnail regeneration failed for {cad_file_id}: {exc}")
|
||||
|
||||
Reference in New Issue
Block a user