Add implement of restarting processes

This patch adds implementation of restarting processes

Change-Id: I1d88127ec9316c2721f2a1cafc9c7a55b3534875
Implements: bp pythonize-host-and-process-monitor
This commit is contained in:
Kengo Takahara 2017-02-09 19:12:55 +09:00
parent f7bf003c84
commit fc187a8cbf
4 changed files with 70 additions and 1 deletions

View File

@ -18,6 +18,13 @@ monitor_process_opts = [
cfg.IntOpt('check_interval',
default=5,
help='Interval in seconds for checking a process.'),
cfg.IntOpt('restart_retries',
default=3,
help='Number of retries when the failure of restarting a'
' process.'),
cfg.IntOpt('restart_interval',
default=5,
help='Interval in seconds for restarting a process.'),
cfg.StrOpt('process_list_path',
default='/etc/masakarimonitors/process_list.yaml',
help='The file path of process list.'),

View File

@ -73,7 +73,7 @@ class ProcessmonitorManager(manager.Manager):
if len(down_process_list) != 0:
# Restart down processes.
pass
self.process_handler.restart_processes(down_process_list)
# Reload process list and set to the process handler.
process_list = self._load_process_list()

View File

@ -12,6 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import eventlet
from oslo_log import log as oslo_logging
import masakarimonitors.conf
@ -104,3 +105,34 @@ class HandleProcess(object):
LOG.error(_LW("Monitoring command raised exception: %s"), e)
return down_process_list
def restart_processes(self, down_process_list):
"""Restart processes.
This method restarts the processes using restart command written in
the process list.
:param down_process_list: down process list object
"""
for down_process in down_process_list:
cmd_str = down_process['restart_command']
LOG.info(
_LI("Retart of process with executing command: %s"), cmd_str)
for retries in range(0, CONF.process.restart_retries + 1):
# Execute start command.
ret = self._execute_cmd(cmd_str, down_process['run_as_root'])
if ret == 0:
# Succeeded in restarting process.
break
else:
# Failed to restart process.
eventlet.greenthread.sleep(CONF.process.restart_interval)
continue
if retries == CONF.process.restart_retries:
# Send a notification.
pass

View File

@ -36,6 +36,20 @@ MOCK_PROCESS_LIST = [
},
]
MOCK_DOWN_PROCESS_LIST = [
{
'id': 1,
'process_name': 'mock_process_name_A',
'start_command': 'mock_start_command',
'pre_start_command': 'mock_pre_start_command',
'post_start_command': 'mock_post_start_command',
'restart_command': 'mock_restart_command',
'pre_restart_command': 'mock_pre_restart_command',
'post_restart_command': 'mock_post_restart_command',
'run_as_root': True
},
]
PS_RESULT = "\n" \
"UID PID PPID C STIME TTY TIME CMD\n" \
"root 11187 1 0 18:52 ? 00:00:00 mock_process_name_A\n"
@ -77,3 +91,19 @@ class TestHandleProcess(testtools.TestCase):
down_process_list = obj.monitor_processes()
self.assertEqual([], down_process_list)
@mock.patch.object(utils, 'execute')
def test_restart_processes(self,
mock_execute):
process_list = MOCK_PROCESS_LIST
obj = handle_process.HandleProcess()
obj.set_process_list(process_list)
down_process_list = MOCK_DOWN_PROCESS_LIST
mock_execute.return_value = ('test_stdout', '')
obj.restart_processes(down_process_list)
mock_execute.assert_called_once_with(
MOCK_PROCESS_LIST[0].get('restart_command'),
run_as_root=MOCK_DOWN_PROCESS_LIST[0].get('run_as_root'))