refactor monitor, add threshold rtesting
This commit is contained in:
+13
-10
@@ -8,6 +8,7 @@ import asyncio
|
||||
import json
|
||||
import logging
|
||||
from typing import Callable, Iterable, Optional
|
||||
from . import data
|
||||
|
||||
import websockets
|
||||
|
||||
@@ -16,7 +17,7 @@ logger.setLevel(logging.INFO)
|
||||
_connections = set()
|
||||
_loop: Optional[asyncio.AbstractEventLoop] = None
|
||||
_get_hosts: Optional[Callable[[], Iterable]] = None
|
||||
_get_msgs: Optional[Callable[[], Iterable]] = None
|
||||
#_get_msgs: Optional[Callable[[], Iterable]] = None
|
||||
_verbose = False
|
||||
|
||||
|
||||
@@ -38,11 +39,11 @@ async def _handler(websocket, path=None):
|
||||
except Exception as e:
|
||||
logger.error("Error sending initial hosts: %s", e, exc_info=True)
|
||||
# send recent messages
|
||||
if _get_msgs:
|
||||
if data.msgs:
|
||||
try:
|
||||
msgs = list(_get_msgs())[-100:]
|
||||
logger.debug("Sending %d recent messages to new WebSocket client", len(msgs))
|
||||
for m in msgs:
|
||||
# msgs = list(_get_msgs())[-100:]
|
||||
logger.debug("Sending %d recent messages to new WebSocket client", len(data.msgs))
|
||||
for m in data.msgs:
|
||||
jmsg = json.dumps({"type": "message", "data": m})
|
||||
await websocket.send(jmsg)
|
||||
except Exception as e:
|
||||
@@ -77,7 +78,7 @@ async def start(
|
||||
wss_port: Optional[int] = None,
|
||||
ssl_context=None,
|
||||
get_hosts: Optional[Callable] = None,
|
||||
get_msgs: Optional[Callable] = None,
|
||||
# get_msgs: Optional[Callable] = None,
|
||||
config: dict = {},
|
||||
):
|
||||
"""Start WebSocket servers and block until cancelled.
|
||||
@@ -86,17 +87,19 @@ async def start(
|
||||
If `wss_port` and `ssl_context` are provided, a WSS server will also be
|
||||
started.
|
||||
"""
|
||||
global _loop, _get_hosts, _get_msgs, _verbose
|
||||
global _loop, _get_hosts, _verbose
|
||||
_loop = asyncio.get_running_loop()
|
||||
_get_hosts = get_hosts
|
||||
_get_msgs = get_msgs
|
||||
_verbose = config.get("verbose", False),
|
||||
_debug = config.get("debug", False),
|
||||
_debug = config.get("debug", 0),
|
||||
|
||||
servers = []
|
||||
# plain WebSocket
|
||||
websockets_logger = logging.getLogger("websockets.server")
|
||||
websockets_logger.setLevel(logging.DEBUG if _debug > 2 else logging.INFO)
|
||||
#if _debug > 2:
|
||||
# websockets_logger.setLevel(logging.DEBUG)
|
||||
#else:
|
||||
# websockets_logger.setLevel(logging.INFO)
|
||||
# regular WebSocket
|
||||
ws_server = websockets.serve(_handler, host, ws_port) # , subprotocols=["hbd"])
|
||||
servers.append(ws_server)
|
||||
|
||||
Reference in New Issue
Block a user