refactor: use try block for error handling and session rollback
All checks were successful
Component testing / Hub testing (push) Successful in 20s
Component testing / Store testing (push) Successful in 35s
Component testing / Integration smoke testing (push) Successful in 2m38s

This commit is contained in:
ІМ-24 Владислав Коваленко
2026-03-26 13:38:04 +00:00
parent 2e623c3a93
commit 92c91c2594

View File

@@ -60,19 +60,23 @@ async def websocket_endpoint(websocket: WebSocket, user_id: int):
# receive forever
while True:
data = await websocket.receive_text()
if (data):
ws_data = WebSocketData.model_validate(json.loads(data))
session = SessionLocal()
update_query = (
processed_agent_data.update()
.where(processed_agent_data.c.id == ws_data.id)
.values(visible=False)
).returning(processed_agent_data)
res = session.execute(update_query).fetchone()
if (not res):
print("Websocket update fail")
session.commit()
session.close()
try:
if (data):
ws_data = WebSocketData.model_validate(json.loads(data))
session = SessionLocal()
update_query = (
processed_agent_data.update()
.where(processed_agent_data.c.id == ws_data.id)
.values(visible=False)
).returning(processed_agent_data)
res = session.execute(update_query).fetchone()
if (not res):
session.rollback()
raise Exception("Error while websocket PUT")
session.commit()
finally:
session.close()
except WebSocketDisconnect:
subscriptions.remove(websocket)