##// 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 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2
2
3 # Copyright (C) 2016-2017 RhodeCode GmbH
3 # Copyright (C) 2016-2017 RhodeCode GmbH
4 #
4 #
5 # This program is free software: you can redistribute it and/or modify
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Affero General Public License, version 3
6 # it under the terms of the GNU Affero General Public License, version 3
7 # (only), as published by the Free Software Foundation.
7 # (only), as published by the Free Software Foundation.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU Affero General Public License
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #
16 #
17 # This program is dual-licensed. If you wish to learn more about the
17 # This program is dual-licensed. If you wish to learn more about the
18 # RhodeCode Enterprise Edition, including its added features, Support services,
18 # RhodeCode Enterprise Edition, including its added features, Support services,
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
19 # and proprietary license terms, please see https://rhodecode.com/licenses/
20
20
21 import logging
21 import logging
22
22
23 import psutil
23 import psutil
24 from pyramid.view import view_config
24 from pyramid.view import view_config
25
25
26 from rhodecode.apps._base import BaseAppView
26 from rhodecode.apps._base import BaseAppView
27 from rhodecode.apps.admin.navigation import navigation_list
27 from rhodecode.apps.admin.navigation import navigation_list
28 from rhodecode.lib import helpers as h
28 from rhodecode.lib import helpers as h
29 from rhodecode.lib.auth import (
29 from rhodecode.lib.auth import (
30 LoginRequired, HasPermissionAllDecorator, CSRFRequired)
30 LoginRequired, HasPermissionAllDecorator, CSRFRequired)
31 from vcsserver.utils import safe_int
31 from rhodecode.lib.utils2 import safe_int
32
32
33 log = logging.getLogger(__name__)
33 log = logging.getLogger(__name__)
34
34
35
35
36 class AdminProcessManagementView(BaseAppView):
36 class AdminProcessManagementView(BaseAppView):
37 def load_default_context(self):
37 def load_default_context(self):
38 c = self._get_local_tmpl_context()
38 c = self._get_local_tmpl_context()
39 self._register_global_c(c)
39 self._register_global_c(c)
40 return c
40 return c
41
41
42 @LoginRequired()
42 @LoginRequired()
43 @HasPermissionAllDecorator('hg.admin')
43 @HasPermissionAllDecorator('hg.admin')
44 @view_config(
44 @view_config(
45 route_name='admin_settings_process_management', request_method='GET',
45 route_name='admin_settings_process_management', request_method='GET',
46 renderer='rhodecode:templates/admin/settings/settings.mako')
46 renderer='rhodecode:templates/admin/settings/settings.mako')
47 def process_management(self):
47 def process_management(self):
48 _ = self.request.translate
48 _ = self.request.translate
49 c = self.load_default_context()
49 c = self.load_default_context()
50
50
51 c.active = 'process_management'
51 c.active = 'process_management'
52 c.navlist = navigation_list(self.request)
52 c.navlist = navigation_list(self.request)
53 c.gunicorn_processes = (
53 c.gunicorn_processes = (
54 p for p in psutil.process_iter() if 'gunicorn' in p.name())
54 p for p in psutil.process_iter() if 'gunicorn' in p.name())
55 return self._get_template_context(c)
55 return self._get_template_context(c)
56
56
57 @LoginRequired()
57 @LoginRequired()
58 @HasPermissionAllDecorator('hg.admin')
58 @HasPermissionAllDecorator('hg.admin')
59 @CSRFRequired()
59 @CSRFRequired()
60 @view_config(
60 @view_config(
61 route_name='admin_settings_process_management_signal',
61 route_name='admin_settings_process_management_signal',
62 request_method='POST', renderer='json_ext')
62 request_method='POST', renderer='json_ext')
63 def process_management_signal(self):
63 def process_management_signal(self):
64 pids = self.request.json.get('pids', [])
64 pids = self.request.json.get('pids', [])
65 result = []
65 result = []
66 def on_terminate(proc):
66 def on_terminate(proc):
67 msg = "process `PID:{}` terminated with exit code {}".format(
67 msg = "process `PID:{}` terminated with exit code {}".format(
68 proc.pid, proc.returncode)
68 proc.pid, proc.returncode)
69 result.append(msg)
69 result.append(msg)
70
70
71 procs = []
71 procs = []
72 for pid in pids:
72 for pid in pids:
73 pid = safe_int(pid)
73 pid = safe_int(pid)
74 if pid:
74 if pid:
75 try:
75 try:
76 proc = psutil.Process(pid)
76 proc = psutil.Process(pid)
77 except psutil.NoSuchProcess:
77 except psutil.NoSuchProcess:
78 continue
78 continue
79
79
80 children = proc.children(recursive=True)
80 children = proc.children(recursive=True)
81 if children:
81 if children:
82 print('Wont kill Master Process')
82 print('Wont kill Master Process')
83 else:
83 else:
84 procs.append(proc)
84 procs.append(proc)
85
85
86 for p in procs:
86 for p in procs:
87 p.terminate()
87 p.terminate()
88 gone, alive = psutil.wait_procs(procs, timeout=10, callback=on_terminate)
88 gone, alive = psutil.wait_procs(procs, timeout=10, callback=on_terminate)
89 for p in alive:
89 for p in alive:
90 p.kill()
90 p.kill()
91
91
92 return {'result': result}
92 return {'result': result}
General Comments 0
You need to be logged in to leave comments. Login now