Skip to content

Commit d3f4fa1

Browse files
committed
Hub: Refactor startup
Move startup logic out of ASGI lifespan handler.
1 parent 9142e67 commit d3f4fa1

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

not_my_board/_hub.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -61,18 +61,7 @@ async def _handle_lifespan(scope, receive, send):
6161
message = await receive()
6262
if message["type"] == "lifespan.startup":
6363
try:
64-
config_file = os.environ.get("NOT_MY_BOARD_HUB_CONFIG")
65-
if not config_file:
66-
config_file = "/etc/not-my-board/hub.toml"
67-
config_file = pathlib.Path(config_file)
68-
69-
if config_file.exists():
70-
config = util.toml_loads(config_file.read_text())
71-
else:
72-
config = {}
73-
74-
hub = Hub(config, http.Client())
75-
await hub.startup()
64+
hub = await _on_startup()
7665
scope["state"]["hub"] = hub
7766
except Exception as err:
7867
await send({"type": "lifespan.startup.failed", "message": str(err)})
@@ -90,6 +79,24 @@ async def _handle_lifespan(scope, receive, send):
9079
logger.warning("Unknown lifespan message %s", message["type"])
9180

9281

82+
async def _on_startup():
83+
config_file = os.environ.get("NOT_MY_BOARD_HUB_CONFIG")
84+
if not config_file:
85+
config_file = "/etc/not-my-board/hub.toml"
86+
config_file = pathlib.Path(config_file)
87+
88+
if config_file.exists():
89+
config = util.toml_loads(config_file.read_text())
90+
else:
91+
logger.warning('Config file "%s" not found', config_file)
92+
config = {}
93+
94+
hub = Hub(config, http.Client())
95+
await hub.startup()
96+
97+
return hub
98+
99+
93100
@asgineer.to_asgi
94101
async def _handle_request(request):
95102
hub = request.scope["state"]["hub"]

0 commit comments

Comments
 (0)