default.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. from __future__ import absolute_import
  2. import os
  3. import warnings
  4. from importlib import import_module
  5. from celery.datastructures import AttributeDict
  6. from celery.exceptions import NotConfigured
  7. from celery.loaders.base import BaseLoader
  8. from celery.utils import find_module
  9. DEFAULT_CONFIG_MODULE = "celeryconfig"
  10. class Loader(BaseLoader):
  11. """The loader used by the default app."""
  12. def setup_settings(self, settingsdict):
  13. return AttributeDict(settingsdict)
  14. def find_module(self, module):
  15. return find_module(module)
  16. def read_configuration(self):
  17. """Read configuration from :file:`celeryconfig.py` and configure
  18. celery and Django so it can be used by regular Python."""
  19. configname = os.environ.get("CELERY_CONFIG_MODULE",
  20. DEFAULT_CONFIG_MODULE)
  21. try:
  22. self.find_module(configname)
  23. except ImportError:
  24. warnings.warn(NotConfigured(
  25. "No %r module found! Please make sure it exists and "
  26. "is available to Python." % (configname, )))
  27. return self.setup_settings({})
  28. else:
  29. celeryconfig = self.import_from_cwd(configname)
  30. usercfg = dict((key, getattr(celeryconfig, key))
  31. for key in dir(celeryconfig)
  32. if self.wanted_module_item(key))
  33. self.configured = True
  34. return self.setup_settings(usercfg)
  35. def wanted_module_item(self, item):
  36. return item[0].isupper() and not item.startswith("_")
  37. def on_worker_init(self):
  38. """Imports modules at worker init so tasks can be registered
  39. and used by the worked.
  40. The list of modules to import is taken from the
  41. :setting:`CELERY_IMPORTS` setting.
  42. """
  43. self.import_default_modules()