# HG changeset patch # User Marcin Kuzminski # Date 2018-02-05 17:45:04 # Node ID 370f15d3bd28a84bf6f0a555e267e5a746b7dafe # Parent b55500061638920e7a77af5a2f765f6e90222820 process-management: use safer way to extract data for processes. - prevents from unknown PID errors. diff --git a/rhodecode/apps/admin/views/process_management.py b/rhodecode/apps/admin/views/process_management.py --- a/rhodecode/apps/admin/views/process_management.py +++ b/rhodecode/apps/admin/views/process_management.py @@ -28,7 +28,7 @@ from rhodecode.apps._base import BaseApp from rhodecode.apps.admin.navigation import navigation_list from rhodecode.lib.auth import ( LoginRequired, HasPermissionAllDecorator, CSRFRequired) -from rhodecode.lib.utils2 import safe_int +from rhodecode.lib.utils2 import safe_int, StrictAttributeDict log = logging.getLogger(__name__) @@ -36,8 +36,40 @@ log = logging.getLogger(__name__) class AdminProcessManagementView(BaseAppView): def load_default_context(self): c = self._get_local_tmpl_context() + return c - return c + def _format_proc(self, proc, with_children=False): + try: + mem = proc.memory_info() + proc_formatted = StrictAttributeDict({ + 'pid': proc.pid, + 'name': proc.name(), + 'mem_rss': mem.rss, + 'mem_vms': mem.vms, + 'cpu_percent': proc.cpu_percent(), + 'create_time': proc.create_time(), + 'cmd': ' '.join(proc.cmdline()), + }) + + if with_children: + proc_formatted.update({ + 'children': [self._format_proc(x) + for x in proc.children(recursive=True)] + }) + except Exception: + log.exception('Failed to load proc') + proc_formatted = None + return proc_formatted + + def get_processes(self): + proc_list = [] + for p in psutil.process_iter(): + if 'gunicorn' in p.name(): + proc = self._format_proc(p, with_children=True) + if proc: + proc_list.append(proc) + + return proc_list @LoginRequired() @HasPermissionAllDecorator('hg.admin') @@ -50,8 +82,7 @@ class AdminProcessManagementView(BaseApp c.active = 'process_management' c.navlist = navigation_list(self.request) - c.gunicorn_processes = ( - p for p in psutil.process_iter() if 'gunicorn' in p.name()) + c.gunicorn_processes = self.get_processes() return self._get_template_context(c) @LoginRequired() @@ -62,8 +93,7 @@ class AdminProcessManagementView(BaseApp def process_management_data(self): _ = self.request.translate c = self.load_default_context() - c.gunicorn_processes = ( - p for p in psutil.process_iter() if 'gunicorn' in p.name()) + c.gunicorn_processes = self.get_processes() return self._get_template_context(c) @LoginRequired() diff --git a/rhodecode/templates/admin/settings/settings_process_management_data.mako b/rhodecode/templates/admin/settings/settings_process_management_data.mako --- a/rhodecode/templates/admin/settings/settings_process_management_data.mako +++ b/rhodecode/templates/admin/settings/settings_process_management_data.mako @@ -2,12 +2,11 @@ <% def get_name(proc): - cmd = ' '.join(proc.cmdline()) - if 'vcsserver.ini' in cmd: + if 'vcsserver.ini' in proc.cmd: return 'VCSServer' - elif 'rhodecode.ini' in cmd: + elif 'rhodecode.ini' in proc.cmd: return 'RhodeCode' - return proc.name() + return proc.name %> % for proc in c.gunicorn_processes: - <% mem = proc.memory_info()%> - <% children = proc.children(recursive=True) %> - % if children: + % if proc.children: <% mem_sum = 0 %> - % for proc_child in children: - <% mem = proc_child.memory_info()%> + % for proc_child in proc.children: % endfor - +
@@ -15,10 +14,8 @@
@@ -28,18 +25,18 @@ command - RSS:${h.format_byte_size_binary(mem.rss)} + RSS:${h.format_byte_size_binary(proc.mem_rss)} - VMS:${h.format_byte_size_binary(mem.vms)} + VMS:${h.format_byte_size_binary(proc.mem_vms)} - AGE: ${h.age_component(h.time_to_utcdatetime(proc.create_time()))} + AGE: ${h.age_component(h.time_to_utcdatetime(proc.create_time))} MASTER @@ -49,8 +46,7 @@
@@ -60,21 +56,21 @@ command - CPU: ${proc_child.cpu_percent()} % + CPU: ${proc_child.cpu_percent} % - RSS:${h.format_byte_size_binary(mem.rss)} - <% mem_sum += mem.rss %> + RSS:${h.format_byte_size_binary(proc_child.mem_rss)} + <% mem_sum += proc_child.mem_rss %> - VMS:${h.format_byte_size_binary(mem.vms)} + VMS:${h.format_byte_size_binary(proc_child.mem_vms)} - AGE: ${h.age_component(h.time_to_utcdatetime(proc_child.create_time()))} + AGE: ${h.age_component(h.time_to_utcdatetime(proc_child.create_time))} @@ -84,7 +80,7 @@
| total processes: ${len(children)}| total processes: ${len(proc.children)} RSS:${h.format_byte_size_binary(mem_sum)}