Merge branch 'i507_beaker_cache'
[mediagoblin.git] / mediagoblin / init / celery / __init__.py
CommitLineData
e231d9e8 1# GNU MediaGoblin -- federated, autonomous media hosting
12a100e4 2# Copyright (C) 2011 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
17import os
694c2351 18import sys
e231d9e8 19
e231d9e8 20
88816492
CAW
21MANDATORY_CELERY_IMPORTS = ['mediagoblin.process_media']
22
073b61fe 23DEFAULT_SETTINGS_MODULE = 'mediagoblin.init.celery.dummy_settings_module'
694c2351 24
52f20ff3 25
694c2351
CAW
26def setup_celery_from_config(app_config, global_config,
27 settings_module=DEFAULT_SETTINGS_MODULE,
571198c9 28 force_celery_always_eager=False,
694c2351 29 set_environ=True):
e231d9e8 30 """
a32f17c9
CAW
31 Take a mediagoblin app config and try to set up a celery settings
32 module from this.
694c2351
CAW
33
34 Args:
35 - app_config: the application config section
a32f17c9 36 - global_config: the entire ConfigObj loaded config, all sections
694c2351 37 - settings_module: the module to populate, as a string
d01744dd
CAW
38 - force_celery_always_eager: whether or not to force celery into
39 always eager mode; good for development and small installs
694c2351
CAW
40 - set_environ: if set, this will CELERY_CONFIG_MODULE to the
41 settings_module
e231d9e8 42 """
52f20ff3
CAW
43 if global_config.has_key('celery'):
44 celery_conf = global_config['celery']
e231d9e8
CAW
45 else:
46 celery_conf = {}
47
48 celery_settings = {}
49
50 # set up mongodb stuff
1c61a6ca
CAW
51 celery_settings['CELERY_RESULT_BACKEND'] = 'mongodb'
52 if not celery_settings.has_key('BROKER_BACKEND'):
53 celery_settings['BROKER_BACKEND'] = 'mongodb'
54
e231d9e8 55 celery_mongo_settings = {}
1c61a6ca 56
e231d9e8
CAW
57 if app_config.has_key('db_host'):
58 celery_mongo_settings['host'] = app_config['db_host']
1c61a6ca
CAW
59 if celery_settings['BROKER_BACKEND'] == 'mongodb':
60 celery_settings['BROKER_HOST'] = app_config['db_host']
e231d9e8 61 if app_config.has_key('db_port'):
d01744dd 62 celery_mongo_settings['port'] = app_config['db_port']
1c61a6ca 63 if celery_settings['BROKER_BACKEND'] == 'mongodb':
d01744dd 64 celery_settings['BROKER_PORT'] = app_config['db_port']
39c6b2bd 65 celery_mongo_settings['database'] = app_config['db_name']
e231d9e8
CAW
66
67 celery_settings['CELERY_MONGODB_BACKEND_SETTINGS'] = celery_mongo_settings
e231d9e8
CAW
68
69 # Add anything else
70 for key, value in celery_conf.iteritems():
71 key = key.upper()
e231d9e8
CAW
72 celery_settings[key] = value
73
88816492
CAW
74 # add mandatory celery imports
75 celery_imports = celery_settings.setdefault('CELERY_IMPORTS', [])
76 celery_imports.extend(MANDATORY_CELERY_IMPORTS)
77
571198c9
CAW
78 if force_celery_always_eager:
79 celery_settings['CELERY_ALWAYS_EAGER'] = True
55c74e1e 80 celery_settings['CELERY_EAGER_PROPAGATES_EXCEPTIONS'] = True
571198c9 81
694c2351
CAW
82 __import__(settings_module)
83 this_module = sys.modules[settings_module]
e231d9e8 84
ef30978a 85 for key, value in celery_settings.iteritems():
694c2351
CAW
86 setattr(this_module, key, value)
87
88 if set_environ:
89 os.environ['CELERY_CONFIG_MODULE'] = settings_module