1
0
mirror of https://github.com/GNS3/gns3-server synced 2024-11-25 01:38:08 +00:00

Fix some issues with HTTP notification streams

This commit is contained in:
grossmj 2022-09-13 22:10:01 +02:00
parent e8973b9c44
commit b0e646b97d
2 changed files with 18 additions and 10 deletions

View File

@ -18,7 +18,7 @@
API routes for controller notifications. API routes for controller notifications.
""" """
from fastapi import APIRouter, Depends, WebSocket, WebSocketDisconnect from fastapi import APIRouter, Request, Depends, WebSocket, WebSocketDisconnect
from fastapi.responses import StreamingResponse from fastapi.responses import StreamingResponse
from websockets.exceptions import ConnectionClosed, WebSocketException from websockets.exceptions import ConnectionClosed, WebSocketException
@ -35,17 +35,24 @@ router = APIRouter()
@router.get("", dependencies=[Depends(get_current_active_user)]) @router.get("", dependencies=[Depends(get_current_active_user)])
async def controller_http_notifications() -> StreamingResponse: async def controller_http_notifications(request: Request) -> StreamingResponse:
""" """
Receive controller notifications about the controller from HTTP stream. Receive controller notifications about the controller from HTTP stream.
""" """
from gns3server.api.server import app
log.info(f"New client {request.client.host}:{request.client.port} has connected to controller HTTP "
f"notification stream")
async def event_stream(): async def event_stream():
try:
with Controller.instance().notification.controller_queue() as queue: with Controller.instance().notification.controller_queue() as queue:
while True: while not app.state.exiting:
msg = await queue.get_json(5) msg = await queue.get_json(5)
yield f"{msg}\n".encode("utf-8") yield f"{msg}\n".encode("utf-8")
finally:
log.info(f"Client {request.client.host}:{request.client.port} has disconnected from controller HTTP "
f"notification stream")
return StreamingResponse(event_stream(), media_type="application/json") return StreamingResponse(event_stream(), media_type="application/json")

View File

@ -210,18 +210,19 @@ async def project_http_notifications(project_id: UUID) -> StreamingResponse:
Receive project notifications about the controller from HTTP stream. Receive project notifications about the controller from HTTP stream.
""" """
from gns3server.api.server import app
controller = Controller.instance() controller = Controller.instance()
project = controller.get_project(str(project_id)) project = controller.get_project(str(project_id))
log.info(f"New client has connected to the notification stream for project ID '{project.id}' (HTTP steam method)") log.info(f"New client has connected to the notification stream for project ID '{project.id}' (HTTP stream method)")
async def event_stream(): async def event_stream():
try: try:
with controller.notification.project_queue(project.id) as queue: with controller.notification.project_queue(project.id) as queue:
while True: while not app.state.exiting:
msg = await queue.get_json(5) msg = await queue.get_json(5)
yield (f"{msg}\n").encode("utf-8") yield f"{msg}\n".encode("utf-8")
finally: finally:
log.info(f"Client has disconnected from notification for project ID '{project.id}' (HTTP stream method)") log.info(f"Client has disconnected from notification for project ID '{project.id}' (HTTP stream method)")
if project.auto_close: if project.auto_close: