-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
148 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
"""Exceptions for Application.""" | ||
import logging | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class BaseAppException(Exception): | ||
"""Base Exception for App""" | ||
|
||
def __init__(self, message: str, view: str): | ||
"""Base Exception for App""" | ||
super().__init__(message) | ||
self.message = message | ||
self.view = view | ||
|
||
|
||
class AppActionException(BaseAppException): | ||
"""Exception for App Action.""" | ||
|
||
def __init__(self, message: str, view: str): | ||
"""Exception for App Action""" | ||
super().__init__(message, view) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import asyncio | ||
import logging | ||
import traceback | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def async_retry( | ||
retries: int = 1, parallel_executions: int = 1, catch_exceptions=(Exception,) | ||
): | ||
"""Async retry decorator. | ||
Examples: | ||
.. code-block:: python | ||
@async_retry(retries=3, parallel_executions=2) | ||
async def my_func(): | ||
# Some code that may raise exceptions | ||
pass | ||
Args: | ||
retries (int): Number of retries. | ||
parallel_executions (int): Number of parallel executions. | ||
catch_exceptions (tuple): Tuple of exceptions to catch. | ||
""" | ||
|
||
def decorator(func): | ||
async def wrapper(*args, **kwargs): | ||
last_exception = None | ||
for attempt in range(retries): | ||
tasks = [func(*args, **kwargs) for _ in range(parallel_executions)] | ||
results = await asyncio.gather(*tasks, return_exceptions=True) | ||
|
||
for result in results: | ||
if not isinstance(result, Exception): | ||
return result | ||
if isinstance(result, catch_exceptions): | ||
last_exception = result | ||
logger.error( | ||
f"Attempt {attempt + 1} of {retries} failed with error: " | ||
f"{type(result).__name__}, {str(result)}" | ||
) | ||
logger.debug(traceback.format_exc()) | ||
|
||
logger.info(f"Retrying... (Attempt {attempt + 1} of {retries})") | ||
|
||
raise last_exception # After all retries, raise the last caught exception | ||
|
||
return wrapper | ||
|
||
return decorator |