##// END OF EJS Templates
process-management: dont rely on vcsserver code.
marcink -
r1982:6563f4ae default
parent child Browse files
Show More
@@ -1,92 +1,92 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2016-2017 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 from pyramid.view import view_config
25 25
26 26 from rhodecode.apps._base import BaseAppView
27 27 from rhodecode.apps.admin.navigation import navigation_list
28 28 from rhodecode.lib import helpers as h
29 29 from rhodecode.lib.auth import (
30 30 LoginRequired, HasPermissionAllDecorator, CSRFRequired)
31 from vcsserver.utils import safe_int
31 from rhodecode.lib.utils2 import safe_int
32 32
33 33 log = logging.getLogger(__name__)
34 34
35 35
36 36 class AdminProcessManagementView(BaseAppView):
37 37 def load_default_context(self):
38 38 c = self._get_local_tmpl_context()
39 39 self._register_global_c(c)
40 40 return c
41 41
42 42 @LoginRequired()
43 43 @HasPermissionAllDecorator('hg.admin')
44 44 @view_config(
45 45 route_name='admin_settings_process_management', request_method='GET',
46 46 renderer='rhodecode:templates/admin/settings/settings.mako')
47 47 def process_management(self):
48 48 _ = self.request.translate
49 49 c = self.load_default_context()
50 50
51 51 c.active = 'process_management'
52 52 c.navlist = navigation_list(self.request)
53 53 c.gunicorn_processes = (
54 54 p for p in psutil.process_iter() if 'gunicorn' in p.name())
55 55 return self._get_template_context(c)
56 56
57 57 @LoginRequired()
58 58 @HasPermissionAllDecorator('hg.admin')
59 59 @CSRFRequired()
60 60 @view_config(
61 61 route_name='admin_settings_process_management_signal',
62 62 request_method='POST', renderer='json_ext')
63 63 def process_management_signal(self):
64 64 pids = self.request.json.get('pids', [])
65 65 result = []
66 66 def on_terminate(proc):
67 67 msg = "process `PID:{}` terminated with exit code {}".format(
68 68 proc.pid, proc.returncode)
69 69 result.append(msg)
70 70
71 71 procs = []
72 72 for pid in pids:
73 73 pid = safe_int(pid)
74 74 if pid:
75 75 try:
76 76 proc = psutil.Process(pid)
77 77 except psutil.NoSuchProcess:
78 78 continue
79 79
80 80 children = proc.children(recursive=True)
81 81 if children:
82 82 print('Wont kill Master Process')
83 83 else:
84 84 procs.append(proc)
85 85
86 86 for p in procs:
87 87 p.terminate()
88 88 gone, alive = psutil.wait_procs(procs, timeout=10, callback=on_terminate)
89 89 for p in alive:
90 90 p.kill()
91 91
92 92 return {'result': result}
General Comments 0
You need to be logged in to leave comments. Login now