|
4 | 4 | from django.db import transaction |
5 | 5 | from django.utils import timezone |
6 | 6 | from tenacity import retry, wait_exponential, stop_after_attempt, retry_if_exception_type |
| 7 | +from time import sleep |
7 | 8 | from urllib3.exceptions import MaxRetryError |
8 | 9 |
|
9 | 10 | from panelapp.models import PaLocusList, PaLocusListGene |
|
17 | 18 |
|
18 | 19 | REQUEST_TIMEOUT_S = 300 |
19 | 20 |
|
| 21 | +class TooManyRequestsError(Exception): |
| 22 | + pass |
20 | 23 |
|
21 | 24 | def import_all_panels(user, panel_app_api_url, label=None): |
22 | 25 | def _extract_ensembl_id_from_json(raw_gene_json): |
@@ -118,12 +121,15 @@ def _get_all_panels(panels_url, all_results): |
118 | 121 |
|
119 | 122 | def _get_all_genes(genes_url: str, results_by_panel_id: dict): |
120 | 123 | @retry( |
121 | | - retry=retry_if_exception_type(MaxRetryError), |
| 124 | + retry=retry_if_exception_type((MaxRetryError, TooManyRequestsError)), |
122 | 125 | wait=wait_exponential(multiplier=1, min=4, max=10), |
123 | 126 | stop=stop_after_attempt(5), |
124 | 127 | ) |
125 | 128 | def _get(url): |
126 | 129 | resp = requests.get(url, timeout=REQUEST_TIMEOUT_S) |
| 130 | + if resp.status_code == 429: |
| 131 | + sleep(10) |
| 132 | + raise TooManyRequestsError() |
127 | 133 | return resp.json() |
128 | 134 |
|
129 | 135 | resp_json = _get(genes_url) |
|
0 commit comments