]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
Refactor the ProcessMonitor _exit_handler to ProcessMonitor
authorMiguel Angel Ajo <mangelajo@redhat.com>
Tue, 27 Jan 2015 11:52:30 +0000 (11:52 +0000)
committerMiguel Angel Ajo <mangelajo@redhat.com>
Tue, 27 Jan 2015 11:52:30 +0000 (11:52 +0000)
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
neutron/agent/linux/external_process.py
neutron/cmd/netns_cleanup.py
neutron/tests/functional/agent/linux/test_process_monitor.py
neutron/tests/unit/agent/linux/test_process_monitor.py

index cf96f4fcf722cd6024e358b2dac290d9dd2e86ec..50dcec94dba66e4c4bf8176faaf78c52e96d4504 100644 (file)
@@ -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."""
index 87af224518136e6af15231d79e44fb03a624cb16..f8c5220a9d94534ae76ef5e003161a7a62aadcf7 100644 (file)
@@ -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)
index 1f98197cc2922c6736091e5b9ca968dc23e18f63..0ad95d07011fe41a0a1bc7ed4ac7f8f9e937a300 100644 (file)
@@ -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):
index 8dc6eaa996611b1b341b3aa903cb82844e774ca9..5521f17c9a444780a08cbc088d4b6437b9a30106 100644 (file)
@@ -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):
index 28d1d4768b259914275596f621f68b2871e967f9..ecc47fcdf021cb26d59a62292e1fafca2f96f686 100644 (file)
@@ -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)