Skip to content

Commit 87fc3e7

Browse files
GitHKAndrei Neagu
andauthored
🐛 Refactored retry logic to include failing case in AWS master (#7809)
Co-authored-by: Andrei Neagu <[email protected]>
1 parent 6748f5e commit 87fc3e7

File tree

1 file changed

+30
-24
lines changed

1 file changed

+30
-24
lines changed

packages/simcore-sdk/src/simcore_sdk/node_ports_common/file_io_utils.py

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from aiohttp import (
1111
ClientConnectionError,
1212
ClientError,
13-
ClientPayloadError,
1413
ClientResponse,
1514
ClientResponseError,
1615
ClientSession,
@@ -32,7 +31,11 @@
3231
from tenacity.after import after_log
3332
from tenacity.asyncio import AsyncRetrying
3433
from tenacity.before_sleep import before_sleep_log
35-
from tenacity.retry import retry_if_exception, retry_if_exception_type
34+
from tenacity.retry import (
35+
retry_if_exception,
36+
retry_if_exception_type,
37+
retry_if_not_exception_type,
38+
)
3639
from tenacity.stop import stop_after_attempt
3740
from tenacity.wait import wait_exponential
3841
from tqdm import tqdm
@@ -154,7 +157,7 @@ async def _file_chunk_writer(
154157
progress_bar: ProgressBarData,
155158
):
156159
async with aiofiles.open(file, "wb") as file_pointer:
157-
while chunk := await response.content.read(CHUNK_SIZE):
160+
async for chunk in response.content.iter_chunked(CHUNK_SIZE):
158161
await file_pointer.write(chunk)
159162
if io_log_redirect_cb and pbar.update(len(chunk)):
160163
with log_catch(_logger, reraise=False):
@@ -181,25 +184,28 @@ async def download_link_to_file(
181184
progress_bar: ProgressBarData,
182185
):
183186
_logger.debug("Downloading from %s to %s", url, file_path)
184-
async for attempt in AsyncRetrying(
185-
reraise=True,
186-
wait=wait_exponential(min=1, max=10),
187-
stop=stop_after_attempt(num_retries),
188-
retry=retry_if_exception_type(ClientConnectionError),
189-
before_sleep=before_sleep_log(_logger, logging.WARNING, exc_info=True),
190-
after=after_log(_logger, log_level=logging.ERROR),
191-
):
192-
with attempt:
193-
async with AsyncExitStack() as stack:
194-
response = await stack.enter_async_context(session.get(url))
195-
if response.status == status.HTTP_404_NOT_FOUND:
196-
raise exceptions.InvalidDownloadLinkError(url)
197-
if response.status > _VALID_HTTP_STATUS_CODES:
198-
raise exceptions.TransferError(url)
199-
file_path.parent.mkdir(parents=True, exist_ok=True)
200-
# SEE https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Length
201-
file_size = int(response.headers.get("Content-Length", 0)) or None
202-
try:
187+
try:
188+
async for attempt in AsyncRetrying(
189+
reraise=True,
190+
wait=wait_exponential(min=1, max=10),
191+
stop=stop_after_attempt(num_retries),
192+
retry=retry_if_not_exception_type(
193+
(exceptions.InvalidDownloadLinkError, exceptions.TransferError)
194+
),
195+
before_sleep=before_sleep_log(_logger, logging.WARNING, exc_info=True),
196+
after=after_log(_logger, log_level=logging.ERROR),
197+
):
198+
with attempt:
199+
async with AsyncExitStack() as stack:
200+
response = await stack.enter_async_context(session.get(url))
201+
if response.status == status.HTTP_404_NOT_FOUND:
202+
raise exceptions.InvalidDownloadLinkError(url)
203+
if response.status > _VALID_HTTP_STATUS_CODES:
204+
raise exceptions.TransferError(url)
205+
file_path.parent.mkdir(parents=True, exist_ok=True)
206+
# SEE https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Length
207+
file_size = int(response.headers.get("Content-Length", 0)) or None
208+
203209
tqdm_progress = stack.enter_context(
204210
tqdm_logging_redirect(
205211
desc=f"downloading {url.path} --> {file_path.name}\n",
@@ -231,8 +237,8 @@ async def download_link_to_file(
231237
sub_progress,
232238
)
233239
_logger.debug("Download complete")
234-
except ClientPayloadError as exc:
235-
raise exceptions.TransferError(url) from exc
240+
except Exception as exc:
241+
raise exceptions.TransferError(url) from exc
236242

237243

238244
def _check_for_aws_http_errors(exc: BaseException) -> bool:

0 commit comments

Comments
 (0)