|
1 | 1 | import logging
|
2 | 2 | import re
|
3 | 3 | import subprocess
|
| 4 | +import time |
4 | 5 |
|
5 | 6 | import xarray as xr
|
6 | 7 |
|
@@ -134,33 +135,41 @@ def filter_download_script(wget_script_content, start_year, end_year):
|
134 | 135 | return "\n".join(modified_script)
|
135 | 136 |
|
136 | 137 |
|
137 |
| -# TODO add retry + logger so failure can be tracked |
138 |
| -def _download_vars_process(temp_download_path, search_results): |
139 |
| - temp_download_path.mkdir(parents=True, exist_ok=True) |
140 |
| - for result in search_results: |
141 |
| - file_context = result.file_context() |
142 |
| - wget_script_content = file_context.get_download_script() |
| 138 | +def _download_result(result, download_path, logger: logging.Logger = LOGGER): |
| 139 | + max_retries = 3 |
| 140 | + delay = 1 |
| 141 | + for attempt in range(1, max_retries + 1): |
| 142 | + try: |
| 143 | + file_context = result.file_context() |
| 144 | + wget_script_content = file_context.get_download_script() |
| 145 | + subprocess.run(["bash", "-c", wget_script_content, "download", "-s"], shell=False, cwd=download_path) |
| 146 | + except Exception as e: # pylint: disable=W0718 |
| 147 | + logger.error(f"Attempt {attempt} failed: {e}") |
| 148 | + if attempt < max_retries: |
| 149 | + time.sleep(delay) |
| 150 | + else: |
| 151 | + raise e |
143 | 152 |
|
144 |
| - # Optionally filter file list for download |
145 |
| - # if self.start_year is not None and self.end_year is not None: |
146 |
| - # wget_script_content = filter_download_script(wget_script_content, self.start_year, self.end_year) |
147 | 153 |
|
148 |
| - subprocess.run(["bash", "-c", wget_script_content, "download", "-s"], shell=False, cwd=temp_download_path) |
| 154 | +def _download_process(temp_download_path, search_results, logger: logging.Logger = LOGGER): |
| 155 | + temp_download_path.mkdir(parents=True, exist_ok=True) |
| 156 | + for result in search_results: |
| 157 | + _download_result(result=result, download_path=temp_download_path, logger=logger) |
149 | 158 |
|
150 | 159 |
|
151 | 160 | def download_raw_input_variable(institution_id, search_results, variable):
|
152 | 161 | temp_download_path = RAW_DATA / f"raw_input_vars/{institution_id}/{variable}"
|
153 |
| - _download_vars_process(temp_download_path, search_results) |
| 162 | + _download_process(temp_download_path, search_results) |
154 | 163 |
|
155 | 164 |
|
156 | 165 | def download_model_variable(model_id, search_results, variable):
|
157 | 166 | temp_download_path = RAW_DATA / f"model_vars/{model_id}/{variable}"
|
158 |
| - _download_vars_process(temp_download_path, search_results) |
| 167 | + _download_process(temp_download_path, search_results) |
159 | 168 |
|
160 | 169 |
|
161 | 170 | def download_metadata_variable(institution_id, search_results, variable):
|
162 | 171 | temp_download_path = RAW_DATA / f"meta_vars/{institution_id}/{variable}"
|
163 |
| - _download_vars_process(temp_download_path, search_results) |
| 172 | + _download_process(temp_download_path, search_results) |
164 | 173 |
|
165 | 174 |
|
166 | 175 | def get_grid_label(ctx, default_grid_label, logger=LOGGER):
|
|
0 commit comments