Reconnect to libvirt service if previous connection is lost

re-trying a requiest doesn't help in cases when chached connection
was closed.

- do not cache the connection in a property
- check conn.isAlive() each time when already existing connection
  is picked

Closes-Bug:#1639788
Change-Id: I23c65162e32c97a03fc2950c41d13b83f350fee2
This commit is contained in:
Dennis Dmitriev
2016-11-04 15:41:59 +02:00
parent 01bb266e85
commit b5a5886c37

View File

@@ -57,11 +57,17 @@ class _LibvirtManager(object):
:type connection_string: str
"""
if connection_string not in self.connections:
conn = libvirt.open(connection_string)
self.connections[connection_string] = conn
else:
if connection_string in self.connections:
conn = self.connections[connection_string]
if conn.isAlive():
# Use a cached connection only if it is alive
return conn
else:
logger.error("Connection to libvirt '{0}' is broken, create a"
" new connection".format(connection_string))
# Create a new connection
conn = libvirt.open(connection_string)
self.connections[connection_string] = conn
return conn
def _error_handler(self, error):
@@ -218,7 +224,7 @@ class LibvirtDriver(driver.Driver):
_device_name_generators = {}
@functional.cached_property
@property
def conn(self):
"""Connection to libvirt api"""
# noinspection PyTypeChecker