From a56af856b89b9e18030c02298f12e16387cabff4 Mon Sep 17 00:00:00 2001 From: Martin Konecny Date: Thu, 30 Aug 2012 15:52:47 -0400 Subject: [PATCH] CC-4322: Prevent pypo-notify from hanging if server becomes unresponsive -fixed --- python_apps/api_clients/api_client.py | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/python_apps/api_clients/api_client.py b/python_apps/api_clients/api_client.py index 7fb2de631..983b59281 100644 --- a/python_apps/api_clients/api_client.py +++ b/python_apps/api_clients/api_client.py @@ -71,7 +71,7 @@ class AirtimeApiClient(): self.logger.error('Error loading config file: %s', e) sys.exit(1) - def get_response_from_server(self, url): + def get_response_from_server(self, url, attempts=-1): logger = self.logger successful_response = False @@ -93,6 +93,14 @@ class AirtimeApiClient(): else: logger.debug(url) + #If the user passed in a positive attempts number then that means + #attempts will roll over 0 and we stop. If attempts was initially negative, + #then we have unlimited attempts + if attempts > 0: + attempts = attempts - 1 + if attempts == 0: + successful_response = True + if not successful_response: logger.error("Error connecting to server, waiting 5 seconds and trying again.") time.sleep(5) @@ -240,7 +248,7 @@ class AirtimeApiClient(): url = url.replace("%%api_key%%", self.config["api_key"]) - self.get_response_from_server(url) + self.get_response_from_server(url, attempts=5) except Exception, e: logger.error("Exception: %s", str(e)) @@ -259,7 +267,7 @@ class AirtimeApiClient(): logger.debug(url) url = url.replace("%%api_key%%", self.config["api_key"]) - response = self.get_response_from_server(url) + response = self.get_response_from_server(url, attempts = 5) response = json.loads(response) logger.info("API-Status %s", response['status']) logger.info("API-Message %s", response['message']) @@ -591,7 +599,7 @@ class AirtimeApiClient(): url = url.replace("%%stream_id%%", stream_id) url = url.replace("%%boot_time%%", time) - self.get_response_from_server(url) + self.get_response_from_server(url, attempts = 5) except Exception, e: logger.error("Exception: %s", e) @@ -604,7 +612,7 @@ class AirtimeApiClient(): url = url.replace("%%sourcename%%", sourcename) url = url.replace("%%status%%", status) - self.get_response_from_server(url) + self.get_response_from_server(url, attempts = 5) except Exception, e: logger.error("Exception: %s", e) @@ -725,6 +733,6 @@ class AirtimeApiClient(): self.logger.debug(url) request = urllib2.Request(url, data) - self.logger.info(self.get_response_from_server(request)) + self.logger.info(self.get_response_from_server(request, attempts = 5)) except Exception, e: self.logger.error("Exception: %s", e)