From 5ea4484a2ca2b323ddb3c1561296be18ea80a16d Mon Sep 17 00:00:00 2001 From: Russell Haering Date: Wed, 22 Jan 2014 17:28:24 -0800 Subject: [PATCH] cache a global hardware manager --- teeth_agent/agent.py | 4 ++-- teeth_agent/hardware.py | 34 ++++++++++++++++++++-------------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/teeth_agent/agent.py b/teeth_agent/agent.py index 718f2b436..efad3d12e 100644 --- a/teeth_agent/agent.py +++ b/teeth_agent/agent.py @@ -118,7 +118,7 @@ class TeethAgent(object): self.api = api.TeethAgentAPIServer(self) self.command_results = collections.OrderedDict() self.heartbeater = TeethAgentHeartbeater(self) - self.hardware = hardware.load_hardware_manager() + self.hardware = hardware.get_manager() self.command_lock = threading.Lock() self.log = structlog.get_logger() self.started_at = None @@ -235,7 +235,7 @@ def build_agent(api_url, if not listen_host: listen_host = advertise_host - mac_addr = hardware.load_hardware_manager().get_primary_mac_address() + mac_addr = hardware.get_manager().get_primary_mac_address() api_client = overlord_agent_api.APIClient(api_url) log.info('fetching agent configuration from API', diff --git a/teeth_agent/hardware.py b/teeth_agent/hardware.py index 5dfc56940..b747180cd 100644 --- a/teeth_agent/hardware.py +++ b/teeth_agent/hardware.py @@ -59,20 +59,26 @@ def _compare_extensions(ext1, ext2): return mgr1.evaluate_hardware_support() - mgr2.evaluate_hardware_support() -def load_hardware_manager(): - log = structlog.get_logger() - extension_manager = stevedore.ExtensionManager( - namespace='teeth_agent.hardware_managers', - invoke_on_load=True) +def get_manager(): + global _global_manager - # There will always be at least one extension available (the - # GenericHardwareManager). - preferred_extension = sorted(extension_manager, _compare_extensions)[0] - preferred_manager = preferred_extension.obj + if not _global_manager: + log = structlog.get_logger() + extension_manager = stevedore.ExtensionManager( + namespace='teeth_agent.hardware_managers', + invoke_on_load=True) - if preferred_manager.evaluate_hardware_support() <= 0: - raise RuntimeError('No suitable HardwareManager could be found') + # There will always be at least one extension available (the + # GenericHardwareManager). + preferred_extension = sorted(extension_manager, _compare_extensions)[0] + preferred_manager = preferred_extension.obj - log.info('selected hardware manager', - manager_name=preferred_extension.entry_point_target) - return preferred_manager + if preferred_manager.evaluate_hardware_support() <= 0: + raise RuntimeError('No suitable HardwareManager could be found') + + log.info('selected hardware manager', + manager_name=preferred_extension.entry_point_target) + + _global_manager = preferred_manager + + return _global_manager