Commit | Line | Data |
---|---|---|
e231d9e8 | 1 | # GNU MediaGoblin -- federated, autonomous media hosting |
cf29e8a8 | 2 | # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS. |
e231d9e8 CAW |
3 | # |
4 | # This program is free software: you can redistribute it and/or modify | |
5 | # it under the terms of the GNU Affero General Public License as published by | |
6 | # the Free Software Foundation, either version 3 of the License, or | |
7 | # (at your option) any later version. | |
8 | # | |
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU Affero General Public License for more details. | |
13 | # | |
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/>. | |
16 | ||
4f9f969d LS |
17 | import os |
18 | import sys | |
19 | ||
d693f6bd | 20 | from celery import Celery |
04f295e2 | 21 | from mediagoblin.tools.pluginapi import callable_runall |
d693f6bd | 22 | |
4f9f969d | 23 | |
4114e54d | 24 | MANDATORY_CELERY_IMPORTS = ['mediagoblin.processing.task'] |
4f9f969d LS |
25 | |
26 | DEFAULT_SETTINGS_MODULE = 'mediagoblin.init.celery.dummy_settings_module' | |
27 | ||
28 | ||
d693f6bd CAW |
29 | def get_celery_settings_dict(app_config, global_config, |
30 | force_celery_always_eager=False): | |
4f9f969d | 31 | """ |
d693f6bd | 32 | Get a celery settings dictionary from reading the config |
4f9f969d LS |
33 | """ |
34 | if 'celery' in global_config: | |
35 | celery_conf = global_config['celery'] | |
36 | else: | |
37 | celery_conf = {} | |
38 | ||
39 | celery_settings = {} | |
40 | ||
41 | # Add all celery settings from config | |
42 | for key, value in celery_conf.iteritems(): | |
43 | celery_settings[key] = value | |
44 | ||
45 | # TODO: use default result stuff here if it exists | |
46 | ||
47 | # add mandatory celery imports | |
48 | celery_imports = celery_settings.setdefault('CELERY_IMPORTS', []) | |
49 | celery_imports.extend(MANDATORY_CELERY_IMPORTS) | |
50 | ||
51 | if force_celery_always_eager: | |
52 | celery_settings['CELERY_ALWAYS_EAGER'] = True | |
53 | celery_settings['CELERY_EAGER_PROPAGATES_EXCEPTIONS'] = True | |
54 | ||
d693f6bd CAW |
55 | return celery_settings |
56 | ||
57 | ||
58 | def setup_celery_app(app_config, global_config, | |
59 | settings_module=DEFAULT_SETTINGS_MODULE, | |
60 | force_celery_always_eager=False): | |
61 | """ | |
62 | Setup celery without using terrible setup-celery-module hacks. | |
63 | """ | |
64 | celery_settings = get_celery_settings_dict( | |
65 | app_config, global_config, force_celery_always_eager) | |
66 | celery_app = Celery() | |
67 | celery_app.config_from_object(celery_settings) | |
68 | ||
04f295e2 | 69 | callable_runall('celery_setup', celery_app) |
f3f53028 | 70 | |
d693f6bd CAW |
71 | |
72 | def setup_celery_from_config(app_config, global_config, | |
73 | settings_module=DEFAULT_SETTINGS_MODULE, | |
74 | force_celery_always_eager=False, | |
75 | set_environ=True): | |
76 | """ | |
77 | Take a mediagoblin app config and try to set up a celery settings | |
78 | module from this. | |
79 | ||
80 | Args: | |
81 | - app_config: the application config section | |
82 | - global_config: the entire ConfigObj loaded config, all sections | |
83 | - settings_module: the module to populate, as a string | |
84 | - force_celery_always_eager: whether or not to force celery into | |
85 | always eager mode; good for development and small installs | |
86 | - set_environ: if set, this will CELERY_CONFIG_MODULE to the | |
87 | settings_module | |
88 | """ | |
89 | celery_settings = get_celery_settings_dict( | |
90 | app_config, global_config, force_celery_always_eager) | |
91 | ||
4f9f969d LS |
92 | __import__(settings_module) |
93 | this_module = sys.modules[settings_module] | |
94 | ||
95 | for key, value in celery_settings.iteritems(): | |
96 | setattr(this_module, key, value) | |
97 | ||
98 | if set_environ: | |
99 | os.environ['CELERY_CONFIG_MODULE'] = settings_module |