[API][ADMIN_API] Cycle DB servers if one fails
10 attempts at cycling servers before we give up, and log when one is failing Change-Id: Ib8ed63fb3eb1f0b2b4565783950a637a86ed02a4
This commit is contained in:
@@ -21,6 +21,7 @@ import random
|
|||||||
import time
|
import time
|
||||||
import ConfigParser
|
import ConfigParser
|
||||||
from pecan import conf
|
from pecan import conf
|
||||||
|
import logging
|
||||||
|
|
||||||
|
|
||||||
config = ConfigParser.SafeConfigParser()
|
config = ConfigParser.SafeConfigParser()
|
||||||
@@ -163,10 +164,23 @@ class RoutingSession(Session):
|
|||||||
class db_session(object):
|
class db_session(object):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.session = None
|
self.session = None
|
||||||
|
self.logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
self.session = sessionmaker(class_=RoutingSession)()
|
for x in xrange(10):
|
||||||
return self.session
|
try:
|
||||||
|
self.session = sessionmaker(class_=RoutingSession)()
|
||||||
|
self.session.execute("SELECT 1")
|
||||||
|
return self.session
|
||||||
|
except:
|
||||||
|
self.logger.error(
|
||||||
|
'Could not connect to DB server: {0}'.format(
|
||||||
|
RoutingSession.last_engine.url
|
||||||
|
)
|
||||||
|
)
|
||||||
|
RoutingSession.last_engine = None
|
||||||
|
self.logger.error('Could not connect to any DB server')
|
||||||
|
return None
|
||||||
|
|
||||||
def __exit__(self, type, value, traceback):
|
def __exit__(self, type, value, traceback):
|
||||||
self.session.close()
|
self.session.close()
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ import time
|
|||||||
import random
|
import random
|
||||||
import ConfigParser
|
import ConfigParser
|
||||||
from pecan import conf
|
from pecan import conf
|
||||||
|
import logging
|
||||||
|
|
||||||
|
|
||||||
config = ConfigParser.SafeConfigParser()
|
config = ConfigParser.SafeConfigParser()
|
||||||
@@ -163,10 +164,23 @@ class RoutingSession(Session):
|
|||||||
class db_session(object):
|
class db_session(object):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.session = None
|
self.session = None
|
||||||
|
self.logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
def __enter__(self):
|
def __enter__(self):
|
||||||
self.session = sessionmaker(class_=RoutingSession)()
|
for x in xrange(10):
|
||||||
return self.session
|
try:
|
||||||
|
self.session = sessionmaker(class_=RoutingSession)()
|
||||||
|
self.session.execute("SELECT 1")
|
||||||
|
return self.session
|
||||||
|
except:
|
||||||
|
self.logger.error(
|
||||||
|
'Could not connect to DB server: {0}'.format(
|
||||||
|
RoutingSession.last_engine.url
|
||||||
|
)
|
||||||
|
)
|
||||||
|
RoutingSession.last_engine = None
|
||||||
|
self.logger.error('Could not connect to any DB server')
|
||||||
|
return None
|
||||||
|
|
||||||
def __exit__(self, type, value, traceback):
|
def __exit__(self, type, value, traceback):
|
||||||
self.session.close()
|
self.session.close()
|
||||||
|
|||||||
Reference in New Issue
Block a user