From 6365f342d24ccbaf33dfda3f3e65d101fc180737 Mon Sep 17 00:00:00 2001 From: Simon Holesch Date: Thu, 15 Aug 2024 12:37:10 +0200 Subject: [PATCH] Add Ready Notification Hub, Exporter and Agent now write a single line to stdout, which can be used as a ready notification for the service supervisor. For systemd, you can use a wrapper like https://github.com/holesch/sdnotify-wrapper-py --- not_my_board/_hub.py | 32 ++++++++++++++++++++------------ not_my_board/_usbip.py | 1 + not_my_board/cli/__init__.py | 2 ++ 3 files changed, 23 insertions(+), 12 deletions(-) diff --git a/not_my_board/_hub.py b/not_my_board/_hub.py index 1707693..28cb14e 100644 --- a/not_my_board/_hub.py +++ b/not_my_board/_hub.py @@ -46,18 +46,7 @@ async def _handle_lifespan(scope, receive, send): message = await receive() if message["type"] == "lifespan.startup": try: - config_file = os.environ.get("NOT_MY_BOARD_HUB_CONFIG") - if not config_file: - config_file = "/etc/not-my-board/hub.toml" - config_file = pathlib.Path(config_file) - - if config_file.exists(): - config = util.toml_loads(config_file.read_text()) - else: - config = {} - - hub = Hub(config, http.Client()) - await hub.startup() + hub = await _on_startup() scope["state"]["hub"] = hub except Exception as err: await send({"type": "lifespan.startup.failed", "message": str(err)}) @@ -75,6 +64,25 @@ async def _handle_lifespan(scope, receive, send): logger.warning("Unknown lifespan message %s", message["type"]) +async def _on_startup(): + config_file = os.environ.get("NOT_MY_BOARD_HUB_CONFIG") + if not config_file: + config_file = "/etc/not-my-board/hub.toml" + config_file = pathlib.Path(config_file) + + if config_file.exists(): + config = util.toml_loads(config_file.read_text()) + else: + logger.warning('Config file "%s" not found', config_file) + config = {} + + hub = Hub(config, http.Client()) + await hub.startup() + print("ready", flush=True) # noqa: T201 + + return hub + + @asgineer.to_asgi async def _handle_request(request): hub = request.scope["state"]["hub"] diff --git a/not_my_board/_usbip.py b/not_my_board/_usbip.py index 2bbac29..cb9fe16 100644 --- a/not_my_board/_usbip.py +++ b/not_my_board/_usbip.py @@ -536,6 +536,7 @@ async def _main(): server = util.Server(usbip_server.handle_client, port=args.port) async with server: logger.info("listening") + print("ready", flush=True) # noqa: T201 await server.serve_forever() diff --git a/not_my_board/cli/__init__.py b/not_my_board/cli/__init__.py index d5ea828..764b92c 100644 --- a/not_my_board/cli/__init__.py +++ b/not_my_board/cli/__init__.py @@ -168,6 +168,7 @@ async def _export_command(args): args.hub_url, args.export_description, http_client, token_src ) as exporter: await exporter.register_place() + print("ready", flush=True) await exporter.serve_forever() @@ -177,6 +178,7 @@ async def _agent_command(args): token_src = _token_src(args, http_client) async with agent.Agent(args.hub_url, io, token_src) as agent_: + print("ready", flush=True) await agent_.serve_forever()