Merge remote-tracking branch 'cwebber/254_delete_queue_directories'
[mediagoblin.git] / mediagoblin / init / celery / from_celery.py
CommitLineData
1e48a830 1# GNU MediaGoblin -- federated, autonomous media hosting
cf29e8a8 2# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
1e48a830
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
b1bb15d1 18import logging
9d7c69fb 19import logging.config
b1bb15d1 20
b1bb15d1 21from celery.signals import setup_logging
1e48a830 22
924c586b 23from mediagoblin import app, mg_globals
073b61fe 24from mediagoblin.init.celery import setup_celery_from_config
f3f53028 25from mediagoblin.tools.pluginapi import PluginManager
1e48a830
CAW
26
27
e893094a 28OUR_MODULENAME = __name__
1e48a830 29
b1bb15d1 30_log = logging.getLogger(__name__)
b1bb15d1
JW
31
32
b4ab702f 33def setup_logging_from_paste_ini(loglevel, **kw):
b1bb15d1
JW
34 if os.path.exists(os.path.abspath('paste_local.ini')):
35 logging_conf_file = 'paste_local.ini'
36 else:
37 logging_conf_file = 'paste.ini'
38
86f0ff75
CAW
39 # allow users to set up explicitly which paste file to check via the
40 # PASTE_CONFIG environment variable
41 logging_conf_file = os.environ.get(
42 'PASTE_CONFIG', logging_conf_file)
43
b1bb15d1
JW
44 if not os.path.exists(logging_conf_file):
45 raise IOError('{0} does not exist. Logging can not be set up.'.format(
46 logging_conf_file))
47
9d7c69fb 48 logging.config.fileConfig(logging_conf_file)
b1bb15d1 49
f3f53028
JW
50 for callable_hook in \
51 PluginManager().get_hook_callables('celery_logging_setup'):
52 callable_hook()
53
b1bb15d1
JW
54
55setup_logging.connect(setup_logging_from_paste_ini)
56
1e48a830 57
9ea5c28b 58def setup_self(check_environ_for_conf=True, module_name=OUR_MODULENAME,
80dc071b 59 default_conf_file=None):
1e48a830
CAW
60 """
61 Transform this module into a celery config module by reading the
62 mediagoblin config file. Set the environment variable
924c586b 63 MEDIAGOBLIN_CONFIG to specify where this config file is.
1e48a830 64
924c586b 65 By default it defaults to 'mediagoblin.ini'.
1e48a830 66
924c586b
CAW
67 Note that if celery_setup_elsewhere is set in your config file,
68 this simply won't work.
1e48a830 69 """
a8acb5a3
CAW
70 if not default_conf_file:
71 if os.path.exists(os.path.abspath('mediagoblin_local.ini')):
72 default_conf_file = 'mediagoblin_local.ini'
73 else:
74 default_conf_file = 'mediagoblin.ini'
80dc071b 75
eaca7874
CAW
76 if check_environ_for_conf:
77 mgoblin_conf_file = os.path.abspath(
9ea5c28b 78 os.environ.get('MEDIAGOBLIN_CONFIG', default_conf_file))
eaca7874 79 else:
9ea5c28b 80 mgoblin_conf_file = default_conf_file
eaca7874 81
1e48a830
CAW
82 if not os.path.exists(mgoblin_conf_file):
83 raise IOError(
84 "MEDIAGOBLIN_CONFIG not set or file does not exist")
243c3843 85
924c586b
CAW
86 # By setting the environment variable here we should ensure that
87 # this is the module that gets set up.
eaca7874 88 os.environ['CELERY_CONFIG_MODULE'] = module_name
924c586b 89 app.MediaGoblinApp(mgoblin_conf_file, setup_celery=False)
9ea5c28b 90
1e48a830 91 setup_celery_from_config(
924c586b 92 mg_globals.app_config, mg_globals.global_config,
eaca7874 93 settings_module=module_name,
1e48a830
CAW
94 set_environ=False)
95
1e48a830
CAW
96
97if os.environ['CELERY_CONFIG_MODULE'] == OUR_MODULENAME:
98 setup_self()