trustgraph/trustgraph-flow/trustgraph/gateway/command.py

83 lines
1.8 KiB
Python
Raw Normal View History

import asyncio
import queue
from pulsar.schema import JsonSchema
import uuid
from aiohttp import web, WSMsgType
from . socket import SocketEndpoint
2024-12-06 20:58:31 +00:00
from . text_completion import TextCompletionRequestor
class CommandEndpoint(SocketEndpoint):
def __init__(
2024-12-06 20:58:31 +00:00
self, pulsar_host, auth,
services,
path="/api/v1/command",
):
super(CommandEndpoint, self).__init__(
endpoint_path=path, auth=auth,
)
2024-12-06 21:21:27 +00:00
self.q = asyncio.Queue(maxsize=10)
2024-12-06 21:21:27 +00:00
self.services = services
2024-12-06 20:58:31 +00:00
async def start(self):
pass
async def async_thread(self, ws, running):
while running.get():
2024-12-06 21:21:27 +00:00
try:
svc, request = await asyncio.wait_for(self.q.get(), 1)
except TimeoutError:
continue
except Exception as e:
await ws.send_json({"error": str(e)})
try:
print(svc, request)
requestor = self.services[svc]
resp = await requestor.process(request)
await ws.send_json({ "response": resp })
except Exception as e:
await ws.send_json({"error": str(e)})
running.stop()
async def listener(self, ws, running):
async for msg in ws:
# On error, finish
if msg.type == WSMsgType.ERROR:
break
else:
try:
data = msg.json()
2024-12-06 21:21:27 +00:00
if data["service"] not in self.services:
raise RuntimeError("Bad service")
await self.q.put(
( data["service"], data["request"] )
)
except Exception as e:
2024-12-06 21:21:27 +00:00
await ws.send_json({"error": str(e)})
continue
running.stop()