Skip to content

Commit

Permalink
Merge pull request #132 from Hafsa-Naeem/retry_git_connection
Browse files Browse the repository at this point in the history
retry after exception (for one case of communicating with GitHub)

Good first example of how a retry can be implemented.

All requested changes have been implemented.
  • Loading branch information
trz42 authored Jan 9, 2023
2 parents a122aac + 63773f1 commit b102d06
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions connections/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
# license: GPLv2
#
import datetime
import time


from tools import config, logging
from github import Github, GithubIntegration
Expand All @@ -30,15 +32,25 @@ def get_token():
with open(private_key_path, 'r') as private_key_file:
private_key = private_key_file.read()

# If the config keys are not set, get_access_token will raise a NotImplementedError
# Returning NoneType token will stop the connection in get_instance
try:
github_integration = GithubIntegration(app_id, private_key)
# Note that installation access tokens last only for 1 hour, you will need to regenerate them after they expire.
_token = github_integration.get_access_token(installation_id)
except NotImplementedError as err:
logging.error(err)
_token = None
tries = 3
for i in range(tries):
# If the config keys are not set, get_access_token will raise a NotImplementedError
# Returning NoneType token will stop the connection in get_instance
try:
github_integration = GithubIntegration(app_id, private_key)
# Note that installation access tokens last only for 1 hour,
# you will need to regenerate them after they expire.
_token = github_integration.get_access_token(installation_id)
break
except NotImplementedError as err:
if i < tries - 1:
n = 0.8
t = n*(i+1)
time.sleep(t)
continue
else:
logging.error(err)
_token = None

return _token

Expand Down

0 comments on commit b102d06

Please sign in to comment.