__init__.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 sys
  18. import datetime
  19. import logging
  20. import six
  21. from celery import Celery
  22. from mediagoblin.tools.pluginapi import hook_runall
  23. _log = logging.getLogger(__name__)
  24. MANDATORY_CELERY_IMPORTS = [
  25. 'mediagoblin.processing.task',
  26. 'mediagoblin.notifications.task',
  27. 'mediagoblin.submit.task',
  28. ]
  29. DEFAULT_SETTINGS_MODULE = 'mediagoblin.init.celery.dummy_settings_module'
  30. def get_celery_settings_dict(app_config, global_config,
  31. force_celery_always_eager=False):
  32. """
  33. Get a celery settings dictionary from reading the config
  34. """
  35. if 'celery' in global_config:
  36. celery_conf = global_config['celery']
  37. else:
  38. celery_conf = {}
  39. celery_settings = {}
  40. # Add all celery settings from config
  41. for key, value in six.iteritems(celery_conf):
  42. celery_settings[key] = value
  43. # TODO: use default result stuff here if it exists
  44. # add mandatory celery imports
  45. celery_imports = celery_settings.setdefault('CELERY_IMPORTS', [])
  46. celery_imports.extend(MANDATORY_CELERY_IMPORTS)
  47. if force_celery_always_eager:
  48. celery_settings['CELERY_ALWAYS_EAGER'] = True
  49. celery_settings['CELERY_EAGER_PROPAGATES_EXCEPTIONS'] = True
  50. # Garbage collection periodic task
  51. frequency = app_config.get('garbage_collection', 60)
  52. if frequency:
  53. frequency = int(frequency)
  54. celery_settings['CELERYBEAT_SCHEDULE'] = {
  55. 'garbage-collection': {
  56. 'task': 'mediagoblin.submit.task.collect_garbage',
  57. 'schedule': datetime.timedelta(minutes=frequency),
  58. }
  59. }
  60. celery_settings['BROKER_HEARTBEAT'] = 1
  61. return celery_settings
  62. def setup_celery_app(app_config, global_config,
  63. settings_module=DEFAULT_SETTINGS_MODULE,
  64. force_celery_always_eager=False):
  65. """
  66. Setup celery without using terrible setup-celery-module hacks.
  67. """
  68. celery_settings = get_celery_settings_dict(
  69. app_config, global_config, force_celery_always_eager)
  70. celery_app = Celery()
  71. celery_app.config_from_object(celery_settings)
  72. hook_runall('celery_setup', celery_app)
  73. def setup_celery_from_config(app_config, global_config,
  74. settings_module=DEFAULT_SETTINGS_MODULE,
  75. force_celery_always_eager=False,
  76. set_environ=True):
  77. """
  78. Take a mediagoblin app config and try to set up a celery settings
  79. module from this.
  80. Args:
  81. - app_config: the application config section
  82. - global_config: the entire ConfigObj loaded config, all sections
  83. - settings_module: the module to populate, as a string
  84. - force_celery_always_eager: whether or not to force celery into
  85. always eager mode; good for development and small installs
  86. - set_environ: if set, this will CELERY_CONFIG_MODULE to the
  87. settings_module
  88. """
  89. celery_settings = get_celery_settings_dict(
  90. app_config, global_config, force_celery_always_eager)
  91. __import__(settings_module)
  92. this_module = sys.modules[settings_module]
  93. for key, value in six.iteritems(celery_settings):
  94. setattr(this_module, key, value)
  95. if set_environ:
  96. os.environ['CELERY_CONFIG_MODULE'] = settings_module
  97. # Replace the default celery.current_app.conf if celery has already been
  98. # initiated
  99. from celery import current_app
  100. _log.info('Setting celery configuration from object "{0}"'.format(
  101. settings_module))
  102. current_app.config_from_object(this_module)
  103. _log.debug('Celery broker host: {0}'.format(current_app.conf['BROKER_HOST']))