Skip to content

Commit

Permalink
feat: method to check whether server is started
Browse files Browse the repository at this point in the history
  • Loading branch information
lars-reimann committed May 2, 2024
1 parent d9414a8 commit 1871be1
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/safeds_runner/server/_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class SafeDsServer:
def __init__(self) -> None:
self._sio = socketio.AsyncServer(logger=True, async_mode="asgi")
self._app = socketio.ASGIApp(self._sio)
self._server: uvicorn.Server | None = None

self._process_manager = ProcessManager()
self._pipeline_manager = PipelineManager(self._process_manager)
self._lock = Lock()
Expand All @@ -35,14 +37,18 @@ async def startup(self, port: int) -> None:

logging.info("Starting Safe-DS Runner on port %s...", str(port))
config = uvicorn.config.Config(self._app, host="127.0.0.1", port=port)
server = uvicorn.server.Server(config)
await server.serve()
self._server = uvicorn.Server(config)
await self._server.serve()

async def shutdown(self) -> None:
"""Shutdown the server."""
self._process_manager.shutdown()
await self._sio.shutdown()

def is_started(self) -> bool:
"""Check if the server is started."""
return self._server is not None and self._server.started

def _interrupt_handler(self, _signal: Any, _frame: Any) -> None:
"""Handle the interrupt signal."""
asyncio.get_running_loop().create_task(self.shutdown())
Expand Down
6 changes: 6 additions & 0 deletions tests/safeds_runner/server/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ def run_server():
thread = threading.Thread(target=run_server, daemon=True)
thread.start()

# Wait until the server is ready to accept connections
for _ in range(10 * BASE_TIMEOUT):
if server.is_started():
break
await asyncio.sleep(0.1)

# Run the actual test
yield

Expand Down

0 comments on commit 1871be1

Please sign in to comment.