Compare commits
4 Commits
feat/dag-a
...
dc64a66f0f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dc64a66f0f | ||
|
|
3d38e4b4c3 | ||
|
|
da2a360c56 | ||
|
|
3dd0da3964 |
156
freecad/runner.py
Normal file
156
freecad/runner.py
Normal file
@@ -0,0 +1,156 @@
|
||||
"""Headless runner entry points for silorunner compute jobs.
|
||||
|
||||
These functions are invoked via ``create --console -e`` by the
|
||||
silorunner binary. They must work without a display server.
|
||||
|
||||
Entry Points
|
||||
------------
|
||||
dag_extract(input_path, output_path)
|
||||
Extract feature DAG and write JSON.
|
||||
validate(input_path, output_path)
|
||||
Rebuild all features and report pass/fail per node.
|
||||
export(input_path, output_path, format='step')
|
||||
Export geometry to STEP, IGES, STL, or OBJ.
|
||||
"""
|
||||
|
||||
import json
|
||||
|
||||
import FreeCAD
|
||||
|
||||
|
||||
def dag_extract(input_path, output_path):
|
||||
"""Extract the feature DAG from a Create file.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
input_path : str
|
||||
Path to the ``.kc`` or ``.FCStd`` file.
|
||||
output_path : str
|
||||
Path to write the JSON output.
|
||||
|
||||
Output JSON::
|
||||
|
||||
{"nodes": [...], "edges": [...]}
|
||||
"""
|
||||
from dag import extract_dag
|
||||
|
||||
doc = FreeCAD.openDocument(input_path)
|
||||
try:
|
||||
nodes, edges = extract_dag(doc)
|
||||
with open(output_path, "w") as f:
|
||||
json.dump({"nodes": nodes, "edges": edges}, f)
|
||||
FreeCAD.Console.PrintMessage(
|
||||
f"DAG extracted: {len(nodes)} nodes, {len(edges)} edges -> {output_path}\n"
|
||||
)
|
||||
finally:
|
||||
FreeCAD.closeDocument(doc.Name)
|
||||
|
||||
|
||||
def validate(input_path, output_path):
|
||||
"""Validate a Create file by rebuilding all features.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
input_path : str
|
||||
Path to the ``.kc`` or ``.FCStd`` file.
|
||||
output_path : str
|
||||
Path to write the JSON output.
|
||||
|
||||
Output JSON::
|
||||
|
||||
{
|
||||
"valid": true/false,
|
||||
"nodes": [
|
||||
{"node_key": "Pad001", "state": "clean", "message": null, "properties_hash": "..."},
|
||||
...
|
||||
]
|
||||
}
|
||||
"""
|
||||
from dag import classify_type, compute_properties_hash
|
||||
|
||||
doc = FreeCAD.openDocument(input_path)
|
||||
try:
|
||||
doc.recompute()
|
||||
|
||||
results = []
|
||||
all_valid = True
|
||||
|
||||
for obj in doc.Objects:
|
||||
if not hasattr(obj, "TypeId"):
|
||||
continue
|
||||
node_type = classify_type(obj.TypeId)
|
||||
if node_type is None:
|
||||
continue
|
||||
|
||||
state = "clean"
|
||||
message = None
|
||||
if hasattr(obj, "isValid") and not obj.isValid():
|
||||
state = "failed"
|
||||
message = f"Feature {obj.Label} failed to recompute"
|
||||
all_valid = False
|
||||
|
||||
results.append(
|
||||
{
|
||||
"node_key": obj.Name,
|
||||
"state": state,
|
||||
"message": message,
|
||||
"properties_hash": compute_properties_hash(obj),
|
||||
}
|
||||
)
|
||||
|
||||
with open(output_path, "w") as f:
|
||||
json.dump({"valid": all_valid, "nodes": results}, f)
|
||||
|
||||
status = "PASS" if all_valid else "FAIL"
|
||||
FreeCAD.Console.PrintMessage(
|
||||
f"Validation {status}: {len(results)} nodes -> {output_path}\n"
|
||||
)
|
||||
finally:
|
||||
FreeCAD.closeDocument(doc.Name)
|
||||
|
||||
|
||||
def export(input_path, output_path, format="step"):
|
||||
"""Export a Create file to an external geometry format.
|
||||
|
||||
Parameters
|
||||
----------
|
||||
input_path : str
|
||||
Path to the ``.kc`` or ``.FCStd`` file.
|
||||
output_path : str
|
||||
Path to write the exported file.
|
||||
format : str
|
||||
One of ``step``, ``iges``, ``stl``, ``obj``.
|
||||
"""
|
||||
import Part
|
||||
|
||||
doc = FreeCAD.openDocument(input_path)
|
||||
try:
|
||||
shapes = [
|
||||
obj.Shape for obj in doc.Objects if hasattr(obj, "Shape") and obj.Shape
|
||||
]
|
||||
if not shapes:
|
||||
raise ValueError("No geometry found in document")
|
||||
|
||||
compound = Part.makeCompound(shapes)
|
||||
|
||||
format_lower = format.lower()
|
||||
if format_lower == "step":
|
||||
compound.exportStep(output_path)
|
||||
elif format_lower == "iges":
|
||||
compound.exportIges(output_path)
|
||||
elif format_lower == "stl":
|
||||
import Mesh
|
||||
|
||||
Mesh.export([compound], output_path)
|
||||
elif format_lower == "obj":
|
||||
import Mesh
|
||||
|
||||
Mesh.export([compound], output_path)
|
||||
else:
|
||||
raise ValueError(f"Unsupported format: {format}")
|
||||
|
||||
FreeCAD.Console.PrintMessage(
|
||||
f"Exported {format_lower.upper()} -> {output_path}\n"
|
||||
)
|
||||
finally:
|
||||
FreeCAD.closeDocument(doc.Name)
|
||||
@@ -717,6 +717,28 @@ class Silo_New:
|
||||
return _server_mode == "normal"
|
||||
|
||||
|
||||
def _push_dag_after_upload(doc, part_number, revision_number):
|
||||
"""Extract and push the feature DAG after a successful upload.
|
||||
|
||||
Failures are logged as warnings -- DAG sync must never block save.
|
||||
"""
|
||||
try:
|
||||
from dag import extract_dag
|
||||
|
||||
nodes, edges = extract_dag(doc)
|
||||
if not nodes:
|
||||
return
|
||||
|
||||
result = _client.push_dag(part_number, revision_number, nodes, edges)
|
||||
node_count = result.get("node_count", len(nodes))
|
||||
edge_count = result.get("edge_count", len(edges))
|
||||
FreeCAD.Console.PrintMessage(
|
||||
f"DAG synced: {node_count} nodes, {edge_count} edges\n"
|
||||
)
|
||||
except Exception as e:
|
||||
FreeCAD.Console.PrintWarning(f"DAG sync failed: {e}\n")
|
||||
|
||||
|
||||
class Silo_Save:
|
||||
"""Save locally and upload to MinIO."""
|
||||
|
||||
@@ -787,6 +809,8 @@ class Silo_Save:
|
||||
new_rev = result["revision_number"]
|
||||
FreeCAD.Console.PrintMessage(f"Uploaded as revision {new_rev}\n")
|
||||
|
||||
_push_dag_after_upload(doc, part_number, new_rev)
|
||||
|
||||
except Exception as e:
|
||||
FreeCAD.Console.PrintWarning(f"Upload failed: {e}\n")
|
||||
FreeCAD.Console.PrintMessage("File saved locally but not uploaded.\n")
|
||||
@@ -841,6 +865,8 @@ class Silo_Commit:
|
||||
new_rev = result["revision_number"]
|
||||
FreeCAD.Console.PrintMessage(f"Committed revision {new_rev}: {comment}\n")
|
||||
|
||||
_push_dag_after_upload(doc, part_number, new_rev)
|
||||
|
||||
except Exception as e:
|
||||
FreeCAD.Console.PrintError(f"Commit failed: {e}\n")
|
||||
|
||||
@@ -2312,6 +2338,18 @@ class SiloEventListener(QtCore.QThread):
|
||||
) # (status, retry_count, error_message)
|
||||
server_mode_changed = QtCore.Signal(str) # "normal" / "read-only" / "degraded"
|
||||
|
||||
# DAG events
|
||||
dag_updated = QtCore.Signal(str, int, int) # part_number, node_count, edge_count
|
||||
dag_validated = QtCore.Signal(str, bool, int) # part_number, valid, failed_count
|
||||
|
||||
# Job lifecycle events
|
||||
job_created = QtCore.Signal(str, str, str) # job_id, definition_name, part_number
|
||||
job_claimed = QtCore.Signal(str, str) # job_id, runner_id
|
||||
job_progress = QtCore.Signal(str, int, str) # job_id, progress, message
|
||||
job_completed = QtCore.Signal(str) # job_id
|
||||
job_failed = QtCore.Signal(str, str) # job_id, error
|
||||
job_cancelled = QtCore.Signal(str) # job_id
|
||||
|
||||
_MAX_RETRIES = 10
|
||||
_BASE_DELAY = 1 # seconds, doubles each retry
|
||||
_MAX_DELAY = 60 # seconds, backoff cap
|
||||
@@ -2428,6 +2466,35 @@ class SiloEventListener(QtCore.QThread):
|
||||
self.server_mode_changed.emit(mode)
|
||||
return
|
||||
|
||||
# Job lifecycle events (keyed by job_id, not part_number)
|
||||
job_id = payload.get("job_id", "")
|
||||
if event_type == "job.created":
|
||||
self.job_created.emit(
|
||||
job_id,
|
||||
payload.get("definition_name", ""),
|
||||
payload.get("part_number", ""),
|
||||
)
|
||||
return
|
||||
if event_type == "job.claimed":
|
||||
self.job_claimed.emit(job_id, payload.get("runner_id", ""))
|
||||
return
|
||||
if event_type == "job.progress":
|
||||
self.job_progress.emit(
|
||||
job_id,
|
||||
int(payload.get("progress", 0)),
|
||||
payload.get("message", ""),
|
||||
)
|
||||
return
|
||||
if event_type == "job.completed":
|
||||
self.job_completed.emit(job_id)
|
||||
return
|
||||
if event_type == "job.failed":
|
||||
self.job_failed.emit(job_id, payload.get("error", ""))
|
||||
return
|
||||
if event_type == "job.cancelled":
|
||||
self.job_cancelled.emit(job_id)
|
||||
return
|
||||
|
||||
pn = payload.get("part_number", "")
|
||||
if not pn:
|
||||
return
|
||||
@@ -2437,6 +2504,18 @@ class SiloEventListener(QtCore.QThread):
|
||||
elif event_type == "revision_created":
|
||||
rev = payload.get("revision", 0)
|
||||
self.revision_created.emit(pn, int(rev))
|
||||
elif event_type == "dag.updated":
|
||||
self.dag_updated.emit(
|
||||
pn,
|
||||
int(payload.get("node_count", 0)),
|
||||
int(payload.get("edge_count", 0)),
|
||||
)
|
||||
elif event_type == "dag.validated":
|
||||
self.dag_validated.emit(
|
||||
pn,
|
||||
bool(payload.get("valid", False)),
|
||||
int(payload.get("failed_count", 0)),
|
||||
)
|
||||
|
||||
|
||||
class _SSEUnsupported(Exception):
|
||||
@@ -2653,6 +2732,11 @@ class SiloAuthDockWidget:
|
||||
self._event_listener.revision_created.connect(self._on_remote_revision)
|
||||
self._event_listener.connection_status.connect(self._on_sse_status)
|
||||
self._event_listener.server_mode_changed.connect(self._on_server_mode)
|
||||
self._event_listener.dag_updated.connect(self._on_dag_updated)
|
||||
self._event_listener.dag_validated.connect(self._on_dag_validated)
|
||||
self._event_listener.job_created.connect(self._on_job_created)
|
||||
self._event_listener.job_completed.connect(self._on_job_completed)
|
||||
self._event_listener.job_failed.connect(self._on_job_failed)
|
||||
self._event_listener.start()
|
||||
else:
|
||||
if self._event_listener is not None and self._event_listener.isRunning():
|
||||
@@ -2732,6 +2816,70 @@ class SiloAuthDockWidget:
|
||||
)
|
||||
self._refresh_activity_panel()
|
||||
|
||||
def _on_dag_updated(self, part_number, node_count, edge_count):
|
||||
FreeCAD.Console.PrintMessage(
|
||||
f"Silo: DAG updated for {part_number}"
|
||||
f" ({node_count} nodes, {edge_count} edges)\n"
|
||||
)
|
||||
self._add_activity_entry(
|
||||
f"\u25b6 {part_number} \u2013 DAG synced"
|
||||
f" ({node_count} nodes, {edge_count} edges)",
|
||||
part_number,
|
||||
)
|
||||
|
||||
def _on_dag_validated(self, part_number, valid, failed_count):
|
||||
if valid:
|
||||
status = "\u2713 PASS"
|
||||
FreeCAD.Console.PrintMessage(f"Silo: Validation passed for {part_number}\n")
|
||||
else:
|
||||
status = f"\u2717 FAIL ({failed_count} failed)"
|
||||
FreeCAD.Console.PrintWarning(
|
||||
f"Silo: Validation failed for {part_number}"
|
||||
f" ({failed_count} features failed)\n"
|
||||
)
|
||||
self._add_activity_entry(f"{status} \u2013 {part_number}", part_number)
|
||||
|
||||
def _on_job_created(self, job_id, definition_name, part_number):
|
||||
FreeCAD.Console.PrintMessage(
|
||||
f"Silo: Job {definition_name} created for {part_number}\n"
|
||||
)
|
||||
self._add_activity_entry(
|
||||
f"\u23f3 {part_number} \u2013 {definition_name} queued",
|
||||
part_number,
|
||||
)
|
||||
|
||||
def _on_job_completed(self, job_id):
|
||||
FreeCAD.Console.PrintMessage(f"Silo: Job {job_id} completed\n")
|
||||
self._refresh_activity_panel()
|
||||
|
||||
def _on_job_failed(self, job_id, error):
|
||||
FreeCAD.Console.PrintError(f"Silo: Job {job_id} failed: {error}\n")
|
||||
self._add_activity_entry(f"\u2717 Job {job_id[:8]} failed: {error}", None)
|
||||
|
||||
def _add_activity_entry(self, text, part_number):
|
||||
"""Insert a live event entry at the top of the Activity panel."""
|
||||
from PySide import QtCore, QtGui, QtWidgets
|
||||
|
||||
mw = FreeCADGui.getMainWindow()
|
||||
if mw is None:
|
||||
return
|
||||
panel = mw.findChild(QtWidgets.QDockWidget, "SiloDatabaseActivity")
|
||||
if panel is None:
|
||||
return
|
||||
activity_list = panel.findChild(QtWidgets.QListWidget)
|
||||
if activity_list is None:
|
||||
return
|
||||
|
||||
item = QtWidgets.QListWidgetItem(text)
|
||||
if part_number:
|
||||
item.setData(QtCore.Qt.UserRole, part_number)
|
||||
item.setForeground(QtGui.QColor("#89b4fa"))
|
||||
activity_list.insertItem(0, item)
|
||||
|
||||
# Cap the list at 50 entries
|
||||
while activity_list.count() > 50:
|
||||
activity_list.takeItem(activity_list.count() - 1)
|
||||
|
||||
def _refresh_activity_panel(self):
|
||||
"""Refresh the Database Activity panel if it exists."""
|
||||
from PySide import QtCore, QtGui, QtWidgets
|
||||
|
||||
Reference in New Issue
Block a user