Skip to content

Commit

Permalink
Merge pull request #92 from JohnStrunk/backoff
Browse files Browse the repository at this point in the history
Use a backoff library for API retries
  • Loading branch information
mergify[bot] authored Jun 3, 2024
2 parents 701b611 + 4b44739 commit 35824c1
Show file tree
Hide file tree
Showing 8 changed files with 452 additions and 270 deletions.
1 change: 1 addition & 0 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ name = "pypi"
atlassian-python-api = "3.41.11"
langchain = "*"
ibm-generative-ai = "*"
backoff-utils = "*"

[dev-packages]
ipykernel = "*"
Expand Down
424 changes: 269 additions & 155 deletions Pipfile.lock

Large diffs are not rendered by default.

51 changes: 11 additions & 40 deletions bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import time
from datetime import UTC, datetime

import requests
from atlassian import Jira # type: ignore

from jiraissues import issue_cache
Expand Down Expand Up @@ -75,23 +74,9 @@ def main() -> None: # pylint: disable=too-many-locals,too-many-statements
start_time = datetime.now(UTC)
logging.info("Starting iteration at %s", start_time.isoformat())
issue_keys: list[str] = []
successful = False
while not successful:
try:
(issue_keys, most_recent_modification) = get_issues_to_summarize(
jira, since, limit
)
successful = True
except requests.exceptions.HTTPError as error:
logging.error(
"HTTPError exception (%s): %s",
error.request.url,
error.response.reason,
)
time.sleep(5)
except requests.exceptions.ReadTimeout as error:
logging.error("ReadTimeout exception: %s", error, exc_info=True)
time.sleep(5)
(issue_keys, most_recent_modification) = get_issues_to_summarize(
jira, since, limit
)

if len(issue_keys) < limit - 5:
# We retrieved all the modified issues, so we can advance farther
Expand All @@ -100,28 +85,14 @@ def main() -> None: # pylint: disable=too-many-locals,too-many-statements
logging.info("Got updates through %s", most_recent_modification.isoformat())

for issue_key in issue_keys:
successful = False
while not successful:
try:
issue_start_time = datetime.now(UTC)
issue = issue_cache.get_issue(jira, issue_key)
summary = summarize_issue(
issue, max_depth=max_depth, send_updates=send_updates
)
elapsed = datetime.now(UTC) - issue_start_time
print(f"Summarized {issue_key} ({elapsed}s):\n{summary}\n")
successful = True
except requests.exceptions.HTTPError as error:
logging.error(
"HTTPError exception (%s): %s",
error.request.url,
error.response.reason,
)
time.sleep(5)
except requests.exceptions.ReadTimeout as error:
logging.error("ReadTimeout exception: %s", error, exc_info=True)
time.sleep(5)
since = most_recent_modification # Only update if we succeeded
issue_start_time = datetime.now(UTC)
issue = issue_cache.get_issue(jira, issue_key)
summary = summarize_issue(
issue, max_depth=max_depth, send_updates=send_updates
)
elapsed = datetime.now(UTC) - issue_start_time
print(f"Summarized {issue_key} ({elapsed}s):\n{summary}\n")
since = most_recent_modification
logging.info("Cache stats: %s", issue_cache)
now = datetime.now(UTC)
elapsed = now - start_time
Expand Down
40 changes: 19 additions & 21 deletions estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
from dataclasses import dataclass
from datetime import datetime, timedelta

import requests
from atlassian import Jira # type: ignore

import jiraissues
from jiraissues import Issue, check_response, get_self, issue_cache
from jiraissues import Issue, check_response, get_self, issue_cache, with_retry
from summarizer import count_tokens, summarize_issue


Expand Down Expand Up @@ -82,15 +80,22 @@ def get_modified_issues(client: Jira, since: datetime) -> list[Issue]:
since_string = since.astimezone(user_zi).strftime("%Y-%m-%d %H:%M")

issues = check_response(
client.jql(
f"updated >= '{since_string}' ORDER BY updated DESC",
limit=1000,
fields="key",
with_retry(
lambda: client.jql(
f"updated >= '{since_string}' ORDER BY updated DESC",
limit=1000,
fields="key",
)
)
)
for issue in issues["issues"]:
issue_cache.remove(issue["key"])
return [issue_cache.get_issue(client, issue["key"]) for issue in issues["issues"]]

issue_list: list[Issue] = []
for idict in issues["issues"]:
issue = issue_cache.get_issue(client, idict["key"])
issue_list.append(issue)
return issue_list


def main() -> None:
Expand Down Expand Up @@ -125,25 +130,18 @@ def main() -> None:

jira = Jira(url=os.environ["JIRA_URL"], token=os.environ["JIRA_TOKEN"])

jiraissues.MIN_CALL_DELAY = 0.25 # Override the default delay

print(IssueEstimate.csv_header(), file=outfile)
since = datetime.now() + timedelta(seconds=-delay)
while True:
start_time = datetime.now()
logging.info("Starting iteration at %s", start_time.isoformat())
processed = 0
try:
issues = get_modified_issues(jira, since)
print(f"Found {len(issues)} issues modified since {since}")
for issue in issues:
print(estimate_issue(issue).as_csv(), file=outfile, flush=True)
processed += 1
since = start_time # Only update if we succeeded
except requests.exceptions.HTTPError as error:
logging.error("HTTPError exception: %s", error.response.reason)
except requests.exceptions.ReadTimeout as error:
logging.error("ReadTimeout exception: %s", error, exc_info=True)
issues = get_modified_issues(jira, since)
print(f"Found {len(issues)} issues modified since {since}")
for issue in issues:
print(estimate_issue(issue).as_csv(), file=outfile, flush=True)
processed += 1
since = start_time
print(issue_cache)
print(f"Issues processed: {processed}")
print(f"Iteration elapsed time: {datetime.now() - start_time}")
Expand Down
78 changes: 77 additions & 1 deletion jira_howto.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,83 @@
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
"source": [
"from requests import HTTPError\n",
"\n",
"\n",
"issue = Issue(jira, \"OCTOET-377\")\n",
"pprint(issue.changelog)\n",
"try:\n",
" pprint(jira.get_issue_changelog(\"OCTOET-377\"))\n",
"except HTTPError as e:\n",
" pprint(e.response.json())\n",
" pprint(e.request.params)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from datetime import UTC, datetime\n",
"\n",
"print(issue.updated.tzname())\n",
"print(datetime.now(UTC).tzname())\n",
"print(datetime.now(UTC))\n",
"print(datetime.now())\n",
"print(datetime.now().isoformat())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import time\n",
"from zoneinfo import ZoneInfo\n",
"\n",
"\n",
"us_east = ZoneInfo(\"America/New_York\")\n",
"issue = Issue(jira, \"OCTOET-377\")\n",
"print(issue.updated.astimezone(us_east))\n",
"\n",
"time.time()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"issue = Issue(jira, \"OCTOET-85\")\n",
"us_east = ZoneInfo(\"America/New_York\")\n",
"\n",
"print(issue.updated.astimezone(us_east))\n",
"comments = issue.comments\n",
"for comment in comments:\n",
" print(comment.created.astimezone(us_east))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"jira.user(username=\"assisted-installer-bot\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"jira.myself()"
]
}
],
"metadata": {
Expand Down
Loading

0 comments on commit 35824c1

Please sign in to comment.