From 47db3035f632b1885269d1a39f79b14f3d4ceadd Mon Sep 17 00:00:00 2001 From: Will McGugan Date: Fri, 29 Sep 2023 14:48:08 +0100 Subject: [PATCH] health check wait for connect --- pyproject.toml | 2 +- src/textual_web/ganglion_client.py | 31 +++++++++++++++++------------- src/textual_web/web.py | 4 +++- 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 79f3019..f836f19 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "textual_web" -version = "0.5.3" +version = "0.5.4" description = "Serve Textual apps" authors = ["Will McGugan "] license = "MIT" diff --git a/src/textual_web/ganglion_client.py b/src/textual_web/ganglion_client.py index be3009e..e724e71 100644 --- a/src/textual_web/ganglion_client.py +++ b/src/textual_web/ganglion_client.py @@ -98,6 +98,7 @@ class GanglionClient(Handlers): self.exit_event = asyncio.Event() self._task: asyncio.Task | None = None self._exit_poller = ExitPoller(self, exit_on_idle) + self._connected_event = asyncio.Event() @property def app_count(self) -> int: @@ -207,7 +208,7 @@ class GanglionClient(Handlers): self._poller.start() if self.web_interface: - app = await run_web_interface() + app = await run_web_interface(self._connected_event) try: self._task = asyncio.create_task(self.connect()) finally: @@ -241,6 +242,7 @@ class GanglionClient(Handlers): retry = Retry() async for retry_count in retry: + self._connected_event.clear() if self.exit_event.is_set(): break try: @@ -325,19 +327,22 @@ class GanglionClient(Handlers): async def post_connect(self) -> None: """Called immediately after connecting to server.""" # Inform the server about our apps - apps = [ - app.model_dump(include={"name", "slug", "color", "terminal"}) - for app in self.config.apps - ] - if WINDOWS: - filter_apps = [app for app in apps if not app["terminal"]] - if filter_apps != apps: - log.warn( - "Sorry, textual-web does not currently support terminals on Windows" - ) - apps = filter_apps + try: + apps = [ + app.model_dump(include={"name", "slug", "color", "terminal"}) + for app in self.config.apps + ] + if WINDOWS: + filter_apps = [app for app in apps if not app["terminal"]] + if filter_apps != apps: + log.warn( + "Sorry, textual-web does not currently support terminals on Windows" + ) + apps = filter_apps - await self.send(packets.DeclareApps(apps)) + await self.send(packets.DeclareApps(apps)) + finally: + self._connected_event.set() async def send(self, packet: Packet) -> bool: """Send a packet. diff --git a/src/textual_web/web.py b/src/textual_web/web.py index 53bab24..88715a0 100644 --- a/src/textual_web/web.py +++ b/src/textual_web/web.py @@ -7,16 +7,18 @@ Note: Currently just a stub. import logging +import asyncio from aiohttp import web log = logging.getLogger("textual-web") -async def run_web_interface() -> web.Application: +async def run_web_interface(connected_event: asyncio.Event) -> web.Application: """Run the web interface.""" async def health_check(request) -> web.Response: + await asyncio.wait_for(connected_event.wait(), 5.0) return web.Response(text="Hello, world") app = web.Application()