From e104175af072b8fe411bb52876086f203d77638e Mon Sep 17 00:00:00 2001 From: Peter Dudfield <34686298+peterdudfield@users.noreply.github.com> Date: Tue, 14 Jan 2025 15:40:43 +0000 Subject: [PATCH] Issue/n calls per hour (#380) * default, rate limit to 60 * add "current_running" in cache * better logging * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * docs * lint * rename QUERY_WAIT_SECONDS * docs * add LOGLEVEL, default wait time 30, set route to not running after waiting 30 seconds * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- src/cache.py | 15 ++++++++++----- src/main.py | 4 +++- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/src/cache.py b/src/cache.py index 1a7d1247..bdd6a941 100644 --- a/src/cache.py +++ b/src/cache.py @@ -17,7 +17,7 @@ DELETE_CACHE_TIME_SECONDS = 240 delete_cache_time_seconds = int(os.getenv("DELETE_CACHE_TIME_SECONDS", DELETE_CACHE_TIME_SECONDS)) -QUERY_WAIT_SECONDS = int(os.getenv("QUERY_WAIT_SECONDS", 10)) +QUERY_WAIT_SECONDS = int(os.getenv("QUERY_WAIT_SECONDS", 30)) def remove_old_cache( @@ -112,7 +112,7 @@ def wrapper(*args, **kwargs): # noqa # 1.0 if currently_running.get(route_variables, False): - logger.debug("Route is being called somewhere else, so waiting for it to finish") + logger.debug("1.0 Route is being called somewhere else, so waiting for it to finish") attempt = 0 while attempt < QUERY_WAIT_SECONDS: logger.debug(f"waiting for route to finish, {attempt} seconds elapsed") @@ -125,14 +125,19 @@ def wrapper(*args, **kwargs): # noqa if route_variables in response: return response[route_variables] else: - logger.warning("route finished, but response not in cache") + logger.warning( + f"Waited {QUERY_WAIT_SECONDS} seconds but response not " + f"in cache. Setting this route as not running, " + f"and continuing" + ) + currently_running[route_variables] = False break # 1.1 check if its been called before and not currently running if (route_variables not in last_updated) and ( not currently_running.get(route_variables, False) ): - logger.debug("First time this is route run, and not running now") + logger.debug("1.1 First time this is route run, and not running now") # run the route currently_running[route_variables] = True @@ -161,7 +166,7 @@ def wrapper(*args, **kwargs): # noqa # 1.3. re-run if response is not cached for some reason or is empty if route_variables not in response or response[route_variables] is None: - logger.debug("not using cache as response is empty") + logger.debug("1.3 not using cache as response is empty") attempt = 0 # wait until response has been cached while attempt < QUERY_WAIT_SECONDS: diff --git a/src/main.py b/src/main.py index b7ab176e..a6602dff 100644 --- a/src/main.py +++ b/src/main.py @@ -24,7 +24,9 @@ # flake8: noqa E501 structlog.configure( - wrapper_class=structlog.make_filtering_bound_logger(logging.INFO), + wrapper_class=structlog.make_filtering_bound_logger( + getattr(logging, os.getenv("LOGLEVEL", "INFO")) + ), processors=[ structlog.processors.EventRenamer("message", replace_by="_event"), structlog.stdlib.PositionalArgumentsFormatter(),