From b5a5886c37d1aeda37e7de5553708fc76639bfe2 Mon Sep 17 00:00:00 2001 From: Dennis Dmitriev Date: Fri, 4 Nov 2016 15:41:59 +0200 Subject: [PATCH] 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 --- devops/driver/libvirt/libvirt_driver.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/devops/driver/libvirt/libvirt_driver.py b/devops/driver/libvirt/libvirt_driver.py index d50135ce..b924a800 100644 --- a/devops/driver/libvirt/libvirt_driver.py +++ b/devops/driver/libvirt/libvirt_driver.py @@ -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