Skip to content

Commit

Permalink
Issue/n calls per hour (#380)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
peterdudfield and pre-commit-ci[bot] authored Jan 14, 2025
1 parent 8258dfe commit e104175
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
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

0 comments on commit e104175

Please sign in to comment.