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