__init__.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # -*- coding: utf-8 -*-
  2. """
  3. celery.app
  4. ~~~~~~~~~~
  5. Celery Application.
  6. :copyright: (c) 2009 - 2012 by Ask Solem.
  7. :license: BSD, see LICENSE for more details.
  8. """
  9. from __future__ import absolute_import
  10. import os
  11. from celery.local import Proxy
  12. from . import state
  13. from .state import ( # noqa
  14. set_default_app,
  15. get_current_app as current_app,
  16. get_current_task as current_task,
  17. )
  18. from .base import Celery, AppPickler # noqa
  19. #: Proxy always returning the app set as default.
  20. default_app = Proxy(lambda: state.default_app)
  21. #: Function returning the app provided or the default app if none.
  22. #:
  23. #: The environment variable :envvar:`CELERY_TRACE_APP` is used to
  24. #: trace app leaks. When enabled an exception is raised if there
  25. #: is no active app.
  26. app_or_default = None
  27. #: The "default" loader is the default loader used by old applications.
  28. default_loader = os.environ.get("CELERY_LOADER") or "default"
  29. #: Global fallback app instance.
  30. set_default_app(Celery("default", loader=default_loader,
  31. set_as_current=False,
  32. accept_magic_kwargs=True))
  33. def bugreport():
  34. return current_app().bugreport()
  35. def _app_or_default(app=None):
  36. if app is None:
  37. return state.get_current_app()
  38. return app
  39. def _app_or_default_trace(app=None): # pragma: no cover
  40. from traceback import print_stack
  41. from billiard import current_process
  42. if app is None:
  43. if getattr(state._tls, "current_app", None):
  44. print("-- RETURNING TO CURRENT APP --") # noqa+
  45. print_stack()
  46. return state._tls.current_app
  47. if current_process()._name == "MainProcess":
  48. raise Exception("DEFAULT APP")
  49. print("-- RETURNING TO DEFAULT APP --") # noqa+
  50. print_stack()
  51. return state.default_app
  52. return app
  53. def enable_trace():
  54. global app_or_default
  55. app_or_default = _app_or_default_trace
  56. def disable_trace():
  57. global app_or_default
  58. app_or_default = _app_or_default
  59. if os.environ.get("CELERY_TRACE_APP"): # pragma: no cover
  60. enable_trace()
  61. else:
  62. disable_trace()
  63. App = Celery # XXX Compat