-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f86dd5f
commit 9d7e81f
Showing
4 changed files
with
60 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,11 +9,11 @@ | |
Developers: | ||
- Erez Zomer ([email protected]) - 2023 | ||
""" | ||
from aiohttp import web, WSMsgType | ||
from logging import Logger | ||
import uuid | ||
import asyncio | ||
import uuid | ||
from logging import Logger | ||
|
||
from aiohttp import WSMsgType, web | ||
from movai_core_shared.logger import Log | ||
from movai_core_shared.messages.log_data import LogRequest | ||
|
||
|
@@ -22,6 +22,7 @@ | |
|
||
QUEUE_SIZE = 100000 | ||
|
||
|
||
class LogClient: | ||
def __init__(self, logger: Logger = None) -> None: | ||
self._id = uuid.uuid4() | ||
|
@@ -82,7 +83,7 @@ async def send_msg(self, request: LogRequest): | |
try: | ||
log_msg = request.get_client_log_format() | ||
await self._ws.send_json(log_msg) | ||
except (ValueError ,RuntimeError, TypeError) as err: | ||
except (ValueError, RuntimeError, TypeError) as err: | ||
self.logger.error(err.__str__()) | ||
|
||
async def stream_msgs(self): | ||
|
@@ -94,17 +95,18 @@ async def stream_msgs(self): | |
await self.send_msg(msg) | ||
|
||
async def listen_to_client_msgs(self): | ||
"""listens for client msgs and repond if necessary. | ||
""" | ||
"""listens for client msgs and repond if necessary.""" | ||
self._validate_socket() | ||
async for msg in self._ws: | ||
if msg.type == WSMsgType.TEXT: | ||
if msg.data == 'close': | ||
self._logger.debug("closing the websocket connection for client id: {self._id}") | ||
if msg.data == "close": | ||
self._logger.debug( | ||
"closing the websocket connection for client id: {self._id}" | ||
) | ||
await self._ws.close() | ||
elif msg.type == WSMsgType.ERROR: | ||
self._logger.error(f"ws connection closed with exception {ws.exception()}") | ||
self._logger.error(f"ws connection closed with exception {ws.exception()}") | ||
|
||
async def run(self, request: web.Request): | ||
"""Runs the client object in oreder to stream logs from backed to client. | ||
|
@@ -141,4 +143,4 @@ def _validate_socket(self): | |
if self._ws is None: | ||
raise TypeError("The websocket is not initialized for client {self._id}") | ||
if not self.is_alive(): | ||
raise ConnectionError(f"The websocket for client {self.id} is closed!") | ||
raise ConnectionError(f"The websocket for client {self.id} is closed!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,9 +9,9 @@ | |
Developers: | ||
- Erez Zomer ([email protected]) - 2023 | ||
""" | ||
from aiohttp import web | ||
import uuid | ||
|
||
from aiohttp import web | ||
from movai_core_shared.core.zmq_server import ZMQServer | ||
from movai_core_shared.envvars import LOG_STREAMER_BIND_ADDR | ||
from movai_core_shared.logger import Log | ||
|
@@ -22,14 +22,15 @@ | |
|
||
|
||
class LogsStreamer(ZMQServer): | ||
|
||
def __init__(self, debug: bool = False) -> None: | ||
"""Initializes the object. | ||
Args: | ||
debug (bool, optional): if True, will show debug logs while running ZMQServer | ||
""" | ||
super().__init__(self.__class__.__name__, LOG_STREAMER_BIND_ADDR, new_loop=False, debug=debug) | ||
super().__init__( | ||
self.__class__.__name__, LOG_STREAMER_BIND_ADDR, new_loop=False, debug=debug | ||
) | ||
self._logger = Log.get_logger(self.__class__.__name__) | ||
self._clients = {} | ||
|
||
|
@@ -43,7 +44,7 @@ def is_client_registered(self, client_id: uuid.UUID) -> bool: | |
bool: True if registered, False otherwise. | ||
""" | ||
return client_id in self._clients | ||
|
||
def register_client(self, client: LogClient) -> uuid.UUID: | ||
"""Register the client in the LogStreamer, so whenever a new log will arive it | ||
will be sent to this client if it pass the filter. | ||
|
@@ -52,7 +53,9 @@ def register_client(self, client: LogClient) -> uuid.UUID: | |
client (LogClient): the client to register. | ||
""" | ||
if self.is_client_registered(client.id): | ||
self._logger.debug(f"The client: {client.id} is already registered in {self.__class__.__name__}") | ||
self._logger.debug( | ||
f"The client: {client.id} is already registered in {self.__class__.__name__}" | ||
) | ||
return | ||
self._clients[client.id] = client | ||
self._logger.debug(f"The client: {client.id} has been added to {self.__class__.__name__}") | ||
|
@@ -81,21 +84,23 @@ async def handle_request(self, request: dict) -> dict: | |
try: | ||
log_msg = LogRequest(**request) | ||
if self._debug: | ||
self._logger.debug(f"{self.__class__.__name__}: {log_msg.req_data.log_fields.message}") | ||
self._logger.debug( | ||
f"{self.__class__.__name__}: {log_msg.req_data.log_fields.message}" | ||
) | ||
for client in self._clients.values(): | ||
if client.is_alive(): | ||
await client.push(log_msg) | ||
else: | ||
clients_to_remove.add(client) | ||
|
||
for client in clients_to_remove: | ||
self.unregister_client(client) | ||
|
||
return {} | ||
except Exception as error: | ||
self._logger.error(str(error)) | ||
return {} | ||
|
||
async def stream_logs(self, request: web.Request): | ||
"""Stream logs from arriving from message-server to the client. | ||
|
@@ -105,7 +110,7 @@ async def stream_logs(self, request: web.Request): | |
Returns: | ||
web.WebSocketResponse: The websocket response to the client. | ||
""" | ||
|
||
if not self._running: | ||
self.run() | ||
client = LogClient() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters