annuaires_pg.py: abandon plus rapide
This commit is contained in:
parent
b88b72c49a
commit
0428bfb106
1 changed files with 11 additions and 4 deletions
|
@ -5,13 +5,17 @@ from functools import wraps
|
|||
import time
|
||||
|
||||
conn = None
|
||||
# : échec définitif, on raise une exception direct
|
||||
def_failed = False
|
||||
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
|
||||
global conn, def_failed
|
||||
if def_failed:
|
||||
raise NameError
|
||||
attempts = 0
|
||||
while not conn or not attempts:
|
||||
if __name__.endswith('annuaires_pg_test'):
|
||||
|
@ -21,14 +25,17 @@ def _need_conn(f):
|
|||
# "connecting …"
|
||||
try:
|
||||
if not conn:
|
||||
if attempts:
|
||||
# Attend un peu avant de reessayer
|
||||
time.sleep(delay)
|
||||
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:
|
||||
conn = None # Connexion morte, on recommence à zéro
|
||||
if attempts > retries:
|
||||
def_failed = True
|
||||
raise NameError
|
||||
# Les scripts appelant annuaires_pg n'ont pas à connaître le
|
||||
# backend pgsql. On utilise donc une exception plus standard
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue