Skip to content

Commit 3f7c896

Browse files
committed
added API call to register nodes
1 parent 0e894cc commit 3f7c896

1 file changed

Lines changed: 39 additions & 9 deletions

File tree

python-sdk/exospherehost/runtime.py

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ def __init__(self, namespace: str, name: str, state_manager_uri: str | None = No
5656
self._namespace = namespace
5757
self._key = key
5858
self._batch_size = batch_size
59-
self._connected = False
6059
self._state_queue = Queue(maxsize=2*batch_size)
6160
self._workers = workers
6261
self._nodes = []
@@ -104,8 +103,38 @@ def _get_executed_endpoint(self, state_id: str):
104103
def _get_errored_endpoint(self, state_id: str):
105104
"""Get the endpoint URL for notifying errored states."""
106105
return f"{self._state_manager_uri}/{str(self._state_manager_version)}/namespace/{self._namespace}/states/{state_id}/errored"
106+
107+
def _get_register_endpoint(self):
108+
"""Get the endpoint URL for registering nodes with runtime"""
109+
return f"{self._state_manager_uri}/{str(self._state_manager_version)}/namespace/{self._namespace}/nodes/register"
110+
111+
async def _register_nodes(self):
112+
"""Register nodes with the runtime"""
113+
async with ClientSession() as session:
114+
endpoint = self._get_register_endpoint()
115+
body = {
116+
"runtime_name": self._name,
117+
"runtime_namespace": self._namespace,
118+
"nodes": [
119+
{
120+
"name": node.get_unique_name(),
121+
"namespace": self._namespace,
122+
"inputs_schema": node.Inputs.model_json_schema(),
123+
"outputs_schema": node.Outputs.model_json_schema(),
124+
} for node in self._nodes
125+
]
126+
}
127+
128+
async with session.post(endpoint, json=body, headers=headers) as response: # type: ignore
129+
res = await response.json()
107130

108-
def connect(self, nodes: List[BaseNode]):
131+
if response.status != 200:
132+
logger.error(f"Failed to register nodes: {res}")
133+
134+
return res
135+
136+
137+
async def _register(self, nodes: List[BaseNode]):
109138
"""
110139
Connect nodes to the runtime.
111140
@@ -121,7 +150,9 @@ def connect(self, nodes: List[BaseNode]):
121150
self._nodes = self._validate_nodes(nodes)
122151
self._node_names = [node.get_unique_name() for node in nodes]
123152
self._node_mapping = {node.get_unique_name(): node for node in self._nodes}
124-
self._connected = True
153+
154+
await self._register_nodes()
155+
125156

126157
async def _enqueue_call(self):
127158
"""
@@ -250,7 +281,7 @@ async def _worker(self):
250281

251282
self._state_queue.task_done() # type: ignore
252283

253-
async def _start(self):
284+
async def _start(self, nodes: List[BaseNode]):
254285
"""
255286
Start the runtime execution.
256287
@@ -260,15 +291,14 @@ async def _start(self):
260291
Raises:
261292
RuntimeError: If the runtime is not connected (no nodes registered)
262293
"""
263-
if not self._connected:
264-
raise RuntimeError("Runtime not connected, you need to call Runtime.connect() before calling Runtime.start()")
294+
await self._register(nodes)
265295

266296
poller = asyncio.create_task(self._enqueue())
267297
worker_tasks = [asyncio.create_task(self._worker()) for _ in range(self._workers)]
268298

269299
await asyncio.gather(poller, *worker_tasks)
270300

271-
def start(self):
301+
def start(self, nodes: List[BaseNode]):
272302
"""
273303
Start the runtime execution.
274304
@@ -281,6 +311,6 @@ def start(self):
281311
"""
282312
try:
283313
loop = asyncio.get_running_loop()
284-
return loop.create_task(self._start())
314+
return loop.create_task(self._start(nodes))
285315
except RuntimeError:
286-
asyncio.run(self._start())
316+
asyncio.run(self._start(nodes))

0 commit comments

Comments
 (0)