|
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 | + |
2 | 35 |
|
3 | 36 | ppktstore_repo = "monarch-initiative/phenopacket-store" |
4 | 37 | this_repo = os.environ["GITHUB_REPOSITORY"] |
5 | 38 | token = os.environ["GITHUB_TOKEN"] |
6 | 39 | var_name = "LAST_RUN_RELEASE" |
7 | 40 |
|
8 | 41 | # Get phenopacket-store latest version |
9 | | -latest = requests.get( |
| 42 | +latest = fetch_with_retry( |
10 | 43 | f"https://api.github.com/repos/{ppktstore_repo}/releases/latest", |
11 | 44 | headers={"Accept": "application/vnd.github+json"} |
12 | 45 | ).json().get("tag_name") |
|
16 | 49 | sys.exit(1) |
17 | 50 |
|
18 | 51 | # Get last version of phenopacket-store that ppkt2prompt ran |
19 | | -r = requests.get( |
| 52 | +r = fetch_with_retry( |
20 | 53 | f"https://api.github.com/repos/{this_repo}/actions/variables/{var_name}", |
21 | 54 | headers={ |
22 | 55 | "Authorization": f"token {token}", |
|
56 | 89 | f"https://api.github.com/repos/{this_repo}/actions/variables/{var_name}", |
57 | 90 | headers={"Authorization": f"token {token}", |
58 | 91 | "Accept": "application/vnd.github+json"}, |
59 | | - json=payload |
| 92 | + json=payload, |
| 93 | + timeout=10 |
60 | 94 | ) |
61 | 95 | if res.status_code == 404: |
62 | 96 | requests.post( |
63 | 97 | f"https://api.github.com/repos/{this_repo}/actions/variables", |
64 | 98 | headers={"Authorization": f"token {token}", |
65 | 99 | "Accept": "application/vnd.github+json"}, |
66 | | - json=payload |
| 100 | + json=payload, |
| 101 | + timeout=10 |
67 | 102 | ) |
68 | 103 |
|
69 | 104 | # You can also send mail here via SMTP if you prefer Python's smtplib |
|
0 commit comments