Skip to content

armgit / SOMA

Public Imported from GitHub
0

Desktop app for designing parametric 3D models in CadQuery together with the coding agent of your choice

soma: Sperre gegen gleichzeitige Builds, Messdienst meldet neueren Build

Kai-Jungsthoefel7a136f261a53
diff --git a/python/soma/build.py b/python/soma/build.py
index bb929af..74dc88b 100644
--- a/python/soma/build.py
+++ b/python/soma/build.py
@@ -3,6 +3,7 @@
Einmal-Prozess, funktioniert ohne App (E24). Jeder Lauf schreibt build.json, auch bei Fehlern, damit App und
Agent dieselbe strukturierte Rueckmeldung bekommen (Status, Fehlerort, Warnungen).
"""
+import contextlib
import json
import os
import runpy
@@ -22,6 +23,9 @@ DEFAULT_ENTRY = "model.py"
# das der Viewer braucht: dann baut die App jedes Projekt beim naechsten Oeffnen einmal neu.
OUTPUT_FORMAT = 1
REFERENCE_GROUP = "Reference" # Baugruppe der Referenzteile in output/model.step
+# So lange wartet ein Build auf einen anderen desselben Modells. Unter dem Zeitlimit der App (10 min, app/src/main/build.ts),
+# damit sie eine verstaendliche Meldung bekommt statt eines abgebrochenen Prozesses
+LOCK_WAIT_S = float(os.environ.get("SOMA_BUILD_LOCK_WAIT_S") or 540)
class BuildError(Exception):
@@ -32,6 +36,93 @@ class BuildError(Exception):
self.message, self.file, self.line, self.kind, self.tb = message, file, line, kind, tb
+if os.name == "nt":
+ import msvcrt
+
+ def _try_lock(f):
+ f.seek(0)
+ try:
+ msvcrt.locking(f.fileno(), msvcrt.LK_NBLCK, 1)
+ return True
+ except OSError:
+ return False
+
+ def _unlock(f):
+ f.seek(0)
+ try:
+ msvcrt.locking(f.fileno(), msvcrt.LK_UNLCK, 1)
+ except OSError:
+ pass
+else:
+ import fcntl
+
+ def _try_lock(f):
+ try:
+ fcntl.flock(f.fileno(), fcntl.LOCK_EX | fcntl.LOCK_NB)
+ return True
+ except OSError:
+ return False
+
+ def _unlock(f):
+ try:
+ fcntl.flock(f.fileno(), fcntl.LOCK_UN)
+ except OSError:
+ pass
+
+
+def build_lock(root, wait=None):
+ """Nur ein Build je Modell zur selben Zeit. Die App baut nach jeder Aenderung, der Agent ruft danach `soma build` im
+ Terminal: Gleichzeitig schrieben beide in dieselben Dateien, und model.map.json konnte auf ein BREP zeigen, das der
+ andere Build schon geloescht hatte. Die Sperre gehoert dem Betriebssystem und endet mit dem Prozess, auch nach einem
+ Absturz bleibt also nichts haengen. Laesst sich die Sperrdatei nicht anlegen, wird ohne Sperre gebaut wie bisher."""
+ wait = LOCK_WAIT_S if wait is None else wait
+ geo = Path(root) / ".soma" / "geometry" # in .gitignore und nie in der ZIP
+ try:
+ geo.mkdir(parents=True, exist_ok=True)
+ f = open(geo / "build.lock", "a+b")
+ except OSError:
+ yield
+ return
+ try:
+ t0 = time.monotonic()
+ told = False
+ while not _try_lock(f):
+ if not told:
+ print("Waiting for another build of this model to finish (the app or another terminal) ...", file=sys.stderr, flush=True)
+ told = True
+ if time.monotonic() - t0 > wait:
+ raise BuildError(f"Another build of this model has been running for more than {wait:.0f} s (the app or "
+ "soma build in a terminal). Build again once it has finished.", kind="BuildBusy")
+ time.sleep(0.2)
+ try:
+ yield
+ finally:
+ _unlock(f)
+ finally:
+ f.close()
+
+
+def write_atomic(path, write, tmp_dir):
+ """Datei ueber eine Zwischendatei ersetzen: Wer sie gerade liest (Viewer, Messdienst), sieht nie eine halbe.
+ `write(tmp)` schreibt den Inhalt. Haelt jemand die Datei so offen, dass Windows das Ersetzen verweigert (Python oeffnet
+ ohne FILE_SHARE_DELETE, ebenso Virenscanner), kurz nochmal versuchen, zuletzt direkt schreiben wie frueher."""
+ path, tmp = Path(path), Path(tmp_dir) / f"{uuid.uuid4().hex}.{Path(path).name}.tmp"
+ result = write(tmp)
+ for _ in range(30):
+ try:
+ os.replace(tmp, path)
+ return result
+ except PermissionError:
+ time.sleep(0.1)
+ result = write(path)
+ try:
+ tmp.unlink()
+ except OSError:
+ pass
+ return result
+
+
def read_project_json(root):
p = root / ".soma" / "project.json"
try:
@@ -185,16 +276,28 @@ def write_outputs(root, parts, project_name):
if p.get("axis"):
meta["axis"] = list(p["axis"])
glb_parts.append(dict(meta=meta, mesh=mesher.mesh_shape(p["shape"], tol, 0.3), color=p["color"]))
- glb_size = mesher.write_glb(root / ".soma" / "model.glb", glb_parts)
- (root / ".soma" / "model.map.json").write_text(
- json.dumps(dict(version=0, build=build_id, parts=map_parts), indent=1, ensure_ascii=False), encoding="utf-8")
+ soma, geo = root / ".soma", root / ".soma" / "geometry"
+ glb_size = write_atomic(soma / "model.glb", lambda p: mesher.write_glb(p, glb_parts), geo)
+ text = json.dumps(dict(version=0, build=build_id, parts=map_parts), indent=1, ensure_ascii=False)
+ write_atomic(soma / "model.map.json", lambda p: p.write_text(text, encoding="utf-8"), geo)
+ measure.remove_old_geometry(soma, build_id) # erst jetzt: model.map.json zeigt auf den neuen Stand
timings["glb"] = time.perf_counter() - t
return dict(glb_bytes=glb_size, tolerance=round(tol, 4), timings=timings, map_parts=map_parts)
def run_build(project_dir):
- """Kompletter Build. Schreibt .soma/build.json und gibt das Ergebnis-Dict zurueck (status ok/error)."""
+ """Kompletter Build. Schreibt .soma/build.json und gibt das Ergebnis-Dict zurueck (status ok/error). Laeuft schon ein
+ Build dieses Modells, wartet er darauf (build_lock) und liest den Code erst danach, baut also den neuesten Stand."""
root = Path(project_dir).resolve()
+ try:
+ with build_lock(root):
+ return _run_build(root)
+ except BuildError as e: # nur die Sperre. build.json bleibt die des anderen Builds, der noch laeuft
+ return dict(status="error", format=OUTPUT_FORMAT, warnings=[], timings={},
+ error=dict(kind=e.kind, message=e.message, file=None, line=None, traceback=""))
+
+
+def _run_build(root):
started = time.time()
timings = {}
result = dict(status="ok", format=OUTPUT_FORMAT, started=started, warnings=[])
@@ -228,8 +331,10 @@ def run_build(project_dir):
result["timings"] = {k: round(v, 3) for k, v in timings.items()}
result["finished"] = time.time()
try:
- (root / ".soma").mkdir(exist_ok=True)
- (root / ".soma" / "build.json").write_text(json.dumps(result, indent=1, ensure_ascii=False), encoding="utf-8")
+ geo = root / ".soma" / "geometry"
+ geo.mkdir(parents=True, exist_ok=True)
+ text = json.dumps(result, indent=1, ensure_ascii=False)
+ write_atomic(root / ".soma" / "build.json", lambda p: p.write_text(text, encoding="utf-8"), geo)
except OSError as e:
result["warnings"].append(f"build.json could not be written: {e}")
return result
diff --git a/python/soma/measure.py b/python/soma/measure.py
index 3b3c043..7e56c24 100644
--- a/python/soma/measure.py
+++ b/python/soma/measure.py
@@ -24,20 +24,27 @@ PARALLEL = 1e-6 # Winkeltoleranz (rad) fuer "parallel"
class MeasureError(Exception):
- """Fehler mit verstaendlicher Meldung. `stale`: die Anfrage gehoert zu einem aelteren Build."""
+ """Fehler mit verstaendlicher Meldung. `stale`: die Anfrage gehoert zu einem aelteren Build. `rebuilt`: auf der Platte
+ liegt ein neuerer Build als im Viewer (etwa `soma build` im Terminal), die App muss ihn erst laden."""
- def __init__(self, message, stale=False):
+ def __init__(self, message, stale=False, rebuilt=False):
super().__init__(message)
- self.stale = stale
+ self.stale = stale or rebuilt
+ self.rebuilt = rebuilt
def write_geometry(folder, shapes, build_id):
- """Legt die Bauteile als ein BREP ab (Reihenfolge = Reihenfolge in model.map.json) und raeumt alte Staende weg."""
+ """Legt die Bauteile als ein BREP ab (Reihenfolge = Reihenfolge in model.map.json). Alte Staende raeumt erst
+ remove_old_geometry weg, nachdem model.map.json auf den neuen zeigt: Bis dahin misst der Dienst noch den alten."""
geo = Path(folder) / "geometry"
geo.mkdir(parents=True, exist_ok=True)
cq.Compound.makeCompound(list(shapes)).exportBrep(str(geo / f"{build_id}.brep"))
- for old in geo.glob("*.brep"):
- if old.stem != build_id:
+
+
+def remove_old_geometry(folder, build_id):
+ """Alles ausser dem Stand `build_id` aus geometry/ entfernen, auch Zwischendateien abgebrochener Builds."""
+ for old in (Path(folder) / "geometry").glob("*"):
+ if old.suffix in (".brep", ".tmp") and not old.name.startswith(build_id):
try:
old.unlink()
except OSError:
@@ -279,7 +286,7 @@ def measure(model, req):
def serve(stdin=sys.stdin, stdout=sys.stdout):
- """Anfragen als JSON-Zeilen: {"id", "folder", "build", "a", "b"?}. Antwort: {"id", "ok", "result"|"error", "stale"?}.
+ """Anfragen als JSON-Zeilen: {"id", "folder", "build", "a", "b"?}. Antwort: {"id", "ok", "result"|"error", "stale"?, "rebuilt"?}.
Geladene Staende bleiben im Speicher, bis sich die Kennung in model.map.json aendert. {"prepare": true} statt "a" laedt
nur den Stand (bei grossen Baugruppen dauert das), damit der erste Klick nicht darauf warten muss."""
models = {}
@@ -296,7 +303,7 @@ def serve(stdin=sys.stdin, stdout=sys.stdout):
folder = str(req["folder"])
current = json.loads((Path(folder) / "model.map.json").read_text(encoding="utf-8")).get("build")
if req.get("build") and req["build"] != current:
- raise MeasureError("The model has been rebuilt in the meantime. Please click again.", stale=True)
+ raise MeasureError("The model has been rebuilt in the meantime. Please click again.", rebuilt=True)
m = models.get(folder)
if m is None or m.build != current:
m = models[folder] = Model(folder)
@@ -305,7 +312,7 @@ def serve(stdin=sys.stdin, stdout=sys.stdout):
else:
out = dict(id=rid, ok=True, result=measure(m, req))
except MeasureError as e:
- out = dict(id=rid, ok=False, error=str(e), stale=e.stale)
+ out = dict(id=rid, ok=False, error=str(e), stale=e.stale, rebuilt=e.rebuilt)
except Exception as e: # nie abstuerzen: der Dienst bedient auch die naechste Anfrage
out = dict(id=rid, ok=False, error=f"{type(e).__name__}: {e}")
stdout.write(json.dumps(out) + "\n")
diff --git a/python/soma/preview.py b/python/soma/preview.py
index 0c413f5..0b72dd6 100644
--- a/python/soma/preview.py
+++ b/python/soma/preview.py
@@ -52,4 +52,5 @@ def step_to_glb(step_path, out_dir):
mesh=mesher.mesh_shape(shape, tol, 0.3), color=rgba))
mesher.write_glb(out / "model.glb", glb_parts)
(out / "model.map.json").write_text(json.dumps(dict(version=0, build=build_id, parts=map_parts), indent=1, ensure_ascii=False), encoding="utf-8")
+ measure.remove_old_geometry(out, build_id)
return map_parts