Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue/n calls per hour #380

Merged
merged 12 commits into from
Jan 14, 2025
15 changes: 10 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,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
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 3 additions & 1 deletion src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
Loading