From 192da034c3697280b8b2ff9d0fcf987baae8d927 Mon Sep 17 00:00:00 2001 From: dim Date: Fri, 15 Feb 2008 09:53:06 +0000 Subject: [PATCH] Handle more nicely connection errors - from python stacktrace to ERROR message --- pgloader.py | 6 +----- pgloader/db.py | 22 ++++++++++++++-------- pgloader/pgloader.py | 7 ++++--- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/pgloader.py b/pgloader.py index 8d7d1f3..59866e8 100644 --- a/pgloader.py +++ b/pgloader.py @@ -465,7 +465,6 @@ def load_data(): threads = {} current = 0 interrupted = False - got_errors = False max_running = MAX_PARALLEL_SECTIONS if max_running == -1: @@ -493,8 +492,6 @@ def load_data(): summary.pop(s) except PGLoader_Error, e: - got_errors = True - if e == '': log.error('[%s] Please correct previous errors' % s) else: @@ -506,7 +503,6 @@ def load_data(): pass except UnicodeDecodeError, e: - got_errors = True log.error("can't open '%s' with given input encoding '%s'" \ % (filename, input_encoding)) @@ -534,7 +530,7 @@ def load_data(): td = time.time() - begin retcode = 0 - if SUMMARY and not interrupted and not got_errors: + if SUMMARY and not interrupted: try: retcode = print_summary(None, sections, summary, td) print diff --git a/pgloader/db.py b/pgloader/db.py index ff272eb..d6c0ef9 100644 --- a/pgloader/db.py +++ b/pgloader/db.py @@ -119,16 +119,22 @@ class db: self.first_commit_time = self.last_commit_time self.partial_coldef = None - if self.dbconn is not None: - self.log.debug('Debug: closing current connection') - self.dbconn.close() + try: + if self.dbconn is not None: + self.log.debug('Debug: closing current connection') + self.dbconn.close() - self.log.debug('Debug: connecting to dns %s', self.dsn) + self.log.debug('Debug: connecting to dns %s', self.dsn) - self.dbconn = psycopg.connect(self.dsn) - self.set_encoding() - self.set_datestyle() - self.set_lc_messages() + self.dbconn = psycopg.connect(self.dsn) + self.set_encoding() + self.set_datestyle() + self.set_lc_messages() + + except psycopg.OperationalError, e: + # e.g. too many connections + self.log.error(e) + raise PGLoader_Error, "Can't connect to database" def print_stats(self): """ output some stats about recent activity """ diff --git a/pgloader/pgloader.py b/pgloader/pgloader.py index fe0b344..0d4133b 100644 --- a/pgloader/pgloader.py +++ b/pgloader/pgloader.py @@ -134,8 +134,8 @@ class PGLoader(threading.Thread): # Now reset database connection if not DRY_RUN: - self.db.log = self.log - self.db.reset() + self.db.log = self.log + self.db.reset() if not self.template and not DRY_RUN: # check we have properly configured the copy separator @@ -1015,7 +1015,8 @@ class PGLoader(threading.Thread): k = threads.keys() for c in range(self.section_threads): - self.log.debug("locks[%d].acquire to set %s.done = True" % (c, k[c])) + self.log.debug("locks[%d].acquire to set %s.done = True" \ + % (c, k[c])) locks[c].acquire() threads[k[c]].done = True