Skip to content

Commit

Permalink
Change issue caching strategy & throttle API requests
Browse files Browse the repository at this point in the history
Signed-off-by: John Strunk <[email protected]>
  • Loading branch information
JohnStrunk committed May 16, 2024
1 parent 0f939d2 commit 82b9408
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
6 changes: 3 additions & 3 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,17 @@ def main():
issue_keys = get_issues_to_summarize(jira, since)
for issue_key in issue_keys:
issue_start_time = datetime.now()
issue = issue_cache.get_issue(jira, issue_key, refresh=True)
issue = issue_cache.get_issue(jira, issue_key)
summary = summarize_issue(
issue, max_depth=max_depth, send_updates=send_updates
)
elapsed = datetime.now() - issue_start_time
print(f"Summarized {issue_key} ({elapsed}s):\n{summary}\n")
since = start_time # Only update if we succeeded
except requests.exceptions.HTTPError as error:
logging.error("HTTPError exception: %s", error)
logging.error("HTTPError exception: %s", error, stack_info=True)
except requests.exceptions.ReadTimeout as error:
logging.error("ReadTimeout exception: %s", error)
logging.error("ReadTimeout exception: %s", error, stack_info=True)
logging.info(
"Cache stats: %d hits, %d total", issue_cache.hits, issue_cache.tries
)
Expand Down
8 changes: 6 additions & 2 deletions jiraissues.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
from dataclasses import dataclass, field
from datetime import datetime
from time import sleep
from typing import Any, List, Optional
from zoneinfo import ZoneInfo

Expand All @@ -16,6 +17,8 @@
CF_PARENT_LINK = "customfield_12313140" # any
CF_STATUS_SUMMARY = "customfield_12320841" # string

CallDelaySeconds = 0.1


@dataclass
class Change:
Expand Down Expand Up @@ -388,6 +391,7 @@ def _check(response: Any) -> dict:
general, when things go well, you get back a dict. Otherwise, you could get
anything.
"""
sleep(CallDelaySeconds)
if isinstance(response, dict):
return response
raise ValueError(f"Unexpected response: {response}")
Expand Down Expand Up @@ -418,7 +422,7 @@ def __init__(self) -> None:
self.hits = 0
self.tries = 0

def get_issue(self, client: Jira, key: str, refresh: bool = False) -> Issue:
def get_issue(self, client: Jira, key: str) -> Issue:
"""
Get an issue from the cache, or fetch it from the server if it's not
already cached.
Expand All @@ -431,7 +435,7 @@ def get_issue(self, client: Jira, key: str, refresh: bool = False) -> Issue:
The issue object.
"""
self.tries += 1
if refresh or key not in self._cache:
if key not in self._cache:
_logger.debug("Cache miss: %s", key)
self._cache[key] = Issue(client, key)
else:
Expand Down
8 changes: 3 additions & 5 deletions summarizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,11 +350,9 @@ def get_issues_to_summarize(
keys: List[str] = [issue["key"] for issue in updated_issues["issues"]]
# Filter out any issues that are not in the allowed projects
filtered_keys = []
issue_cache.clear() # Clear the cache to ensure we have the latest data
for key in keys:
# Refresh the issue cache to ensure we have the latest data in cache.
# This is definitely needed since the keys are the result of the query
# for recently updated issues.
issue = issue_cache.get_issue(client, key, refresh=True)
issue = issue_cache.get_issue(client, key)
if is_ok_to_post_summary(issue):
filtered_keys.append(key)
keys = filtered_keys
Expand All @@ -371,7 +369,7 @@ def get_issues_to_summarize(
# summarize, but only if they are marked for summarization.
for parent in parents:
if parent not in all_keys:
issue = issue_cache.get_issue(client, parent, refresh=True)
issue = issue_cache.get_issue(client, parent)
if is_ok_to_post_summary(issue):
all_keys.append(parent)
else:
Expand Down

0 comments on commit 82b9408

Please sign in to comment.