Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Neater Murfey DB connection handling from server ConnectionManager #186

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 24 additions & 21 deletions src/murfey/server/websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
from typing import Any, Dict, Generic, TypeVar

from fastapi import APIRouter, WebSocket, WebSocketDisconnect
from sqlmodel import select
from sqlmodel import Session, create_engine, select

from murfey.server.murfey_db import get_murfey_db_session
from murfey.server.murfey_db import get_murfey_db_session, url
from murfey.util.db import ClientEnvironment
from murfey.util.state import State, global_state

Expand All @@ -31,23 +31,25 @@ async def connect(self, websocket: WebSocket, client_id: int):
self._register_new_client(client_id)
await websocket.send_json({"message": "state-full", "state": self._state.data})

@staticmethod
def _register_new_client(client_id: int):
def _register_new_client(self, client_id: int):
engine = create_engine(url())
new_client = ClientEnvironment(client_id=client_id, connected=True)
murfey_db = next(get_murfey_db_session())
murfey_db.add(new_client)
murfey_db.commit()
murfey_db.close()
with Session(engine) as murfey_db:
murfey_db.add(new_client)
murfey_db.commit()

def disconnect(self, websocket: WebSocket, client_id: int):
engine = create_engine(url())
self.active_connections.pop(client_id)
murfey_db = next(get_murfey_db_session())
client_env = murfey_db.exec(
select(ClientEnvironment).where(ClientEnvironment.client_id == client_id)
).one()
murfey_db.delete(client_env)
murfey_db.commit()
murfey_db.close()
with Session(engine) as murfey_db:
client_env = murfey_db.exec(
select(ClientEnvironment).where(
ClientEnvironment.client_id == client_id
)
).one()
murfey_db.delete(client_env)
murfey_db.commit()

async def broadcast(self, message: str):
for connection in self.active_connections:
Expand Down Expand Up @@ -119,14 +121,15 @@ async def forward_log(logrecord: dict[str, Any], websocket: WebSocket):

@ws.delete("/test/{client_id}")
async def close_ws_connection(client_id: int):
engine = create_engine(url())
murfey_db = next(get_murfey_db_session())
client_env = murfey_db.exec(
select(ClientEnvironment).where(ClientEnvironment.client_id == client_id)
).one()
client_env.connected = False
murfey_db.add(client_env)
murfey_db.commit()
murfey_db.close()
with Session(engine) as murfey_db:
client_env = murfey_db.exec(
select(ClientEnvironment).where(ClientEnvironment.client_id == client_id)
).one()
client_env.connected = False
murfey_db.add(client_env)
murfey_db.commit()
client_id_str = str(client_id).replace("\r\n", "").replace("\n", "")
log.info(f"Disconnecting {client_id_str}")
manager.disconnect(manager.active_connections[client_id], client_id)
Expand Down