process-management: use safer way to extract data for processes.

- prevents from unknown PID errors.
This commit is contained in:
Marcin Kuzminski 2018-02-05 18:45:04 +01:00
parent b23a023093
commit b8619894a6
2 changed files with 52 additions and 26 deletions

View file

@ -28,7 +28,7 @@ from rhodecode.apps._base import BaseAppView
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,9 +36,41 @@ log = logging.getLogger(__name__)
class AdminProcessManagementView(BaseAppView):
def load_default_context(self):
c = self._get_local_tmpl_context()
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')
@view_config(
@ -50,8 +82,7 @@ class AdminProcessManagementView(BaseAppView):
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(BaseAppView):
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()

View file

@ -2,12 +2,11 @@
<table id="procList">
<%
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
%>
<tr>
<td colspan="8">
@ -15,10 +14,8 @@
</td>
</tr>
% for proc in c.gunicorn_processes:
<% mem = proc.memory_info()%>
<% children = proc.children(recursive=True) %>
% if children:
% if proc.children:
<tr>
<td>
<code>
@ -28,18 +25,18 @@
<td>
<a href="#showCommand" onclick="$('#pid'+${proc.pid}).toggle();return false"> command </a>
<code id="pid${proc.pid}" style="display: none">
${' '.join(proc.cmdline())}
${proc.cmd}
</code>
</td>
<td></td>
<td>
RSS:${h.format_byte_size_binary(mem.rss)}
RSS:${h.format_byte_size_binary(proc.mem_rss)}
</td>
<td>
VMS:${h.format_byte_size_binary(mem.vms)}
VMS:${h.format_byte_size_binary(proc.mem_vms)}
</td>
<td>
AGE: ${h.age_component(h.time_to_utcdatetime(proc.create_time()))}
AGE: ${h.age_component(h.time_to_utcdatetime(proc.create_time))}
</td>
<td>
MASTER
@ -49,8 +46,7 @@
</td>
</tr>
<% mem_sum = 0 %>
% for proc_child in children:
<% mem = proc_child.memory_info()%>
% for proc_child in proc.children:
<tr>
<td>
<code>
@ -60,21 +56,21 @@
<td>
<a href="#showCommand" onclick="$('#pid'+${proc_child.pid}).toggle();return false"> command </a>
<code id="pid${proc_child.pid}" style="display: none">
${' '.join(proc_child.cmdline())}
${proc_child.cmd}
</code>
</td>
<td>
CPU: ${proc_child.cpu_percent()} %
CPU: ${proc_child.cpu_percent} %
</td>
<td>
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 %>
</td>
<td>
VMS:${h.format_byte_size_binary(mem.vms)}
VMS:${h.format_byte_size_binary(proc_child.mem_vms)}
</td>
<td>
AGE: ${h.age_component(h.time_to_utcdatetime(proc_child.create_time()))}
AGE: ${h.age_component(h.time_to_utcdatetime(proc_child.create_time))}
</td>
<td>
<a href="#restartProcess" onclick="restart(this, ${proc_child.pid});return false">
@ -84,7 +80,7 @@
</tr>
% endfor
<tr>
<td colspan="2"><code>| total processes: ${len(children)}</code></td>
<td colspan="2"><code>| total processes: ${len(proc.children)}</code></td>
<td></td>
<td><strong>RSS:${h.format_byte_size_binary(mem_sum)}</strong></td>
<td></td>