##// END OF EJS Templates
celery: make celery load config from pyramid_app if available...
dan -
r654:f6e3b63a default
parent child Browse files
Show More
@@ -1,93 +1,97 b''
1 1 # -*- coding: utf-8 -*-
2 2
3 3 # Copyright (C) 2012-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 import pylons
21 22 import rhodecode
22 from pylons import config
23 23
24 24 from celery.loaders.base import BaseLoader
25 25
26 26 to_pylons = lambda x: x.replace('_', '.').lower()
27 27 to_celery = lambda x: x.replace('.', '_').upper()
28 28
29 29 LIST_PARAMS = """CELERY_IMPORTS ADMINS ROUTES""".split()
30 30
31 31 class PylonsSettingsProxy(object):
32 32 """Pylons Settings Proxy
33 33
34 34 Proxies settings from pylons.config
35 35
36 36 """
37 37 def __getattr__(self, key):
38 38 pylons_key = to_pylons(key)
39 proxy_config = rhodecode.PYRAMID_SETTINGS or pylons.config
39 40 try:
40 value = rhodecode.PYRAMID_SETTINGS[pylons_key]
41 if key in LIST_PARAMS:return value.split()
41 value = proxy_config[pylons_key]
42 if key in LIST_PARAMS:
43 return value.split()
42 44 return self.type_converter(value)
43 45 except KeyError:
44 46 raise AttributeError(pylons_key)
45 47
46 48 def get(self, key):
47 49 try:
48 50 return self.__getattr__(key)
49 51 except AttributeError:
50 52 return None
51 53
52 54 def __getitem__(self, key):
53 55 try:
54 56 return self.__getattr__(key)
55 57 except AttributeError:
56 58 raise KeyError()
57 59
58 60 def __setattr__(self, key, value):
59 61 pylons_key = to_pylons(key)
60 rhodecode.PYRAMID_SETTINGS[pylons_key] = value
62 proxy_config = rhodecode.PYRAMID_SETTINGS or pylons.config
63 proxy_config[pylons_key] = value
61 64
62 65 def __setitem__(self, key, value):
63 66 self.__setattr__(key, value)
64 67
65 68 def type_converter(self, value):
66 69 #cast to int
67 70 if value.isdigit():
68 71 return int(value)
69 72
70 73 #cast to bool
71 74 if value.lower() in ['true', 'false']:
72 75 return value.lower() == 'true'
73 76 return value
74 77
75 78 class PylonsLoader(BaseLoader):
76 79 """Pylons celery loader
77 80
78 81 Maps the celery config onto pylons.config
79 82
80 83 """
81 84 def read_configuration(self):
82 85 self.configured = True
83 86 return PylonsSettingsProxy()
84 87
85 88 def on_worker_init(self):
86 89 """
87 90 Import task modules.
88 91 """
89 92 self.import_default_modules()
90 93 from rhodecode.config.middleware import make_pyramid_app
91 94
92 95 # adding to self to keep a reference around
93 self.pyramid_app = make_pyramid_app(config, **config['app_conf'])
96 self.pyramid_app = make_pyramid_app(
97 pylons.config, **pylons.config['app_conf'])
General Comments 0
You need to be logged in to leave comments. Login now