Show More
@@ -1,156 +1,157 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2010-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 | |
|
22 | 22 | import logging |
|
23 | 23 | import pylons |
|
24 | 24 | import Queue |
|
25 | 25 | import subprocess32 |
|
26 | 26 | |
|
27 | 27 | from pyramid.i18n import get_localizer |
|
28 | 28 | from pyramid.threadlocal import get_current_request |
|
29 | 29 | from threading import Thread |
|
30 | 30 | |
|
31 | 31 | from rhodecode.translation import _ as tsf |
|
32 | 32 | |
|
33 | 33 | |
|
34 | 34 | log = logging.getLogger(__name__) |
|
35 | 35 | |
|
36 | 36 | |
|
37 | 37 | def add_renderer_globals(event): |
|
38 | 38 | # Put pylons stuff into the context. This will be removed as soon as |
|
39 | 39 | # migration to pyramid is finished. |
|
40 | 40 | conf = pylons.config._current_obj() |
|
41 | 41 | event['h'] = conf.get('pylons.h') |
|
42 | 42 | event['c'] = pylons.tmpl_context |
|
43 | 43 | event['url'] = pylons.url |
|
44 | 44 | |
|
45 | 45 | # TODO: When executed in pyramid view context the request is not available |
|
46 | 46 | # in the event. Find a better solution to get the request. |
|
47 | 47 | request = event['request'] or get_current_request() |
|
48 | 48 | |
|
49 | 49 | # Add Pyramid translation as '_' to context |
|
50 | 50 | event['_'] = request.translate |
|
51 |
event[' |
|
|
51 | event['_ungettext'] = request.plularize | |
|
52 | 52 | |
|
53 | 53 | |
|
54 | 54 | def add_localizer(event): |
|
55 | 55 | request = event.request |
|
56 | 56 | localizer = get_localizer(request) |
|
57 | 57 | |
|
58 | 58 | def auto_translate(*args, **kwargs): |
|
59 | 59 | return localizer.translate(tsf(*args, **kwargs)) |
|
60 | 60 | |
|
61 | 61 | request.localizer = localizer |
|
62 | 62 | request.translate = auto_translate |
|
63 | request.plularize = localizer.pluralize | |
|
63 | 64 | |
|
64 | 65 | |
|
65 | 66 | def scan_repositories_if_enabled(event): |
|
66 | 67 | """ |
|
67 | 68 | This is subscribed to the `pyramid.events.ApplicationCreated` event. It |
|
68 | 69 | does a repository scan if enabled in the settings. |
|
69 | 70 | """ |
|
70 | 71 | from rhodecode.model.scm import ScmModel |
|
71 | 72 | from rhodecode.lib.utils import repo2db_mapper, get_rhodecode_base_path |
|
72 | 73 | settings = event.app.registry.settings |
|
73 | 74 | vcs_server_enabled = settings['vcs.server.enable'] |
|
74 | 75 | import_on_startup = settings['startup.import_repos'] |
|
75 | 76 | if vcs_server_enabled and import_on_startup: |
|
76 | 77 | repositories = ScmModel().repo_scan(get_rhodecode_base_path()) |
|
77 | 78 | repo2db_mapper(repositories, remove_obsolete=False) |
|
78 | 79 | |
|
79 | 80 | |
|
80 | 81 | class Subscriber(object): |
|
81 | 82 | """ |
|
82 | 83 | Base class for subscribers to the pyramid event system. |
|
83 | 84 | """ |
|
84 | 85 | def __call__(self, event): |
|
85 | 86 | self.run(event) |
|
86 | 87 | |
|
87 | 88 | def run(self, event): |
|
88 | 89 | raise NotImplementedError('Subclass has to implement this.') |
|
89 | 90 | |
|
90 | 91 | |
|
91 | 92 | class AsyncSubscriber(Subscriber): |
|
92 | 93 | """ |
|
93 | 94 | Subscriber that handles the execution of events in a separate task to not |
|
94 | 95 | block the execution of the code which triggers the event. It puts the |
|
95 | 96 | received events into a queue from which the worker process takes them in |
|
96 | 97 | order. |
|
97 | 98 | """ |
|
98 | 99 | def __init__(self): |
|
99 | 100 | self._stop = False |
|
100 | 101 | self._eventq = Queue.Queue() |
|
101 | 102 | self._worker = self.create_worker() |
|
102 | 103 | self._worker.start() |
|
103 | 104 | |
|
104 | 105 | def __call__(self, event): |
|
105 | 106 | self._eventq.put(event) |
|
106 | 107 | |
|
107 | 108 | def create_worker(self): |
|
108 | 109 | worker = Thread(target=self.do_work) |
|
109 | 110 | worker.daemon = True |
|
110 | 111 | return worker |
|
111 | 112 | |
|
112 | 113 | def stop_worker(self): |
|
113 | 114 | self._stop = False |
|
114 | 115 | self._eventq.put(None) |
|
115 | 116 | self._worker.join() |
|
116 | 117 | |
|
117 | 118 | def do_work(self): |
|
118 | 119 | while not self._stop: |
|
119 | 120 | event = self._eventq.get() |
|
120 | 121 | if event is not None: |
|
121 | 122 | self.run(event) |
|
122 | 123 | |
|
123 | 124 | |
|
124 | 125 | class AsyncSubprocessSubscriber(AsyncSubscriber): |
|
125 | 126 | """ |
|
126 | 127 | Subscriber that uses the subprocess32 module to execute a command if an |
|
127 | 128 | event is received. Events are handled asynchronously. |
|
128 | 129 | """ |
|
129 | 130 | |
|
130 | 131 | def __init__(self, cmd, timeout=None): |
|
131 | 132 | super(AsyncSubprocessSubscriber, self).__init__() |
|
132 | 133 | self._cmd = cmd |
|
133 | 134 | self._timeout = timeout |
|
134 | 135 | |
|
135 | 136 | def run(self, event): |
|
136 | 137 | cmd = self._cmd |
|
137 | 138 | timeout = self._timeout |
|
138 | 139 | log.debug('Executing command %s.', cmd) |
|
139 | 140 | |
|
140 | 141 | try: |
|
141 | 142 | output = subprocess32.check_output( |
|
142 | 143 | cmd, timeout=timeout, stderr=subprocess32.STDOUT) |
|
143 | 144 | log.debug('Command finished %s', cmd) |
|
144 | 145 | if output: |
|
145 | 146 | log.debug('Command output: %s', output) |
|
146 | 147 | except subprocess32.TimeoutExpired as e: |
|
147 | 148 | log.exception('Timeout while executing command.') |
|
148 | 149 | if e.output: |
|
149 | 150 | log.error('Command output: %s', e.output) |
|
150 | 151 | except subprocess32.CalledProcessError as e: |
|
151 | 152 | log.exception('Error while executing command.') |
|
152 | 153 | if e.output: |
|
153 | 154 | log.error('Command output: %s', e.output) |
|
154 | 155 | except: |
|
155 | 156 | log.exception( |
|
156 | 157 | 'Exception while executing command %s.', cmd) |
General Comments 0
You need to be logged in to leave comments.
Login now