Skip to content

Commit

Permalink
Add retry in connect update project call
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaslinhares committed Dec 20, 2024
1 parent 63328c9 commit 7879af0
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
2 changes: 2 additions & 0 deletions weni/internal/clients/connect.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import requests

from weni.internal.clients.base import BaseInternalClient
from weni.internal.clients.decorators import retry_on_exception
from weni.internal.models import Project


Expand Down Expand Up @@ -28,6 +29,7 @@ def create_recent_activity(

return response

@retry_on_exception()
def update_project(self, project: Project):
body = dict(
flow_organization=str(project.uuid),
Expand Down
62 changes: 62 additions & 0 deletions weni/internal/clients/decorators.py
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

0 comments on commit 7879af0

Please sign in to comment.