-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add retry in connect update project call
- Loading branch information
1 parent
63328c9
commit b4180b9
Showing
2 changed files
with
64 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import time | ||
import functools | ||
import logging | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def retry_on_exception(max_attempts=8, start_sleep_time=1, factor=2): | ||
def decorator_retry(func): | ||
@functools.wraps(func) | ||
def wrapper(*args, **kwargs): | ||
attempts, sleep_time = 0, start_sleep_time | ||
last_exception = "" | ||
while attempts < max_attempts: | ||
try: | ||
return func(*args, **kwargs) | ||
except Exception as e: | ||
last_exception = e | ||
status_code = e.status_code if hasattr(e, "status_code") else None | ||
if not status_code: | ||
print(f"Unexpected error: [{str(e)}]") | ||
logger.error(e) | ||
|
||
if status_code == 404: | ||
print(f"Not Found: {str(e)}. Not retrying this.") | ||
raise | ||
elif status_code == 500: | ||
print(f"A 500 error occurred: {str(e)}. Retrying...") | ||
raise | ||
|
||
if attempts >= 5: | ||
if status_code == 429: | ||
print(f"Too many requests: {str(e)}. Retrying...") | ||
elif status_code == 408: | ||
print(f"Timeout error: {str(e)}. Retrying...") | ||
else: | ||
print( | ||
f"Unexpected error: [{str(e)}]. status: {status_code}" | ||
) | ||
logger.error(e) | ||
|
||
if attempts >= 5: | ||
print( | ||
f"Retrying... Attempt {attempts + 1} after {sleep_time} seconds, in {func.__name__}:" | ||
) | ||
|
||
time.sleep(sleep_time) | ||
attempts += 1 | ||
sleep_time *= factor | ||
|
||
message = ( | ||
f"Rate limit exceeded, max retry attempts reached. Last error in {func.__name__}:" | ||
f"Last error:{last_exception}, after {attempts} attempts." | ||
) | ||
|
||
print(message) | ||
logger.error(message) | ||
|
||
return wrapper | ||
|
||
return decorator_retry |