Skip to content

Commit 1693600

Browse files
author
Jennings Zhang
committed
Retry download failutres
1 parent 3ba8262 commit 1693600

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

caw/movedata.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,19 +95,33 @@ def __calculate_target(remote_file: DownloadableFile) -> Tuple[Path, Downloadabl
9595
with typer.progressbar(length=len(to_download), label='Downloading files', file=sys.stderr) as progress:
9696
with ThreadPoolExecutor(max_workers=threads) as pool:
9797

98-
def download_file(t: Tuple[Path, DownloadableFile]) -> int:
98+
def download_file(t: Tuple[Path, DownloadableFile], attempt=0) -> int:
9999
"""
100-
Download file and move the progress bar
100+
Download file and move the progress bar. Retries on failure, shuts down the thread pool
101+
if giving up.
102+
101103
:return: downloaded file size
102104
"""
103105
target, remote_file = t
106+
107+
if attempt >= 3:
108+
typer.secho(f'Failed 3 attempts to download {remote_file.file_resource}. '
109+
f'Giving up...', fg=typer.colors.RED, err=True)
110+
pool.shutdown(cancel_futures=True) # fail fast
111+
raise typer.Abort()
112+
104113
try:
105114
remote_file.download(target)
106115
except requests.exceptions.RequestException as e:
107-
typer.secho(f'Failed to download {remote_file.file_resource}: {str(e)}',
116+
typer.secho(f'attempt={attempt} ::: '
117+
f'failed to download {remote_file.file_resource}: {str(e)}',
108118
fg=typer.colors.RED, err=True)
109-
pool.shutdown(cancel_futures=True) # fail fast
110-
raise typer.Abort()
119+
return download_file(t, attempt + 1)
120+
121+
if attempt > 0:
122+
typer.secho(f'Successfully retried: {remote_file.file_resource}',
123+
color=typer.colors.GREEN, err=True)
124+
111125
progress.update(1)
112126
return target.stat().st_size
113127

0 commit comments

Comments
 (0)