Skip to content

Commit

Permalink
Retry download failutres
Browse files Browse the repository at this point in the history
  • Loading branch information
jennydaman committed Mar 14, 2022
1 parent 3ba8262 commit 1693600
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions caw/movedata.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,19 +95,33 @@ def __calculate_target(remote_file: DownloadableFile) -> Tuple[Path, Downloadabl
with typer.progressbar(length=len(to_download), label='Downloading files', file=sys.stderr) as progress:
with ThreadPoolExecutor(max_workers=threads) as pool:

def download_file(t: Tuple[Path, DownloadableFile]) -> int:
def download_file(t: Tuple[Path, DownloadableFile], attempt=0) -> int:
"""
Download file and move the progress bar
Download file and move the progress bar. Retries on failure, shuts down the thread pool
if giving up.
:return: downloaded file size
"""
target, remote_file = t

if attempt >= 3:
typer.secho(f'Failed 3 attempts to download {remote_file.file_resource}. '
f'Giving up...', fg=typer.colors.RED, err=True)
pool.shutdown(cancel_futures=True) # fail fast
raise typer.Abort()

try:
remote_file.download(target)
except requests.exceptions.RequestException as e:
typer.secho(f'Failed to download {remote_file.file_resource}: {str(e)}',
typer.secho(f'attempt={attempt} ::: '
f'failed to download {remote_file.file_resource}: {str(e)}',
fg=typer.colors.RED, err=True)
pool.shutdown(cancel_futures=True) # fail fast
raise typer.Abort()
return download_file(t, attempt + 1)

if attempt > 0:
typer.secho(f'Successfully retried: {remote_file.file_resource}',
color=typer.colors.GREEN, err=True)

progress.update(1)
return target.stat().st_size

Expand Down

0 comments on commit 1693600

Please sign in to comment.