base.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. BUILTIN_MODULES = ["celery.task"]
  2. class BaseLoader(object):
  3. """The base class for loaders.
  4. Loaders handles to following things:
  5. * Reading celery client/worker configurations.
  6. * What happens when a task starts?
  7. See :meth:`on_task_init`.
  8. * What happens when the worker starts?
  9. See :meth:`on_worker_init`.
  10. * What modules are imported to find tasks?
  11. """
  12. _conf_cache = None
  13. worker_initialized = False
  14. override_backends = {}
  15. def on_task_init(self, task_id, task):
  16. """This method is called before a task is executed."""
  17. pass
  18. def on_worker_init(self):
  19. """This method is called when the worker (``celeryd``) starts."""
  20. pass
  21. def import_task_module(self, module):
  22. return __import__(module, [], [], [''])
  23. def import_default_modules(self):
  24. imports = getattr(self.conf, "CELERY_IMPORTS", None) or []
  25. imports = set(list(imports) + BUILTIN_MODULES)
  26. return map(self.import_task_module, imports)
  27. def init_worker(self):
  28. if not self.worker_initialized:
  29. self.worker_initialized = True
  30. self.on_worker_init()
  31. @property
  32. def conf(self):
  33. """Loader configuration."""
  34. if not self._conf_cache:
  35. self._conf_cache = self.read_configuration()
  36. return self._conf_cache