Skip to content

Commit

Permalink
Hub: Refactor startup
Browse files Browse the repository at this point in the history
Move startup logic out of ASGI lifespan handler.
  • Loading branch information
holesch committed Aug 15, 2024
1 parent f6317cd commit 6ef20d5
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions not_my_board/_hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,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 @@ -90,6 +79,24 @@ 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()

return hub


@asgineer.to_asgi
async def _handle_request(request):
hub = request.scope["state"]["hub"]
Expand Down

0 comments on commit 6ef20d5

Please sign in to comment.