Skip to content

Commit

Permalink
rm monitor websocket
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex committed Nov 22, 2024
1 parent 30f4928 commit 907623f
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 3 deletions.
19 changes: 19 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "/Users/alexloembe/NIST/usnistgov/oar-pdr-py/python/tests/nistoar/midas/dbio/test_inmem.py",
"console": "integratedTerminal",
"pythonPath": "/Users/alexloembe/anaconda3/envs/dbio/bin/python",
"env": {
"PYTHONPATH": "/Users/alexloembe/NIST/usnistgov/oar-pdr-py/python",
},
"terminal": "integrated",
"justMyCode": false
}
]
}

16 changes: 14 additions & 2 deletions scripts/midas-uwsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@
configuration data for (default: pdr-resolve);
this is only used if OAR_CONFIG_SERVICE is used.
"""
import os, sys, logging, copy
import os, sys, logging, copy,asyncio
from copy import deepcopy
from websocket import WebsocketServer

try:
import nistoar
Expand Down Expand Up @@ -61,6 +62,16 @@ def _dec(obj):

# determine where the configuration is coming from
confsrc = _dec(uwsgi.opt.get("oar_config_file"))

def initialize_websocket_server():
websocket_server = WebSocketServer()
loop = asyncio.get_event_loop()
loop.run_until_complete(websocket_server.start())
print("WebSocketServer initialized:", websocket_server)
return websocket_server

websocket_server = initialize_websocket_server()

if confsrc:
cfg = config.resolve_configuration(confsrc)

Expand Down Expand Up @@ -129,9 +140,10 @@ def _dec(obj):
print(f"dburl: {dburl}")
factory = MongoDBClientFactory(cfg.get("dbio", {}), dburl)
elif dbtype == "inmem":
factory = InMemoryDBClientFactory(cfg.get("dbio", {}))
factory = InMemoryDBClientFactory(cfg.get("dbio", {}),websocket_server=websocket_server)
else:
raise RuntimeError("Unsupported database type: "+dbtype)

application = wsgi.app(cfg, factory)
websocket_server.start_in_thread()
logging.info("MIDAS service ready with "+dbtype+" backend")
43 changes: 43 additions & 0 deletions scripts/websocket.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# websocket_server.py
import asyncio
import websockets
from concurrent.futures import ThreadPoolExecutor

class WebSocketServer:
def __init__(self, host="localhost", port=8765):
self.host = host
self.port = port
self.server = None
self.clients = set() # Initialize the clients set

async def start(self):
self.server = await websockets.serve(self.websocket_handler, self.host, self.port)
print(f"WebSocket server started on ws://{self.host}:{self.port}")


async def websocket_handler(self, websocket, path):
# Add the new client to the set
self.clients.add(websocket)
try:
async for message in websocket:
await self.send_message_to_clients(message)
finally:
# Remove the client from the set when they disconnect
self.clients.remove(websocket)

async def send_message_to_clients(self, message):
if self.clients:
await asyncio.wait([client.send(message) for client in self.clients])

def is_running(self):
return self.server is not None and self.server.is_serving()

def start_in_thread(self):
executor = ThreadPoolExecutor(max_workers=1)
executor.submit(self._start_event_loop)

def _start_event_loop(self):
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(self.start())

0 comments on commit 907623f

Please sign in to comment.