Preserve installed operator workspace before consolidation
Local preservation snapshot, not a new deployment or simulation acceptance.
This commit is contained in:
@@ -9,6 +9,15 @@ from pathlib import Path
|
||||
from urllib.parse import urlsplit
|
||||
|
||||
PREFIX = "/api/v1/ai-polygon"
|
||||
ASSET_BLOCK_BYTES = 256 * 1024
|
||||
ASSET_READ_TIMEOUT_SECONDS = 5
|
||||
|
||||
|
||||
class CoreRequestRejected(RuntimeError):
|
||||
def __init__(self, status, detail):
|
||||
self.status = status
|
||||
self.detail = detail
|
||||
super().__init__("Core rejected simulation operation: " + str(status))
|
||||
|
||||
|
||||
class CoreClient:
|
||||
@@ -55,8 +64,14 @@ class CoreClient:
|
||||
)
|
||||
response = connection.getresponse()
|
||||
raw = response.read(4 * 1024**2 + 1)
|
||||
if response.status not in (200, 201) or len(raw) > 4 * 1024**2:
|
||||
raise RuntimeError("Core rejected simulation operation: " + str(response.status))
|
||||
if len(raw) > 4 * 1024**2:
|
||||
raise RuntimeError("Core response exceeds admitted size")
|
||||
if response.status not in (200, 201):
|
||||
try:
|
||||
detail = json.loads(raw).get("detail")
|
||||
except (ValueError, AttributeError):
|
||||
detail = None
|
||||
raise CoreRequestRejected(response.status, detail)
|
||||
self._connections.last_used = time.monotonic()
|
||||
return json.loads(raw)
|
||||
except Exception:
|
||||
@@ -65,18 +80,39 @@ class CoreClient:
|
||||
# Never retry an uncertain mutation automatically.
|
||||
raise
|
||||
|
||||
def download(self, world: dict, target: Path):
|
||||
def download(self, world: dict, target: Path, pulse=None):
|
||||
if pulse:
|
||||
pulse()
|
||||
if target.exists():
|
||||
digest = hashlib.sha256()
|
||||
with target.open("rb") as stream:
|
||||
if hashlib.file_digest(stream, "sha256").hexdigest() == world["sha256"]:
|
||||
while block := stream.read(ASSET_BLOCK_BYTES):
|
||||
digest.update(block)
|
||||
if pulse:
|
||||
pulse()
|
||||
if digest.hexdigest() == world["sha256"]:
|
||||
return
|
||||
if world.get("storage", {}).get("kind") == "worker":
|
||||
raise RuntimeError("The admitted Worker-local scene is missing or changed")
|
||||
connection = http.client.HTTPConnection("127.0.0.1", self.port, timeout=30)
|
||||
self.download_asset(
|
||||
PREFIX + "/worlds/" + world["world_id"] + "/source.ply", world, target, pulse=pulse
|
||||
)
|
||||
|
||||
def download_asset(self, path: str, descriptor: dict, target: Path, pulse=None):
|
||||
if not path.startswith("/api/v1/") or ".." in path or "\\" in path:
|
||||
raise ValueError("Invalid Core asset path")
|
||||
world = descriptor
|
||||
connection = http.client.HTTPConnection(
|
||||
"127.0.0.1", self.port, timeout=ASSET_READ_TIMEOUT_SECONDS
|
||||
)
|
||||
temporary = target.with_suffix(".part")
|
||||
try:
|
||||
connection.request("GET", PREFIX + "/worlds/" + world["world_id"] + "/source.ply")
|
||||
if pulse:
|
||||
pulse()
|
||||
connection.request("GET", path)
|
||||
response = connection.getresponse()
|
||||
if pulse:
|
||||
pulse()
|
||||
if (
|
||||
response.status != 200
|
||||
or int(response.getheader("Content-Length") or 0) != world["byte_length"]
|
||||
@@ -84,7 +120,12 @@ class CoreClient:
|
||||
raise RuntimeError("Scene transfer contract changed")
|
||||
digest, size = hashlib.sha256(), 0
|
||||
with temporary.open("wb") as stream:
|
||||
while block := response.read(4 * 1024**2):
|
||||
# read() may wait for a whole multi-MB block on a slow but live
|
||||
# stream. read1() returns the next available bounded socket read
|
||||
# so control/lease pulses remain independent of transfer size.
|
||||
while block := response.read1(ASSET_BLOCK_BYTES):
|
||||
if pulse:
|
||||
pulse()
|
||||
size += len(block)
|
||||
if size > world["byte_length"]:
|
||||
raise RuntimeError("Scene transfer exceeds admitted size")
|
||||
|
||||
Reference in New Issue
Block a user