From 667e065ed122a8e7e958b28d6bba33c10e55253b Mon Sep 17 00:00:00 2001 From: Daniel STAN Date: Tue, 29 Oct 2013 20:07:02 +0100 Subject: [PATCH] annuaires_pg: reconnect si pg raccroche, une fois MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Allô ? --- gestion/annuaires_pg.py | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/gestion/annuaires_pg.py b/gestion/annuaires_pg.py index 7766f655..848d530a 100755 --- a/gestion/annuaires_pg.py +++ b/gestion/annuaires_pg.py @@ -3,26 +3,36 @@ import psycopg2 from functools import wraps +import time conn = None def _need_conn(f): """Décorateur à appliquer aux fonctions nécessitant une connexion pgsql""" + retries = 1 + delay = 5 @wraps(f) def first_connect(*args, **kwargs): global conn - if conn == None: + attempts = 0 + while not conn or not attempts: if __name__.endswith('annuaires_pg_test'): host='localhost' else: host='pgsql.adm.crans.org' # "connecting …" try: - conn = psycopg2.connect(user='crans', database='switchs', host=host) + if not conn: + conn = psycopg2.connect(user='crans', database='switchs', + host=host) + return f(*args, **kwargs) except psycopg2.OperationalError: + attempts += 1 + conn = None + time.sleep(delay) + if attempts >= retries: + raise NameError # Les scripts appelant annuaires_pg n'ont pas à connaître le # backend pgsql. On utilise donc une exception plus standard - raise NameError - return f(*args, **kwargs) return first_connect bat_switchs = ["a", "b", "c", "g", "h", "i", "j", "m", "o", "p"]