forked from UiPath/uipath-dev-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanager.py
More file actions
174 lines (147 loc) · 6.52 KB
/
Copy pathmanager.py
File metadata and controls
174 lines (147 loc) · 6.52 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
"""WebSocket connection manager with run-level subscriptions."""
from __future__ import annotations
import asyncio
import logging
from typing import Any
from fastapi import WebSocket
from uipath.dev.models.data import (
ChatData,
InterruptData,
LogData,
StateData,
TraceData,
)
from uipath.dev.models.execution import ExecutionRun
from uipath.dev.server.serializers import (
serialize_chat,
serialize_interrupt,
serialize_log,
serialize_run,
serialize_state,
serialize_trace,
)
from uipath.dev.server.ws.protocol import ServerEvent, server_message
logger = logging.getLogger(__name__)
_SENTINEL: dict[str, Any] = {} # Unique object used to signal queue shutdown
class ConnectionManager:
"""Manages WebSocket connections and run-level subscriptions."""
def __init__(self) -> None:
"""Initialize the connection manager."""
self._connections: set[WebSocket] = set()
self._subscriptions: dict[str, set[WebSocket]] = {}
self._queues: dict[int, asyncio.Queue[dict[str, Any]]] = {}
self._send_tasks: dict[int, asyncio.Task[None]] = {}
self._loop: asyncio.AbstractEventLoop | None = None
def _get_loop(self) -> asyncio.AbstractEventLoop:
"""Get or cache the running event loop."""
if self._loop is None or self._loop.is_closed():
try:
self._loop = asyncio.get_running_loop()
except RuntimeError:
self._loop = asyncio.get_event_loop()
return self._loop
async def connect(self, websocket: WebSocket) -> None:
"""Accept a new WebSocket connection."""
await websocket.accept()
self._connections.add(websocket)
queue: asyncio.Queue[dict[str, Any]] = asyncio.Queue()
ws_id = id(websocket)
self._queues[ws_id] = queue
self._send_tasks[ws_id] = asyncio.create_task(self._sender(websocket, queue))
async def _sender(
self, ws: WebSocket, queue: asyncio.Queue[dict[str, Any]]
) -> None:
"""Consume messages from queue and send them serially."""
while True:
message = await queue.get()
if message is _SENTINEL:
break
try:
await ws.send_json(message)
except Exception:
self.disconnect(ws)
break
def _enqueue(self, ws: WebSocket, message: dict[str, Any]) -> None:
"""Put a message on a WebSocket's send queue (non-blocking)."""
queue = self._queues.get(id(ws))
if queue is not None:
queue.put_nowait(message)
def disconnect(self, websocket: WebSocket) -> None:
"""Remove a WebSocket connection and all its subscriptions."""
self._connections.discard(websocket)
for run_id in list(self._subscriptions):
self._subscriptions[run_id].discard(websocket)
if not self._subscriptions[run_id]:
del self._subscriptions[run_id]
ws_id = id(websocket)
queue = self._queues.pop(ws_id, None)
if queue is not None:
try:
queue.put_nowait(_SENTINEL)
except Exception:
pass
task = self._send_tasks.pop(ws_id, None)
if task is not None and not task.done():
task.cancel()
async def disconnect_all(self) -> None:
"""Close all WebSocket connections gracefully."""
for ws in list(self._connections):
try:
await ws.close()
except Exception as e:
logger.debug(f"Error closing WebSocket: {e}")
self.disconnect(ws)
self._connections.clear()
self._subscriptions.clear()
def subscribe(self, websocket: WebSocket, run_id: str) -> None:
"""Subscribe a WebSocket to a run's events."""
if run_id not in self._subscriptions:
self._subscriptions[run_id] = set()
self._subscriptions[run_id].add(websocket)
def unsubscribe(self, websocket: WebSocket, run_id: str) -> None:
"""Unsubscribe a WebSocket from a run's events."""
if run_id in self._subscriptions:
self._subscriptions[run_id].discard(websocket)
if not self._subscriptions[run_id]:
del self._subscriptions[run_id]
def remove_run_subscriptions(self, run_id: str) -> None:
"""Remove all subscriptions for a run."""
self._subscriptions.pop(run_id, None)
def broadcast_run_updated(self, run: ExecutionRun) -> None:
"""Broadcast a run update to all subscribers (safe from sync context)."""
msg = server_message(ServerEvent.RUN_UPDATED, serialize_run(run))
self._schedule_broadcast(run.id, msg)
def broadcast_log(self, log_data: LogData) -> None:
"""Broadcast a log entry to run subscribers."""
msg = server_message(ServerEvent.LOG, serialize_log(log_data))
self._schedule_broadcast(log_data.run_id, msg)
def broadcast_trace(self, trace_data: TraceData) -> None:
"""Broadcast a trace span to run subscribers."""
msg = server_message(ServerEvent.TRACE, serialize_trace(trace_data))
self._schedule_broadcast(trace_data.run_id, msg)
def broadcast_chat(self, chat_data: ChatData) -> None:
"""Broadcast a chat message to run subscribers."""
msg = server_message(ServerEvent.CHAT, serialize_chat(chat_data))
self._schedule_broadcast(chat_data.run_id, msg)
def broadcast_interrupt(self, interrupt_data: InterruptData) -> None:
"""Broadcast a chat interrupt to run subscribers."""
msg = server_message(
ServerEvent.CHAT_INTERRUPT, serialize_interrupt(interrupt_data)
)
self._schedule_broadcast(interrupt_data.run_id, msg)
def broadcast_state(self, state_data: StateData) -> None:
"""Broadcast a state transition to run subscribers."""
msg = server_message(ServerEvent.STATE, serialize_state(state_data))
self._schedule_broadcast(state_data.run_id, msg)
def broadcast_reload(self, changed_files: list[str]) -> None:
"""Broadcast a reload event to all connected clients."""
msg = server_message(ServerEvent.RELOAD, {"files": changed_files})
for ws in self._connections:
self._enqueue(ws, msg)
def _schedule_broadcast(self, run_id: str, message: dict[str, Any]) -> None:
"""Enqueue a message for all subscribers of a run."""
subscribers = self._subscriptions.get(run_id)
if not subscribers:
return
for ws in subscribers:
self._enqueue(ws, message)