Skip to content

Commit

Permalink
add LOGLEVEL, default wait time 30, set route to not running after wa…
Browse files Browse the repository at this point in the history
…iting 30 seconds
  • Loading branch information
peterdudfield committed Jan 14, 2025
1 parent 5a722cf commit 7b47200
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
13 changes: 8 additions & 5 deletions src/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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")
Expand All @@ -125,14 +125,17 @@ 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
Expand Down Expand Up @@ -161,7 +164,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:
Expand Down
3 changes: 2 additions & 1 deletion src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
# 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(),
Expand Down

0 comments on commit 7b47200

Please sign in to comment.