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

Add support for retrying when GitHub rate limit the import #42

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
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
44 changes: 38 additions & 6 deletions scripts/import_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing import TYPE_CHECKING, Dict, List, Sequence, cast

import github
import retrying # type: ignore[import]
from termcolor import cprint
from ticket_type import Ticket

Expand Down Expand Up @@ -222,14 +223,45 @@ def submit(self, ticket: Ticket) -> int:
# primary rate limits which can be queried via the API. Manual testing
# indicates that ~2s isn't always sufficient, so err on the side of a
# larger gap for better reliability.
time.sleep(5)
time.sleep(10)

issue = self.repo.create_issue(
ticket.summary,
self.description_text(ticket),
milestone=self.get_or_create_milestone(ticket.milestone),
labels=self.labels(ticket),
# We also want to wait and retry when GitHub then additionally rate
# limit us anyway...

def retry_on_exception(exception: Exception) -> bool:
if isinstance(exception, github.RateLimitExceededException):
print("... GitHub Rate Limited ...")
return True
return False

@retrying.retry(
retry_on_exception=retry_on_exception,
wait_fixed=60_000,
)
def create_issue() -> github.Issue.Issue:
RATE_LIMIT_MESSAGE = "exceeded a secondary rate limit and have been temporarily blocked" # noqa:E501

try:
return self.repo.create_issue(
ticket.summary,
self.description_text(ticket),
milestone=self.get_or_create_milestone(ticket.milestone),
labels=self.labels(ticket),
)
except github.GithubException as e:
message = e.data.get('message')
if (
isinstance(message, str)
and RATE_LIMIT_MESSAGE in message
):
raise github.RateLimitExceededException(
status=e.status,
data=e.data,
headers=e.headers,
) from e
raise

issue = create_issue()

ticket_number: int = issue.number
self._known_titles[ticket_number] = ticket.summary
Expand Down
7 changes: 6 additions & 1 deletion scripts/requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,13 @@ requests==2.27.1
# via
# -r ./scripts/requirements.txt
# pygithub
retrying==1.3.3
# via -r ./scripts/requirements.txt
six==1.16.0
# via flake8-tuple
# via
# -r ./scripts/requirements.txt
# flake8-tuple
# retrying
termcolor==1.1.0
# via -r ./scripts/requirements.txt
testfixtures==6.18.5
Expand Down
1 change: 1 addition & 0 deletions scripts/requirements.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
PyGithub
PyYAML
retrying
termcolor
4 changes: 4 additions & 0 deletions scripts/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ pyyaml==6.0
# via -r ./scripts/requirements.in
requests==2.27.1
# via pygithub
retrying==1.3.3
# via -r ./scripts/requirements.in
six==1.16.0
# via retrying
termcolor==1.1.0
# via -r ./scripts/requirements.in
urllib3==1.26.9
Expand Down