|
31 | 31 | from sqlalchemy.dialects.postgresql import insert as pg_insert |
32 | 32 | from sqlalchemy.sql.expression import literal_column |
33 | 33 | from tenacity import retry |
| 34 | +from tenacity._asyncio import AsyncRetrying |
34 | 35 | from tenacity.before_sleep import before_sleep_log |
35 | | -from tenacity.retry import retry_if_exception_type, retry_if_result |
| 36 | +from tenacity.retry import retry_if_exception_type |
36 | 37 | from tenacity.stop import stop_after_delay |
37 | 38 | from tenacity.wait import wait_exponential |
38 | 39 | from yarl import URL |
@@ -234,6 +235,7 @@ async def list_files( |
234 | 235 | dex.fmd.file_uuid, |
235 | 236 | dex.fmd.bucket_name, |
236 | 237 | dex.fmd.object_name, |
| 238 | + reraise_exceptions=False, |
237 | 239 | ) |
238 | 240 | if dex: |
239 | 241 | data.append(dex) |
@@ -410,6 +412,7 @@ async def list_file( |
410 | 412 | file_metadata.fmd.file_uuid, |
411 | 413 | file_metadata.fmd.bucket_name, |
412 | 414 | file_metadata.fmd.object_name, |
| 415 | + reraise_exceptions=False, |
413 | 416 | ) |
414 | 417 | return file_metadata |
415 | 418 | # FIXME: returns None in both cases: file does not exist or use has no access |
@@ -441,7 +444,8 @@ async def try_update_database_from_storage( |
441 | 444 | file_uuid: str, |
442 | 445 | bucket_name: str, |
443 | 446 | object_name: str, |
444 | | - silence_exception: bool = False, |
| 447 | + *, |
| 448 | + reraise_exceptions: bool, |
445 | 449 | ) -> Optional[FileMetaDataEx]: |
446 | 450 | try: |
447 | 451 | async with self._create_aiobotocore_client_context() as aioboto_client: |
@@ -472,29 +476,23 @@ async def try_update_database_from_storage( |
472 | 476 |
|
473 | 477 | return to_meta_data_extended(row) |
474 | 478 | except botocore.exceptions.ClientError: |
475 | | - if silence_exception: |
476 | | - logger.debug("Error happened while trying to access %s", file_uuid) |
477 | | - else: |
478 | | - logger.warning( |
479 | | - "Error happened while trying to access %s", file_uuid, exc_info=True |
480 | | - ) |
481 | | - # the file is not existing or some error happened |
482 | | - return None |
| 479 | + logger.warning("Error happened while trying to access %s", file_uuid) |
| 480 | + if reraise_exceptions: |
| 481 | + raise |
483 | 482 |
|
484 | | - @retry( |
485 | | - stop=stop_after_delay(1 * _HOUR), |
486 | | - wait=wait_exponential(multiplier=0.1, exp_base=1.2, max=30), |
487 | | - retry=( |
488 | | - retry_if_exception_type() | retry_if_result(lambda result: result is None) |
489 | | - ), |
490 | | - before_sleep=before_sleep_log(logger, logging.INFO), |
491 | | - ) |
492 | 483 | async def auto_update_database_from_storage_task( |
493 | 484 | self, file_uuid: str, bucket_name: str, object_name: str |
494 | 485 | ) -> Optional[FileMetaDataEx]: |
495 | | - return await self.try_update_database_from_storage( |
496 | | - file_uuid, bucket_name, object_name, silence_exception=True |
497 | | - ) |
| 486 | + async for attempt in AsyncRetrying( |
| 487 | + stop=stop_after_delay(1 * _HOUR), |
| 488 | + wait=wait_exponential(multiplier=0.1, exp_base=1.2, max=30), |
| 489 | + retry=(retry_if_exception_type()), |
| 490 | + before_sleep=before_sleep_log(logger, logging.INFO), |
| 491 | + ): |
| 492 | + with attempt: |
| 493 | + return await self.try_update_database_from_storage( |
| 494 | + file_uuid, bucket_name, object_name, reraise_exceptions=True |
| 495 | + ) |
498 | 496 |
|
499 | 497 | async def update_metadata( |
500 | 498 | self, file_uuid: str, user_id: int |
|
0 commit comments