From bfdc9a56ede8a65d517349b6c9b59129a9011527 Mon Sep 17 00:00:00 2001 From: Conrad Date: Tue, 19 Dec 2017 10:42:48 -0500 Subject: [PATCH] Catch the OperationalError in redshift execute function --- app/lib/redshift_database.py | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/app/lib/redshift_database.py b/app/lib/redshift_database.py index f78c9a5..6c28752 100644 --- a/app/lib/redshift_database.py +++ b/app/lib/redshift_database.py @@ -38,19 +38,23 @@ class RedshiftWrapper(object): """ Execute a database call and handle the InterfaceError Returns the cursor that should be used to make future calls """ - try: - cursor.execute(sql, where_tuple) - except psycopg2.ProgrammingError as e: - self.current_app.logger.info( - 'ProgrammingError while executing redshift db call: {}'.format(e.message) - ) - self.db.rollback() - except psycopg2.InterfaceError as e: - self.current_app.logger.info( - 'InterfaceError while executing redshift db call: {}'.format(e.message) - ) - self.db = self.make_connection() - return self.get_cursor() + retry_counter = 0 + while retry_counter < 5: + retry_counter += 1 + try: + cursor.execute(sql, where_tuple) + break + except psycopg2.ProgrammingError as e: + self.current_app.logger.info( + 'ProgrammingError while executing redshift db call: {}'.format(e.message) + ) + self.db.rollback() + except (psycopg2.InterfaceError, psycopg2.OperationalError) as e: + self.current_app.logger.info( + 'Interface or Operational Error while executing redshift db call: {}'.format(e.message) + ) + self.db = self.make_connection() + cursor = self.get_cursor() return cursor -- GitLab