SQL based tests and refactored Celery setup stuff
[mediagoblin.git] / mediagoblin / init / celery / __init__.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
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
17 import os
18 import sys
19
20 from celery import Celery
21
22
23 MANDATORY_CELERY_IMPORTS = ['mediagoblin.processing.task']
24
25 DEFAULT_SETTINGS_MODULE = 'mediagoblin.init.celery.dummy_settings_module'
26
27
28 def get_celery_settings_dict(app_config, global_config,
29 force_celery_always_eager=False):
30 """
31 Get a celery settings dictionary from reading the config
32 """
33 if 'celery' in global_config:
34 celery_conf = global_config['celery']
35 else:
36 celery_conf = {}
37
38 celery_settings = {}
39
40 # Add all celery settings from config
41 for key, value in celery_conf.iteritems():
42 celery_settings[key] = value
43
44 # TODO: use default result stuff here if it exists
45
46 # add mandatory celery imports
47 celery_imports = celery_settings.setdefault('CELERY_IMPORTS', [])
48 celery_imports.extend(MANDATORY_CELERY_IMPORTS)
49
50 if force_celery_always_eager:
51 celery_settings['CELERY_ALWAYS_EAGER'] = True
52 celery_settings['CELERY_EAGER_PROPAGATES_EXCEPTIONS'] = True
53
54 return celery_settings
55
56
57 def setup_celery_app(app_config, global_config,
58 settings_module=DEFAULT_SETTINGS_MODULE,
59 force_celery_always_eager=False):
60 """
61 Setup celery without using terrible setup-celery-module hacks.
62 """
63 celery_settings = get_celery_settings_dict(
64 app_config, global_config, force_celery_always_eager)
65 celery_app = Celery()
66 celery_app.config_from_object(celery_settings)
67
68
69 def setup_celery_from_config(app_config, global_config,
70 settings_module=DEFAULT_SETTINGS_MODULE,
71 force_celery_always_eager=False,
72 set_environ=True):
73 """
74 Take a mediagoblin app config and try to set up a celery settings
75 module from this.
76
77 Args:
78 - app_config: the application config section
79 - global_config: the entire ConfigObj loaded config, all sections
80 - settings_module: the module to populate, as a string
81 - force_celery_always_eager: whether or not to force celery into
82 always eager mode; good for development and small installs
83 - set_environ: if set, this will CELERY_CONFIG_MODULE to the
84 settings_module
85 """
86 celery_settings = get_celery_settings_dict(
87 app_config, global_config, force_celery_always_eager)
88
89 __import__(settings_module)
90 this_module = sys.modules[settings_module]
91
92 for key, value in celery_settings.iteritems():
93 setattr(this_module, key, value)
94
95 if set_environ:
96 os.environ['CELERY_CONFIG_MODULE'] = settings_module