Greg Padgett has uploaded a new change for review. Change subject: client: use contextlib to manage broker connections ......................................................................
client: use contextlib to manage broker connections Change-Id: I67e0beb40a4b56612469a845d7371cd9cdb4a8cd Signed-off-by: Greg Padgett <[email protected]> --- M ovirt_hosted_engine_ha/client/client.py M ovirt_hosted_engine_ha/lib/brokerlink.py 2 files changed, 14 insertions(+), 5 deletions(-) git pull ssh://gerrit.ovirt.org:29418/ovirt-hosted-engine-ha refs/changes/09/19709/1 diff --git a/ovirt_hosted_engine_ha/client/client.py b/ovirt_hosted_engine_ha/client/client.py index b1f0c95..e9b703b 100644 --- a/ovirt_hosted_engine_ha/client/client.py +++ b/ovirt_hosted_engine_ha/client/client.py @@ -48,11 +48,10 @@ if self._config is None: self._config = config.Config() broker = brokerlink.BrokerLink() - broker.connect() - stats = broker.get_stats_from_storage( - path.get_metadata_path(self._config), - constants.SERVICE_TYPE) - broker.disconnect() + with broker.connection(): + stats = broker.get_stats_from_storage( + path.get_metadata_path(self._config), + constants.SERVICE_TYPE) output = {} for host_str, data in stats.iteritems(): diff --git a/ovirt_hosted_engine_ha/lib/brokerlink.py b/ovirt_hosted_engine_ha/lib/brokerlink.py index ee2749c..f8b9cea 100644 --- a/ovirt_hosted_engine_ha/lib/brokerlink.py +++ b/ovirt_hosted_engine_ha/lib/brokerlink.py @@ -18,6 +18,7 @@ # import base64 +import contextlib import logging import socket @@ -59,6 +60,15 @@ finally: self._socket = None + @contextlib.contextmanager + def connection(self): + was_connected = self.is_connected() + if not was_connected: + self.connect() + yield + if not was_connected: + self.disconnect() + def start_monitor(self, type, options): """ Starts a monitor of the specified type in the ha broker using the -- To view, visit http://gerrit.ovirt.org/19709 To unsubscribe, visit http://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I67e0beb40a4b56612469a845d7371cd9cdb4a8cd Gerrit-PatchSet: 1 Gerrit-Project: ovirt-hosted-engine-ha Gerrit-Branch: master Gerrit-Owner: Greg Padgett <[email protected]> _______________________________________________ Engine-patches mailing list [email protected] http://lists.ovirt.org/mailman/listinfo/engine-patches
