From 09d7d98ae3d60143e363eca60e3aab81cad869fc Mon Sep 17 00:00:00 2001 From: Fabio Manganiello Date: Sun, 19 Aug 2018 21:51:59 +0000 Subject: [PATCH] Retry logic for Mercury API --- .../backend/http/request/rss/__init__.py | 29 +++++++++++++++---- 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/platypush/backend/http/request/rss/__init__.py b/platypush/backend/http/request/rss/__init__.py index 86e91282f..aac0198e3 100644 --- a/platypush/backend/http/request/rss/__init__.py +++ b/platypush/backend/http/request/rss/__init__.py @@ -65,13 +65,30 @@ class RssUpdates(HttpRequest): def _parse_entry_content(self, link): response = None + err = None + n_tries = 5 - try: - self.logger.info('Parsing content for {}'.format(link)) - response = requests.get('https://mercury.postlight.com/parser', - params = {'url': link}, - headers = {'x-api-key': self.mercury_api_key }) - except Exception as e: self.logger.exception(e) + for _ in range(0, n_tries): + try: + self.logger.info('Parsing content for {}'.format(link)) + response = requests.get('https://mercury.postlight.com/parser', + params = {'url': link}, + headers = {'x-api-key': self.mercury_api_key }) + except Exception as e: + err = e + + if response.text: + err = None + break + else: + time.sleep(1) + + if err: + raise err + + if not response.text: + raise RuntimeError("No response from Mercury API for URL {} after {} tries" + .format(link, n_tries)) return response.json()['content'] if response and response.ok else None