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
|
import time
|
||||||
|
|
||||||
conn = None
|
conn = None
|
||||||
|
# : échec définitif, on raise une exception direct
|
||||||
|
def_failed = False
|
||||||
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
|
retries = 1
|
||||||
delay = 5
|
delay = 5
|
||||||
@wraps(f)
|
@wraps(f)
|
||||||
def first_connect(*args, **kwargs):
|
def first_connect(*args, **kwargs):
|
||||||
global conn
|
global conn, def_failed
|
||||||
|
if def_failed:
|
||||||
|
raise NameError
|
||||||
attempts = 0
|
attempts = 0
|
||||||
while not conn or not attempts:
|
while not conn or not attempts:
|
||||||
if __name__.endswith('annuaires_pg_test'):
|
if __name__.endswith('annuaires_pg_test'):
|
||||||
|
@ -21,14 +25,17 @@ def _need_conn(f):
|
||||||
# "connecting …"
|
# "connecting …"
|
||||||
try:
|
try:
|
||||||
if not conn:
|
if not conn:
|
||||||
|
if attempts:
|
||||||
|
# Attend un peu avant de reessayer
|
||||||
|
time.sleep(delay)
|
||||||
conn = psycopg2.connect(user='crans', database='switchs',
|
conn = psycopg2.connect(user='crans', database='switchs',
|
||||||
host=host)
|
host=host)
|
||||||
return f(*args, **kwargs)
|
return f(*args, **kwargs)
|
||||||
except psycopg2.OperationalError:
|
except psycopg2.OperationalError:
|
||||||
attempts += 1
|
attempts += 1
|
||||||
conn = None
|
conn = None # Connexion morte, on recommence à zéro
|
||||||
time.sleep(delay)
|
if attempts > retries:
|
||||||
if attempts >= retries:
|
def_failed = True
|
||||||
raise NameError
|
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
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue