1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 MediaGoblin contributors. See AUTHORS.
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.
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.
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/>.
21 MANDATORY_CELERY_IMPORTS
= ['mediagoblin.process_media']
23 DEFAULT_SETTINGS_MODULE
= 'mediagoblin.init.celery.dummy_settings_module'
26 def setup_celery_from_config(app_config
, global_config
,
27 settings_module
=DEFAULT_SETTINGS_MODULE
,
28 force_celery_always_eager
=False,
31 Take a mediagoblin app config and try to set up a celery settings
35 - app_config: the application config section
36 - global_config: the entire ConfigObj loaded config, all sections
37 - settings_module: the module to populate, as a string
38 - force_celery_always_eager: whether or not to force celery into
39 always eager mode; good for development and small installs
40 - set_environ: if set, this will CELERY_CONFIG_MODULE to the
43 if 'celery' in global_config
:
44 celery_conf
= global_config
['celery']
50 # set up mongodb stuff
51 celery_settings
['CELERY_RESULT_BACKEND'] = 'mongodb'
52 if 'BROKER_BACKEND' not in celery_settings
:
53 celery_settings
['BROKER_BACKEND'] = 'mongodb'
55 celery_mongo_settings
= {}
57 if 'db_host' in app_config
:
58 celery_mongo_settings
['host'] = app_config
['db_host']
59 if celery_settings
['BROKER_BACKEND'] == 'mongodb':
60 celery_settings
['BROKER_HOST'] = app_config
['db_host']
61 if 'db_port' in app_config
:
62 celery_mongo_settings
['port'] = app_config
['db_port']
63 if celery_settings
['BROKER_BACKEND'] == 'mongodb':
64 celery_settings
['BROKER_PORT'] = app_config
['db_port']
65 celery_mongo_settings
['database'] = app_config
['db_name']
67 celery_settings
['CELERY_MONGODB_BACKEND_SETTINGS'] = celery_mongo_settings
70 for key
, value
in celery_conf
.iteritems():
72 celery_settings
[key
] = value
74 # add mandatory celery imports
75 celery_imports
= celery_settings
.setdefault('CELERY_IMPORTS', [])
76 celery_imports
.extend(MANDATORY_CELERY_IMPORTS
)
78 if force_celery_always_eager
:
79 celery_settings
['CELERY_ALWAYS_EAGER'] = True
80 celery_settings
['CELERY_EAGER_PROPAGATES_EXCEPTIONS'] = True
82 __import__(settings_module
)
83 this_module
= sys
.modules
[settings_module
]
85 for key
, value
in celery_settings
.iteritems():
86 setattr(this_module
, key
, value
)
89 os
.environ
['CELERY_CONFIG_MODULE'] = settings_module