Show More
@@ -1,156 +1,109 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2016-2016 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 | import Queue |
|
23 | 23 | import subprocess32 |
|
24 | 24 | from threading import Thread |
|
25 | 25 | |
|
26 | 26 | |
|
27 | 27 | from .utils import generate_mod_dav_svn_config |
|
28 | 28 | |
|
29 | 29 | |
|
30 | 30 | log = logging.getLogger(__name__) |
|
31 | 31 | |
|
32 | 32 | |
|
33 | 33 | def generate_config_subscriber(event): |
|
34 | 34 | """ |
|
35 | 35 | Subscriber to the `rhodcode.events.RepoGroupEvent`. This triggers the |
|
36 | 36 | automatic generation of mod_dav_svn config file on repository group |
|
37 | 37 | changes. |
|
38 | 38 | """ |
|
39 | 39 | try: |
|
40 | 40 | generate_mod_dav_svn_config(event.request.registry) |
|
41 | 41 | except Exception: |
|
42 | 42 | log.exception( |
|
43 | 43 | 'Exception while generating subversion mod_dav_svn configuration.') |
|
44 | 44 | |
|
45 | 45 | |
|
46 | 46 | class Subscriber(object): |
|
47 | 47 | def __call__(self, event): |
|
48 | 48 | self.run(event) |
|
49 | 49 | |
|
50 | 50 | def run(self, event): |
|
51 | 51 | raise NotImplementedError('Subclass has to implement this.') |
|
52 | 52 | |
|
53 | 53 | |
|
54 | 54 | class AsyncSubscriber(Subscriber): |
|
55 | 55 | def __init__(self): |
|
56 | 56 | self._stop = False |
|
57 | 57 | self._eventq = Queue.Queue() |
|
58 | 58 | self._worker = self.create_worker() |
|
59 | 59 | self._worker.start() |
|
60 | 60 | |
|
61 | 61 | def __call__(self, event): |
|
62 | 62 | self._eventq.put(event) |
|
63 | 63 | |
|
64 | 64 | def create_worker(self): |
|
65 | 65 | worker = Thread(target=self.do_work) |
|
66 | 66 | worker.daemon = True |
|
67 | 67 | return worker |
|
68 | 68 | |
|
69 | 69 | def stop_worker(self): |
|
70 | 70 | self._stop = False |
|
71 | 71 | self._eventq.put(None) |
|
72 | 72 | self._worker.join() |
|
73 | 73 | |
|
74 | 74 | def do_work(self): |
|
75 | 75 | while not self._stop: |
|
76 | 76 | event = self._eventq.get() |
|
77 | 77 | if event is not None: |
|
78 | 78 | self.run(event) |
|
79 | 79 | |
|
80 | 80 | |
|
81 | 81 | class AsyncSubprocessSubscriber(AsyncSubscriber): |
|
82 | 82 | |
|
83 | 83 | def __init__(self, cmd, timeout=None): |
|
84 | 84 | super(AsyncSubprocessSubscriber, self).__init__() |
|
85 | 85 | self._cmd = cmd |
|
86 | 86 | self._timeout = timeout |
|
87 | 87 | |
|
88 | 88 | def run(self, event): |
|
89 | 89 | cmd = self._cmd |
|
90 | 90 | timeout = self._timeout |
|
91 | 91 | log.debug('Executing command %s.', cmd) |
|
92 | 92 | |
|
93 | 93 | try: |
|
94 | 94 | output = subprocess32.check_output( |
|
95 | 95 | cmd, timeout=timeout, stderr=subprocess32.STDOUT) |
|
96 | 96 | log.debug('Command finished %s', cmd) |
|
97 | 97 | if output: |
|
98 | 98 | log.debug('Command output: %s', output) |
|
99 | 99 | except subprocess32.TimeoutExpired as e: |
|
100 | 100 | log.exception('Timeout while executing command.') |
|
101 | 101 | if e.output: |
|
102 | 102 | log.error('Command output: %s', e.output) |
|
103 | 103 | except subprocess32.CalledProcessError as e: |
|
104 | 104 | log.exception('Error while executing command.') |
|
105 | 105 | if e.output: |
|
106 | 106 | log.error('Command output: %s', e.output) |
|
107 | 107 | except: |
|
108 | 108 | log.exception( |
|
109 | 109 | 'Exception while executing command %s.', cmd) |
|
110 | ||
|
111 | ||
|
112 | # class ReloadApacheSubscriber(object): | |
|
113 | # """ | |
|
114 | # Subscriber to pyramids event system. It executes the Apache reload command | |
|
115 | # if set in ini-file. The command is executed asynchronously in a separate | |
|
116 | # task. This is done to prevent a delay of the function which triggered the | |
|
117 | # event in case of a longer running command. If a timeout is passed to the | |
|
118 | # constructor the command will be terminated after expiration. | |
|
119 | # """ | |
|
120 | # def __init__(self, settings, timeout=None): | |
|
121 | # self.thread = None | |
|
122 | # cmd = self.get_command_from_settings(settings) | |
|
123 | # if cmd: | |
|
124 | # kwargs = { | |
|
125 | # 'cmd': cmd, | |
|
126 | # 'timeout': timeout, | |
|
127 | # } | |
|
128 | # self.thread = Thread(target=self.run, kwargs=kwargs) | |
|
129 | ||
|
130 | # def __call__(self, event): | |
|
131 | # if self.thread is not None: | |
|
132 | # self.thread.start() | |
|
133 | ||
|
134 | # def get_command_from_settings(self, settings): | |
|
135 | # cmd = settings[config_keys.reload_command] | |
|
136 | # return cmd.split(' ') if cmd else cmd | |
|
137 | ||
|
138 | # def run(self, cmd, timeout=None): | |
|
139 | # log.debug('Executing svn proxy reload command %s.', cmd) | |
|
140 | # try: | |
|
141 | # output = subprocess32.check_output( | |
|
142 | # cmd, timeout=timeout, stderr=subprocess32.STDOUT) | |
|
143 | # log.debug('Svn proxy reload command finished.') | |
|
144 | # if output: | |
|
145 | # log.debug('Command output: %s', output) | |
|
146 | # except subprocess32.TimeoutExpired as e: | |
|
147 | # log.exception('Timeout while executing svn proxy reload command.') | |
|
148 | # if e.output: | |
|
149 | # log.error('Command output: %s', e.output) | |
|
150 | # except subprocess32.CalledProcessError as e: | |
|
151 | # log.exception('Error while executing svn proxy reload command.') | |
|
152 | # if e.output: | |
|
153 | # log.error('Command output: %s', e.output) | |
|
154 | # except: | |
|
155 | # log.exception( | |
|
156 | # 'Exception while executing svn proxy reload command %s.', cmd) |
General Comments 0
You need to be logged in to leave comments.
Login now