From 2e5bf64a51cdcaa8be2e8a07dc74135aca8de182 Mon Sep 17 00:00:00 2001 From: Miguel Angel Ajo Date: Tue, 27 Jan 2015 11:52:30 +0000 Subject: [PATCH] Refactor the ProcessMonitor _exit_handler to ProcessMonitor We allowed to provide an specific _exit_handler, but in the end all the implementations are providing the same one. So, now it's refactored back to the monitor, and removed any YAGNI code. Partially implements: bp/agent-child-processes-status Change-Id: I916feb631885324bc98da1b1350915e14b6fcadc --- neutron/agent/dhcp/agent.py | 16 +--------------- neutron/agent/linux/external_process.py | 19 +++++++++++++------ neutron/cmd/netns_cleanup.py | 3 +-- .../agent/linux/test_process_monitor.py | 8 +------- .../unit/agent/linux/test_process_monitor.py | 10 +++++----- 5 files changed, 21 insertions(+), 35 deletions(-) diff --git a/neutron/agent/dhcp/agent.py b/neutron/agent/dhcp/agent.py index cf96f4fcf72..50dcec94dba 100644 --- a/neutron/agent/dhcp/agent.py +++ b/neutron/agent/dhcp/agent.py @@ -70,21 +70,7 @@ class DhcpAgent(manager.Manager): self._process_monitor = external_process.ProcessMonitor( config=self.conf, root_helper=self.root_helper, - resource_type='dhcp', - exit_handler=self._exit_handler) - - def _exit_handler(self, uuid, service): - """This is an exit handler for the ProcessMonitor. - - It will be called if the administrator configured the exit action in - check_child_processes_actions, and one of our external processes die - unexpectedly. - """ - LOG.error(_LE("Exiting neutron-dhcp-agent because of a malfunction " - "with the %(service)s process identified by uuid " - "%(uuid)s"), - {'service': service, 'uuid': uuid}) - raise SystemExit(1) + resource_type='dhcp') def _populate_networks_cache(self): """Populate the networks cache when the DHCP-agent starts.""" diff --git a/neutron/agent/linux/external_process.py b/neutron/agent/linux/external_process.py index 87af2245181..f8c5220a9d9 100644 --- a/neutron/agent/linux/external_process.py +++ b/neutron/agent/linux/external_process.py @@ -132,7 +132,7 @@ ServiceId = collections.namedtuple('ServiceId', ['uuid', 'service']) class ProcessMonitor(object): - def __init__(self, config, root_helper, resource_type, exit_handler): + def __init__(self, config, root_helper, resource_type): """Handle multiple process managers and watch over all of them. :param config: oslo config object with the agent configuration. @@ -141,15 +141,10 @@ class ProcessMonitor(object): :type root_helper: str :param resource_type: can be dhcp, router, load_balancer, etc. :type resource_type: str - :param exit_handler: function to execute when agent exit has to - be executed, it should take care of actual - exit - :type exit_hanlder: function """ self._config = config self._root_helper = root_helper self._resource_type = resource_type - self._exit_handler = exit_handler self._process_managers = {} @@ -299,3 +294,15 @@ class ProcessMonitor(object): LOG.error(_LE("Exiting agent as programmed in check_child_processes_" "actions")) self._exit_handler(service_id.uuid, service_id.service) + + def _exit_handler(self, uuid, service): + """This is an exit handler for the ProcessMonitor. + + It will be called if the administrator configured the exit action in + check_child_processes_actions, and one of our external processes die + unexpectedly. + """ + LOG.error(_LE("Exiting agent because of a malfunction with the " + "%(service)s process identified by uuid %(uuid)s"), + {'service': service, 'uuid': uuid}) + raise SystemExit(1) diff --git a/neutron/cmd/netns_cleanup.py b/neutron/cmd/netns_cleanup.py index 1f98197cc29..0ad95d07011 100644 --- a/neutron/cmd/netns_cleanup.py +++ b/neutron/cmd/netns_cleanup.py @@ -77,8 +77,7 @@ def _get_dhcp_process_monitor(config, root_helper): return external_process.ProcessMonitor( config=config, root_helper=root_helper, - resource_type='dhcp', - exit_handler=lambda: None) + resource_type='dhcp') def kill_dhcp(conf, namespace): diff --git a/neutron/tests/functional/agent/linux/test_process_monitor.py b/neutron/tests/functional/agent/linux/test_process_monitor.py index 8dc6eaa9966..5521f17c9a4 100644 --- a/neutron/tests/functional/agent/linux/test_process_monitor.py +++ b/neutron/tests/functional/agent/linux/test_process_monitor.py @@ -28,7 +28,6 @@ class BaseTestProcessMonitor(base.BaseSudoTestCase): def setUp(self): super(BaseTestProcessMonitor, self).setUp() - self._exit_handler_called = False cfg.CONF.set_override('check_child_processes_interval', 1, 'AGENT') self._child_processes = [] self._ext_processes = None @@ -43,12 +42,7 @@ class BaseTestProcessMonitor(base.BaseSudoTestCase): return external_process.ProcessMonitor( config=cfg.CONF, root_helper=None, - resource_type='test', - exit_handler=self._exit_handler) - - def _exit_handler(self, uuid, service): - self._exit_handler_called = True - self._exit_handler_params = (uuid, service) + resource_type='test') def _make_cmdline_callback(self, uuid): def _cmdline_callback(pidfile): diff --git a/neutron/tests/unit/agent/linux/test_process_monitor.py b/neutron/tests/unit/agent/linux/test_process_monitor.py index 28d1d4768b2..ecc47fcdf02 100644 --- a/neutron/tests/unit/agent/linux/test_process_monitor.py +++ b/neutron/tests/unit/agent/linux/test_process_monitor.py @@ -42,15 +42,13 @@ class BaseTestProcessMonitor(base.BaseTestCase): self.create_child_process_monitor('respawn') def create_child_process_monitor(self, action): - self.exit_handler = mock.Mock() conf = mock.Mock() conf.AGENT.check_child_processes_action = action conf.AGENT.check_child_processes = True self.pmonitor = external_process.ProcessMonitor( config=conf, root_helper=None, - resource_type='test', - exit_handler=self.exit_handler) + resource_type='test') def get_monitored_process_manager(self, uuid, service=None): self.pmonitor.enable(uuid=uuid, service=service, cmd_callback=None) @@ -69,8 +67,10 @@ class TestProcessMonitor(BaseTestProcessMonitor): self.create_child_process_monitor('exit') pm = self.get_monitored_process_manager(TEST_UUID) pm.active = False - self.pmonitor._check_child_processes() - self.exit_handler.assert_called_once_with(TEST_UUID, None) + with mock.patch.object(external_process.ProcessMonitor, + '_exit_handler') as exit_handler: + self.pmonitor._check_child_processes() + exit_handler.assert_called_once_with(TEST_UUID, None) def test_different_service_types(self): pm_none = self.get_monitored_process_manager(TEST_UUID)