annuaires_pg: reconnect si pg raccroche, une fois

Allô ?
This commit is contained in:
Daniel STAN 2013-10-29 20:07:02 +01:00
parent 9560cf0bf3
commit 667e065ed1

View file

@ -3,26 +3,36 @@
import psycopg2 import psycopg2
from functools import wraps from functools import wraps
import time
conn = None conn = None
def _need_conn(f): def _need_conn(f):
"""Décorateur à appliquer aux fonctions nécessitant une connexion pgsql""" """Décorateur à appliquer aux fonctions nécessitant une connexion pgsql"""
retries = 1
delay = 5
@wraps(f) @wraps(f)
def first_connect(*args, **kwargs): def first_connect(*args, **kwargs):
global conn global conn
if conn == None: attempts = 0
while not conn or not attempts:
if __name__.endswith('annuaires_pg_test'): if __name__.endswith('annuaires_pg_test'):
host='localhost' host='localhost'
else: else:
host='pgsql.adm.crans.org' host='pgsql.adm.crans.org'
# "connecting …" # "connecting …"
try: 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: 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 # Les scripts appelant annuaires_pg n'ont pas à connaître le
# backend pgsql. On utilise donc une exception plus standard # backend pgsql. On utilise donc une exception plus standard
raise NameError
return f(*args, **kwargs)
return first_connect return first_connect
bat_switchs = ["a", "b", "c", "g", "h", "i", "j", "m", "o", "p"] bat_switchs = ["a", "b", "c", "g", "h", "i", "j", "m", "o", "p"]