Skip to content

Commit

Permalink
Add Ready Notification
Browse files Browse the repository at this point in the history
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
  • Loading branch information
holesch committed Aug 15, 2024
1 parent 3af8060 commit 6365f34
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
32 changes: 20 additions & 12 deletions not_my_board/_hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)})
Expand All @@ -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"]
Expand Down
1 change: 1 addition & 0 deletions not_my_board/_usbip.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down
2 changes: 2 additions & 0 deletions not_my_board/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand All @@ -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()


Expand Down

0 comments on commit 6365f34

Please sign in to comment.