Skip to content

Commit 89502c3

Browse files
committed
update retry logic and logging
### What 1. configureable retry count 2. additional logging ### Why 1. pass retry count as a command line arg; default 5 2. show details when api requests fail ### Testing before merge compiles cleanly ### Validation after merge compile and test ### Issue addressed by this PR https://github.com/stellar/ops/issues/2039
1 parent 81a72ac commit 89502c3

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

github_backup/cli.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import logging
55
import os
66
import sys
7+
from github_backup import max_retries
78

89
from github_backup.github_backup import (
910
backup_account,
@@ -39,6 +40,7 @@
3940
def main():
4041
"""Main entry point for github-backup CLI."""
4142
args = parse_args()
43+
max_retries.MAX_RETRIES = args.max_retries
4244

4345
if args.private and not get_auth(args):
4446
logger.warning(

github_backup/github_backup.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from urllib.error import HTTPError, URLError
2626
from urllib.parse import urlencode, urlparse
2727
from urllib.request import HTTPRedirectHandler, Request, build_opener, urlopen
28+
from github_backup import max_retries
2829

2930
try:
3031
from . import __version__
@@ -75,7 +76,7 @@ def __init__(self, message, dmca_url=None):
7576
)
7677

7778
# Retry configuration
78-
MAX_RETRIES = 5
79+
MAX_RETRIES = max_retries.MAX_RETRIES
7980

8081

8182
def logging_subprocess(
@@ -468,6 +469,13 @@ def parse_args(args=None):
468469
parser.add_argument(
469470
"--exclude", dest="exclude", help="names of repositories to exclude", nargs="*"
470471
)
472+
parser.add_argument(
473+
"--retries",
474+
dest="max_retries",
475+
type=int,
476+
default=5,
477+
help="maximum number of retries for API calls (default: 5)",
478+
)
471479
return parser.parse_args(args)
472480

473481

@@ -737,29 +745,32 @@ def is_retryable_status(status_code, headers):
737745
except HTTPError as exc:
738746
# HTTPError can be used as a response-like object
739747
if not is_retryable_status(exc.code, exc.headers):
748+
logger.error(f"API Error: {exc.code} {exc.reason} for {request.full_url}")
740749
raise # Non-retryable error
741750

742751
if attempt >= MAX_RETRIES - 1:
743752
logger.error(f"HTTP {exc.code} failed after {MAX_RETRIES} attempts")
753+
logger.error(f"HTTP {exc.code} failed after {MAX_RETRIES} attempts for {request.full_url}")
744754
raise
745755

746756
delay = calculate_retry_delay(attempt, exc.headers)
747757
logger.warning(
748-
f"HTTP {exc.code}, retrying in {delay:.1f}s "
749-
f"(attempt {attempt + 1}/{MAX_RETRIES})"
758+
f"HTTP {exc.code} ({exc.reason}), retrying in {delay:.1f}s "
759+
f"(attempt {attempt + 1}/{MAX_RETRIES}) for {request.full_url}"
760+
750761
)
751762
if auth is None and exc.code in (403, 429):
752763
logger.info("Hint: Authenticate to raise your GitHub rate limit")
753764
time.sleep(delay)
754765

755766
except (URLError, socket.error) as e:
756767
if attempt >= MAX_RETRIES - 1:
757-
logger.error(f"Connection error failed after {MAX_RETRIES} attempts: {e}")
768+
logger.error(f"Connection error failed after {MAX_RETRIES} attempts: {e} for {request.full_url}")
758769
raise
759770
delay = calculate_retry_delay(attempt, {})
760771
logger.warning(
761772
f"Connection error: {e}, retrying in {delay:.1f}s "
762-
f"(attempt {attempt + 1}/{MAX_RETRIES})"
773+
f"(attempt {attempt + 1}/{MAX_RETRIES}) for {request.full_url}"
763774
)
764775
time.sleep(delay)
765776

github_backup/max_retries.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
MAX_RETRIES=None

0 commit comments

Comments
 (0)