Datasets:
File size: 3,432 Bytes
8ca7ba0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | from __future__ import annotations
import json
import socket
import uuid
from pathlib import Path
from typing import Any, Literal
from pydantic import BaseModel, ConfigDict, Field
class BlenderWorkerResponse(BaseModel):
model_config = ConfigDict(extra="forbid")
protocol_version: Literal["1.0"] = "1.0"
job_id: str
status: Literal["passed", "failed", "shutting_down"]
jobs_completed: int = Field(ge=0)
elapsed_s: float = Field(ge=0.0)
report: str | None = None
error: str | None = None
def _endpoint(value: str) -> tuple[str, int]:
host, separator, raw_port = value.rpartition(":")
if not separator or not host:
raise ValueError("Blender resident endpoint must be host:port")
port = int(raw_port)
if not 1 <= port <= 65535:
raise ValueError("Blender resident endpoint port is invalid")
return host, port
def _request(endpoint: str, payload: dict[str, Any], *, timeout_s: float) -> dict[str, Any]:
host, port = _endpoint(endpoint)
encoded = (json.dumps(payload, separators=(",", ":")) + "\n").encode()
with socket.create_connection((host, port), timeout=timeout_s) as connection:
connection.settimeout(timeout_s)
connection.sendall(encoded)
chunks: list[bytes] = []
total = 0
while True:
chunk = connection.recv(65536)
if not chunk:
break
chunks.append(chunk)
total += len(chunk)
if total > 4 * 1024 * 1024:
raise RuntimeError("Blender resident response exceeded 4 MiB")
if b"\n" in chunk:
break
raw = b"".join(chunks).split(b"\n", 1)[0]
if not raw:
raise RuntimeError("Blender resident worker returned an empty response")
value = json.loads(raw)
if not isinstance(value, dict):
raise RuntimeError("Blender resident worker returned a non-object response")
return value
def submit_blender_job(
endpoint: str,
*,
manifest: Path,
output: Path,
mode: Literal["compose", "render-only"] = "compose",
timeout_s: float = 3600.0,
) -> BlenderWorkerResponse:
job_id = uuid.uuid4().hex
response = BlenderWorkerResponse.model_validate(
_request(
endpoint,
{
"protocol_version": "1.0",
"op": "compose",
"job_id": job_id,
"manifest": str(manifest.resolve()),
"output": str(output.resolve()),
"mode": mode,
},
timeout_s=timeout_s,
)
)
if response.job_id != job_id:
raise RuntimeError("Blender resident response job ID differs")
if response.status != "passed":
raise RuntimeError(f"Blender resident job failed: {response.error}")
return response
def shutdown_blender_worker(
endpoint: str, *, timeout_s: float = 30.0
) -> BlenderWorkerResponse:
job_id = uuid.uuid4().hex
response = BlenderWorkerResponse.model_validate(
_request(
endpoint,
{
"protocol_version": "1.0",
"op": "shutdown",
"job_id": job_id,
},
timeout_s=timeout_s,
)
)
if response.job_id != job_id or response.status != "shutting_down":
raise RuntimeError("Blender resident worker did not acknowledge shutdown")
return response
|