Skip to content

Commit 6f5aa21

Browse files
authored
Merge pull request #90 from P2GX/copilot/add-retry-logic-to-fetch
Add retry logic with exponential backoff to GitHub API requests
2 parents f3ebb52 + b0f1f17 commit 6f5aa21

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ replay_pid*
2727
/prompts/
2828
/fenominal-mined.txt
2929
/p2p_test.txt
30+
__pycache__/

scripts/check_ppktstore_version.py

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,45 @@
1-
import os, requests, sys
1+
import os, requests, sys, time
2+
3+
4+
def fetch_with_retry(url, headers, max_retries=3, timeout=10):
5+
"""
6+
Fetch data from URL with retry logic and exponential backoff.
7+
8+
Args:
9+
url: The URL to fetch
10+
headers: HTTP headers to include in the request
11+
max_retries: Maximum number of retry attempts (default: 3)
12+
timeout: Request timeout in seconds (default: 10)
13+
14+
Returns:
15+
Response object if successful
16+
17+
Raises:
18+
Exception on final failed attempt
19+
"""
20+
for attempt in range(max_retries):
21+
try:
22+
response = requests.get(url, headers=headers, timeout=timeout)
23+
response.raise_for_status()
24+
return response
25+
except Exception as e:
26+
if attempt < max_retries - 1:
27+
wait_time = 2 ** attempt # Exponential backoff: 1s, 2s, 4s
28+
print(f"Request failed (attempt {attempt + 1}/{max_retries}): {e}")
29+
print(f"Retrying in {wait_time} seconds...")
30+
time.sleep(wait_time)
31+
else:
32+
# Final attempt failed, raise the exception
33+
raise
34+
235

336
ppktstore_repo = "monarch-initiative/phenopacket-store"
437
this_repo = os.environ["GITHUB_REPOSITORY"]
538
token = os.environ["GITHUB_TOKEN"]
639
var_name = "LAST_RUN_RELEASE"
740

841
# Get phenopacket-store latest version
9-
latest = requests.get(
42+
latest = fetch_with_retry(
1043
f"https://api.github.com/repos/{ppktstore_repo}/releases/latest",
1144
headers={"Accept": "application/vnd.github+json"}
1245
).json().get("tag_name")
@@ -16,7 +49,7 @@
1649
sys.exit(1)
1750

1851
# Get last version of phenopacket-store that ppkt2prompt ran
19-
r = requests.get(
52+
r = fetch_with_retry(
2053
f"https://api.github.com/repos/{this_repo}/actions/variables/{var_name}",
2154
headers={
2255
"Authorization": f"token {token}",
@@ -56,14 +89,16 @@
5689
f"https://api.github.com/repos/{this_repo}/actions/variables/{var_name}",
5790
headers={"Authorization": f"token {token}",
5891
"Accept": "application/vnd.github+json"},
59-
json=payload
92+
json=payload,
93+
timeout=10
6094
)
6195
if res.status_code == 404:
6296
requests.post(
6397
f"https://api.github.com/repos/{this_repo}/actions/variables",
6498
headers={"Authorization": f"token {token}",
6599
"Accept": "application/vnd.github+json"},
66-
json=payload
100+
json=payload,
101+
timeout=10
67102
)
68103

69104
# You can also send mail here via SMTP if you prefer Python's smtplib

0 commit comments

Comments
 (0)