I have a python code inside a micro-service which I need to publish and/or post to a cloud service. For now I have put this publish/post inside try/except to log the failure of this publish or post:
def func(self,data):
my_message = do_something(data)
try:
self.my_obj.publish(my_message)
return True
except Exception as e:
logging.error(f"publish failed with error {e}")
return False
But what I actually need is to retry using exponential back-off retry mechanism for e.g. 30 seconds and the same time log the failure each time it fails.
I tried to use retrying package by installing and importing it and then moved the publish call inside another private function and use a decorator on that function as follows:
from retrying import retry
@retry(wait_exponential_multiplier=1000, wait_exponential_max=30000)
def __my_push(self,my_message):
self.my_obj.publish(my_message)
return True
Now, I have three issues/questions. The first one is that when it fails and retries in exponential manner, it keeps trying to repeat even if it exceeds the max wait of 30 second specified in decorator. I want to stop to do that after that limit and return False. Second thing I do not know how to return False on failure after this limit. My third question is how to add logging the exception each time it happens? If I put try except inside this function to log the exception, then retry decorator would work? I am not sure how to achieve this? If I can achieve this goal with other libraries for exponential back-off retry? I would do that. Can you give me a simple example to achieve this goal? Thanks in advance