|
18 | 18 | from pydantic import AnyUrl |
19 | 19 | from servicelib.utils import logged_gather |
20 | 20 | from tenacity._asyncio import AsyncRetrying |
| 21 | +from tenacity.after import after_log |
21 | 22 | from tenacity.before_sleep import before_sleep_log |
22 | 23 | from tenacity.retry import retry_if_exception_type |
23 | 24 | from tenacity.stop import stop_after_attempt |
@@ -105,7 +106,8 @@ async def download_link_to_file( |
105 | 106 | wait=wait_exponential(min=1, max=10), |
106 | 107 | stop=stop_after_attempt(num_retries), |
107 | 108 | retry=retry_if_exception_type(ClientConnectionError), |
108 | | - before_sleep=before_sleep_log(log, logging.WARNING), |
| 109 | + before_sleep=before_sleep_log(log, logging.WARNING, exc_info=True), |
| 110 | + after=after_log(log, log_level=logging.ERROR), |
109 | 111 | ): |
110 | 112 | with attempt: |
111 | 113 | async with session.get(url) as response: |
@@ -165,32 +167,33 @@ async def _upload_file_part( |
165 | 167 | wait=wait_exponential(min=1, max=10), |
166 | 168 | stop=stop_after_attempt(num_retries), |
167 | 169 | retry=retry_if_exception_type(ClientConnectionError), |
168 | | - before_sleep=before_sleep_log(log, logging.WARNING), |
| 170 | + before_sleep=before_sleep_log(log, logging.WARNING, exc_info=True), |
| 171 | + after=after_log(log, log_level=logging.ERROR), |
169 | 172 | ): |
170 | 173 | with attempt: |
171 | | - response = await session.put( |
| 174 | + async with session.put( |
172 | 175 | upload_url, |
173 | 176 | data=file_uploader, |
174 | 177 | headers={ |
175 | 178 | "Content-Length": f"{file_part_size}", |
176 | 179 | }, |
177 | | - ) |
178 | | - response.raise_for_status() |
179 | | - if pbar.update(file_part_size) and io_log_redirect_cb: |
180 | | - await io_log_redirect_cb(f"{pbar}") |
181 | | - # NOTE: the response from minio does not contain a json body |
182 | | - assert response.status == web.HTTPOk.status_code # nosec |
183 | | - assert response.headers # nosec |
184 | | - assert "Etag" in response.headers # nosec |
185 | | - received_e_tag = json.loads(response.headers["Etag"]) |
186 | | - log.info( |
187 | | - "--> completed upload %s of %s, [%s], %s", |
188 | | - f"{file_part_size=}", |
189 | | - f"{file_to_upload=}", |
190 | | - f"{part_index+1}/{num_parts}", |
191 | | - f"{received_e_tag=}", |
192 | | - ) |
193 | | - return (part_index, received_e_tag) |
| 180 | + ) as response: |
| 181 | + response.raise_for_status() |
| 182 | + if pbar.update(file_part_size) and io_log_redirect_cb: |
| 183 | + await io_log_redirect_cb(f"{pbar}") |
| 184 | + # NOTE: the response from minio does not contain a json body |
| 185 | + assert response.status == web.HTTPOk.status_code # nosec |
| 186 | + assert response.headers # nosec |
| 187 | + assert "Etag" in response.headers # nosec |
| 188 | + received_e_tag = json.loads(response.headers["Etag"]) |
| 189 | + log.info( |
| 190 | + "--> completed upload %s of %s, [%s], %s", |
| 191 | + f"{file_part_size=}", |
| 192 | + f"{file_to_upload=}", |
| 193 | + f"{part_index+1}/{num_parts}", |
| 194 | + f"{received_e_tag=}", |
| 195 | + ) |
| 196 | + return (part_index, received_e_tag) |
194 | 197 | raise exceptions.S3TransferError( |
195 | 198 | f"Unexpected error while transferring {file_to_upload} to {upload_url}" |
196 | 199 | ) |
|
0 commit comments