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

Change issue caching strategy & throttle API requests #70

Merged
merged 1 commit into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
9 changes: 7 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,9 @@
CF_PARENT_LINK = "customfield_12313140" # any
CF_STATUS_SUMMARY = "customfield_12320841" # string

# How long to delay between API calls
CALL_DELAY_SECONDS: float = 0.2


@dataclass
class Change:
Expand Down Expand Up @@ -388,6 +392,7 @@ def _check(response: Any) -> dict:
general, when things go well, you get back a dict. Otherwise, you could get
anything.
"""
sleep(CALL_DELAY_SECONDS)
if isinstance(response, dict):
return response
raise ValueError(f"Unexpected response: {response}")
Expand Down Expand Up @@ -418,7 +423,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 +436,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
Loading