Skip to content

Commit 47db303

Browse files
committed
health check wait for connect
1 parent 71b4785 commit 47db303

File tree

3 files changed

+23
-16
lines changed

3 files changed

+23
-16
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "textual_web"
3-
version = "0.5.3"
3+
version = "0.5.4"
44
description = "Serve Textual apps"
55
authors = ["Will McGugan <[email protected]>"]
66
license = "MIT"

src/textual_web/ganglion_client.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ def __init__(
9898
self.exit_event = asyncio.Event()
9999
self._task: asyncio.Task | None = None
100100
self._exit_poller = ExitPoller(self, exit_on_idle)
101+
self._connected_event = asyncio.Event()
101102

102103
@property
103104
def app_count(self) -> int:
@@ -207,7 +208,7 @@ def exit_handler(signal_handler, stack_frame) -> None:
207208
self._poller.start()
208209

209210
if self.web_interface:
210-
app = await run_web_interface()
211+
app = await run_web_interface(self._connected_event)
211212
try:
212213
self._task = asyncio.create_task(self.connect())
213214
finally:
@@ -241,6 +242,7 @@ async def _connect(self) -> None:
241242
retry = Retry()
242243

243244
async for retry_count in retry:
245+
self._connected_event.clear()
244246
if self.exit_event.is_set():
245247
break
246248
try:
@@ -325,19 +327,22 @@ async def run_messages() -> None:
325327
async def post_connect(self) -> None:
326328
"""Called immediately after connecting to server."""
327329
# Inform the server about our apps
328-
apps = [
329-
app.model_dump(include={"name", "slug", "color", "terminal"})
330-
for app in self.config.apps
331-
]
332-
if WINDOWS:
333-
filter_apps = [app for app in apps if not app["terminal"]]
334-
if filter_apps != apps:
335-
log.warn(
336-
"Sorry, textual-web does not currently support terminals on Windows"
337-
)
338-
apps = filter_apps
339-
340-
await self.send(packets.DeclareApps(apps))
330+
try:
331+
apps = [
332+
app.model_dump(include={"name", "slug", "color", "terminal"})
333+
for app in self.config.apps
334+
]
335+
if WINDOWS:
336+
filter_apps = [app for app in apps if not app["terminal"]]
337+
if filter_apps != apps:
338+
log.warn(
339+
"Sorry, textual-web does not currently support terminals on Windows"
340+
)
341+
apps = filter_apps
342+
343+
await self.send(packets.DeclareApps(apps))
344+
finally:
345+
self._connected_event.set()
341346

342347
async def send(self, packet: Packet) -> bool:
343348
"""Send a packet.

src/textual_web/web.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@
77

88
import logging
99

10+
import asyncio
1011
from aiohttp import web
1112

1213

1314
log = logging.getLogger("textual-web")
1415

1516

16-
async def run_web_interface() -> web.Application:
17+
async def run_web_interface(connected_event: asyncio.Event) -> web.Application:
1718
"""Run the web interface."""
1819

1920
async def health_check(request) -> web.Response:
21+
await asyncio.wait_for(connected_event.wait(), 5.0)
2022
return web.Response(text="Hello, world")
2123

2224
app = web.Application()

0 commit comments

Comments
 (0)