from_celery.py 3.2 KB

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