Show More
@@ -1,182 +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 |
'cpu_percent': proc.cpu_percent(interval=0. |
|
|
50 | 'cpu_percent': proc.cpu_percent(interval=0.1), | |
|
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 | 122 | msg = "terminated" |
|
123 | 123 | result.append(msg) |
|
124 | 124 | |
|
125 | 125 | procs = [] |
|
126 | 126 | for pid in pids: |
|
127 | 127 | pid = safe_int(pid) |
|
128 | 128 | if pid: |
|
129 | 129 | try: |
|
130 | 130 | proc = psutil.Process(pid) |
|
131 | 131 | except psutil.NoSuchProcess: |
|
132 | 132 | continue |
|
133 | 133 | |
|
134 | 134 | children = proc.children(recursive=True) |
|
135 | 135 | if children: |
|
136 | 136 | log.warning('Wont kill Master Process') |
|
137 | 137 | else: |
|
138 | 138 | procs.append(proc) |
|
139 | 139 | |
|
140 | 140 | for p in procs: |
|
141 | 141 | try: |
|
142 | 142 | p.terminate() |
|
143 | 143 | except psutil.AccessDenied as e: |
|
144 | 144 | log.warning('Access denied: {}'.format(e)) |
|
145 | 145 | |
|
146 | 146 | gone, alive = psutil.wait_procs(procs, timeout=10, callback=on_terminate) |
|
147 | 147 | for p in alive: |
|
148 | 148 | try: |
|
149 | 149 | p.kill() |
|
150 | 150 | except psutil.AccessDenied as e: |
|
151 | 151 | log.warning('Access denied: {}'.format(e)) |
|
152 | 152 | |
|
153 | 153 | return {'result': result} |
|
154 | 154 | |
|
155 | 155 | @LoginRequired() |
|
156 | 156 | @HasPermissionAllDecorator('hg.admin') |
|
157 | 157 | @CSRFRequired() |
|
158 | 158 | @view_config( |
|
159 | 159 | route_name='admin_settings_process_management_master_signal', |
|
160 | 160 | request_method='POST', renderer='json_ext') |
|
161 | 161 | def process_management_master_signal(self): |
|
162 | 162 | pid_data = self.request.json.get('pid_data', {}) |
|
163 | 163 | pid = safe_int(pid_data['pid']) |
|
164 | 164 | action = pid_data['action'] |
|
165 | 165 | if pid: |
|
166 | 166 | try: |
|
167 | 167 | proc = psutil.Process(pid) |
|
168 | 168 | except psutil.NoSuchProcess: |
|
169 | 169 | return {'result': 'failure_no_such_process'} |
|
170 | 170 | |
|
171 | 171 | children = proc.children(recursive=True) |
|
172 | 172 | if children: |
|
173 | 173 | # master process |
|
174 | 174 | if action == '+' and len(children) <= 20: |
|
175 | 175 | proc.send_signal(signal.SIGTTIN) |
|
176 | 176 | elif action == '-' and len(children) >= 2: |
|
177 | 177 | proc.send_signal(signal.SIGTTOU) |
|
178 | 178 | else: |
|
179 | 179 | return {'result': 'failure_wrong_action'} |
|
180 | 180 | return {'result': 'success'} |
|
181 | 181 | |
|
182 | 182 | return {'result': 'failure_not_master'} |
General Comments 0
You need to be logged in to leave comments.
Login now