##// END OF EJS Templates
processes: shorten the message and remove exit code which isn't shown properly anyway.
marcink -
r3492:a7095130 default
parent child Browse files
Show More
@@ -1,183 +1,182 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2016-2019 RhodeCode GmbH
4 4 #
5 5 # This program is free software: you can redistribute it and/or modify
6 6 # it under the terms of the GNU Affero General Public License, version 3
7 7 # (only), as published by the Free Software Foundation.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU Affero General Public License
15 15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 16 #
17 17 # This program is dual-licensed. If you wish to learn more about the
18 18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20 20
21 21 import logging
22 22
23 23 import psutil
24 24 import signal
25 25 from pyramid.view import view_config
26 26
27 27 from rhodecode.apps._base import BaseAppView
28 28 from rhodecode.apps._base.navigation import navigation_list
29 29 from rhodecode.lib import system_info
30 30 from rhodecode.lib.auth import (
31 31 LoginRequired, HasPermissionAllDecorator, CSRFRequired)
32 32 from rhodecode.lib.utils2 import safe_int, StrictAttributeDict
33 33
34 34 log = logging.getLogger(__name__)
35 35
36 36
37 37 class AdminProcessManagementView(BaseAppView):
38 38 def load_default_context(self):
39 39 c = self._get_local_tmpl_context()
40 40 return c
41 41
42 42 def _format_proc(self, proc, with_children=False):
43 43 try:
44 44 mem = proc.memory_info()
45 45 proc_formatted = StrictAttributeDict({
46 46 'pid': proc.pid,
47 47 'name': proc.name(),
48 48 'mem_rss': mem.rss,
49 49 'mem_vms': mem.vms,
50 50 'cpu_percent': proc.cpu_percent(),
51 51 'create_time': proc.create_time(),
52 52 'cmd': ' '.join(proc.cmdline()),
53 53 })
54 54
55 55 if with_children:
56 56 proc_formatted.update({
57 57 'children': [self._format_proc(x)
58 58 for x in proc.children(recursive=True)]
59 59 })
60 60 except Exception:
61 61 log.exception('Failed to load proc')
62 62 proc_formatted = None
63 63 return proc_formatted
64 64
65 65 def get_processes(self):
66 66 proc_list = []
67 67 for p in psutil.process_iter():
68 68 if 'gunicorn' in p.name():
69 69 proc = self._format_proc(p, with_children=True)
70 70 if proc:
71 71 proc_list.append(proc)
72 72
73 73 return proc_list
74 74
75 75 def get_workers(self):
76 76 workers = None
77 77 try:
78 78 rc_config = system_info.rhodecode_config().value['config']
79 79 workers = rc_config['server:main'].get('workers')
80 80 except Exception:
81 81 pass
82 82
83 83 return workers or '?'
84 84
85 85 @LoginRequired()
86 86 @HasPermissionAllDecorator('hg.admin')
87 87 @view_config(
88 88 route_name='admin_settings_process_management', request_method='GET',
89 89 renderer='rhodecode:templates/admin/settings/settings.mako')
90 90 def process_management(self):
91 91 _ = self.request.translate
92 92 c = self.load_default_context()
93 93
94 94 c.active = 'process_management'
95 95 c.navlist = navigation_list(self.request)
96 96 c.gunicorn_processes = self.get_processes()
97 97 c.gunicorn_workers = self.get_workers()
98 98 return self._get_template_context(c)
99 99
100 100 @LoginRequired()
101 101 @HasPermissionAllDecorator('hg.admin')
102 102 @view_config(
103 103 route_name='admin_settings_process_management_data', request_method='GET',
104 104 renderer='rhodecode:templates/admin/settings/settings_process_management_data.mako')
105 105 def process_management_data(self):
106 106 _ = self.request.translate
107 107 c = self.load_default_context()
108 108 c.gunicorn_processes = self.get_processes()
109 109 return self._get_template_context(c)
110 110
111 111 @LoginRequired()
112 112 @HasPermissionAllDecorator('hg.admin')
113 113 @CSRFRequired()
114 114 @view_config(
115 115 route_name='admin_settings_process_management_signal',
116 116 request_method='POST', renderer='json_ext')
117 117 def process_management_signal(self):
118 118 pids = self.request.json.get('pids', [])
119 119 result = []
120 120
121 121 def on_terminate(proc):
122 msg = "process `PID:{}` terminated with exit code {}".format(
123 proc.pid, proc.returncode or 0)
122 msg = "terminated"
124 123 result.append(msg)
125 124
126 125 procs = []
127 126 for pid in pids:
128 127 pid = safe_int(pid)
129 128 if pid:
130 129 try:
131 130 proc = psutil.Process(pid)
132 131 except psutil.NoSuchProcess:
133 132 continue
134 133
135 134 children = proc.children(recursive=True)
136 135 if children:
137 136 log.warning('Wont kill Master Process')
138 137 else:
139 138 procs.append(proc)
140 139
141 140 for p in procs:
142 141 try:
143 142 p.terminate()
144 143 except psutil.AccessDenied as e:
145 144 log.warning('Access denied: {}'.format(e))
146 145
147 146 gone, alive = psutil.wait_procs(procs, timeout=10, callback=on_terminate)
148 147 for p in alive:
149 148 try:
150 149 p.kill()
151 150 except psutil.AccessDenied as e:
152 151 log.warning('Access denied: {}'.format(e))
153 152
154 153 return {'result': result}
155 154
156 155 @LoginRequired()
157 156 @HasPermissionAllDecorator('hg.admin')
158 157 @CSRFRequired()
159 158 @view_config(
160 159 route_name='admin_settings_process_management_master_signal',
161 160 request_method='POST', renderer='json_ext')
162 161 def process_management_master_signal(self):
163 162 pid_data = self.request.json.get('pid_data', {})
164 163 pid = safe_int(pid_data['pid'])
165 164 action = pid_data['action']
166 165 if pid:
167 166 try:
168 167 proc = psutil.Process(pid)
169 168 except psutil.NoSuchProcess:
170 169 return {'result': 'failure_no_such_process'}
171 170
172 171 children = proc.children(recursive=True)
173 172 if children:
174 173 # master process
175 174 if action == '+' and len(children) <= 20:
176 175 proc.send_signal(signal.SIGTTIN)
177 176 elif action == '-' and len(children) >= 2:
178 177 proc.send_signal(signal.SIGTTOU)
179 178 else:
180 179 return {'result': 'failure_wrong_action'}
181 180 return {'result': 'success'}
182 181
183 182 return {'result': 'failure_not_master'}
General Comments 0
You need to be logged in to leave comments. Login now