| 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 logging |
| 19 | import logging.config |
| 20 | |
| 21 | from celery.signals import setup_logging |
| 22 | |
| 23 | from mediagoblin import app, mg_globals |
| 24 | from mediagoblin.init.celery import setup_celery_from_config |
| 25 | |
| 26 | |
| 27 | OUR_MODULENAME = __name__ |
| 28 | |
| 29 | _log = logging.getLogger(__name__) |
| 30 | |
| 31 | |
| 32 | def setup_logging_from_paste_ini(loglevel, **kw): |
| 33 | if os.path.exists(os.path.abspath('paste_local.ini')): |
| 34 | logging_conf_file = 'paste_local.ini' |
| 35 | else: |
| 36 | logging_conf_file = 'paste.ini' |
| 37 | |
| 38 | # allow users to set up explicitly which paste file to check via the |
| 39 | # PASTE_CONFIG environment variable |
| 40 | logging_conf_file = os.environ.get( |
| 41 | 'PASTE_CONFIG', logging_conf_file) |
| 42 | |
| 43 | if not os.path.exists(logging_conf_file): |
| 44 | raise IOError('{0} does not exist. Logging can not be set up.'.format( |
| 45 | logging_conf_file)) |
| 46 | |
| 47 | logging.config.fileConfig(logging_conf_file) |
| 48 | |
| 49 | |
| 50 | setup_logging.connect(setup_logging_from_paste_ini) |
| 51 | |
| 52 | |
| 53 | def setup_self(check_environ_for_conf=True, module_name=OUR_MODULENAME, |
| 54 | default_conf_file=None): |
| 55 | """ |
| 56 | Transform this module into a celery config module by reading the |
| 57 | mediagoblin config file. Set the environment variable |
| 58 | MEDIAGOBLIN_CONFIG to specify where this config file is. |
| 59 | |
| 60 | By default it defaults to 'mediagoblin.ini'. |
| 61 | |
| 62 | Note that if celery_setup_elsewhere is set in your config file, |
| 63 | this simply won't work. |
| 64 | """ |
| 65 | if not default_conf_file: |
| 66 | if os.path.exists(os.path.abspath('mediagoblin_local.ini')): |
| 67 | default_conf_file = 'mediagoblin_local.ini' |
| 68 | else: |
| 69 | default_conf_file = 'mediagoblin.ini' |
| 70 | |
| 71 | if check_environ_for_conf: |
| 72 | mgoblin_conf_file = os.path.abspath( |
| 73 | os.environ.get('MEDIAGOBLIN_CONFIG', default_conf_file)) |
| 74 | else: |
| 75 | mgoblin_conf_file = default_conf_file |
| 76 | |
| 77 | if not os.path.exists(mgoblin_conf_file): |
| 78 | raise IOError( |
| 79 | "MEDIAGOBLIN_CONFIG not set or file does not exist") |
| 80 | |
| 81 | # By setting the environment variable here we should ensure that |
| 82 | # this is the module that gets set up. |
| 83 | os.environ['CELERY_CONFIG_MODULE'] = module_name |
| 84 | app.MediaGoblinApp(mgoblin_conf_file, setup_celery=False) |
| 85 | |
| 86 | setup_celery_from_config( |
| 87 | mg_globals.app_config, mg_globals.global_config, |
| 88 | settings_module=module_name, |
| 89 | set_environ=False) |
| 90 | |
| 91 | |
| 92 | if os.environ['CELERY_CONFIG_MODULE'] == OUR_MODULENAME: |
| 93 | setup_self() |