From 3b3a5c4b41a60d7c028d5314dbf5aaebc19253f3 Mon Sep 17 00:00:00 2001 From: 15532th <92187332+15532th@users.noreply.github.com> Date: Thu, 19 Sep 2024 02:59:52 +0300 Subject: [PATCH] Run first extraction before starting webserver It leaves less time for client to hit 503 error on startup, but more importantly, it deals with race condition in startup sequence: if extractor_task gets interrupted after webserver startup was initiated but before make_server() returned, then potoken_server.stop() becomes a no-op, leaving background task with webserver to run indefinitely. --- potoken_generator/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/potoken_generator/main.py b/potoken_generator/main.py index 2838190..d9859cc 100644 --- a/potoken_generator/main.py +++ b/potoken_generator/main.py @@ -30,8 +30,8 @@ def print_token_and_exit(token_info: Optional[TokenInfo]): async def run(loop: asyncio.AbstractEventLoop, oneshot: bool, update_interval: int, bind_address: str, port: int) -> None: potoken_extractor = PotokenExtractor(loop, update_interval=update_interval) + token = await potoken_extractor.run_once() if oneshot: - token = await potoken_extractor.run_once() print_token_and_exit(token) extractor_task = loop.create_task(potoken_extractor.run())