Skip to content

Commit 762a271

Browse files
committed
Implement retry in download functions
1 parent 56919c7 commit 762a271

File tree

1 file changed

+22
-13
lines changed

1 file changed

+22
-13
lines changed

climateset/download/utils.py

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import logging
22
import re
33
import subprocess
4+
import time
45

56
import xarray as xr
67

@@ -134,33 +135,41 @@ def filter_download_script(wget_script_content, start_year, end_year):
134135
return "\n".join(modified_script)
135136

136137

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
143152

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)
147153

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)
149158

150159

151160
def download_raw_input_variable(institution_id, search_results, variable):
152161
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)
154163

155164

156165
def download_model_variable(model_id, search_results, variable):
157166
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)
159168

160169

161170
def download_metadata_variable(institution_id, search_results, variable):
162171
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)
164173

165174

166175
def get_grid_label(ctx, default_grid_label, logger=LOGGER):

0 commit comments

Comments
 (0)