__init__.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. # -*- coding: utf-8 -*-
  2. """
  3. celery.app
  4. ~~~~~~~~~~
  5. Celery Application.
  6. """
  7. from __future__ import absolute_import
  8. import os
  9. from celery.local import Proxy
  10. from celery import _state
  11. from celery._state import ( # noqa
  12. set_default_app,
  13. get_current_app as current_app,
  14. get_current_task as current_task,
  15. _get_active_apps,
  16. )
  17. from celery.utils import gen_task_name
  18. from .builtins import shared_task as _shared_task
  19. from .base import Celery, AppPickler # noqa
  20. #: Proxy always returning the app set as default.
  21. default_app = Proxy(lambda: _state.default_app)
  22. #: Function returning the app provided or the default app if none.
  23. #:
  24. #: The environment variable :envvar:`CELERY_TRACE_APP` is used to
  25. #: trace app leaks. When enabled an exception is raised if there
  26. #: is no active app.
  27. app_or_default = None
  28. #: The 'default' loader is the default loader used by old applications.
  29. default_loader = os.environ.get('CELERY_LOADER') or 'default'
  30. def bugreport():
  31. return current_app().bugreport()
  32. def _app_or_default(app=None):
  33. if app is None:
  34. return _state.get_current_app()
  35. return app
  36. def _app_or_default_trace(app=None): # pragma: no cover
  37. from traceback import print_stack
  38. from billiard import current_process
  39. if app is None:
  40. if getattr(_state._tls, 'current_app', None):
  41. print('-- RETURNING TO CURRENT APP --') # noqa+
  42. print_stack()
  43. return _state._tls.current_app
  44. if current_process()._name == 'MainProcess':
  45. raise Exception('DEFAULT APP')
  46. print('-- RETURNING TO DEFAULT APP --') # noqa+
  47. print_stack()
  48. return _state.default_app
  49. return app
  50. def enable_trace():
  51. global app_or_default
  52. app_or_default = _app_or_default_trace
  53. def disable_trace():
  54. global app_or_default
  55. app_or_default = _app_or_default
  56. if os.environ.get('CELERY_TRACE_APP'): # pragma: no cover
  57. enable_trace()
  58. else:
  59. disable_trace()
  60. App = Celery # XXX Compat
  61. def shared_task(*args, **kwargs):
  62. """Task decorator that creates shared tasks,
  63. and returns a proxy that always returns the task from the current apps
  64. task registry.
  65. This can be used by library authors to create tasks that will work
  66. for any app environment.
  67. Example:
  68. >>> from celery import Celery, shared_task
  69. >>> @shared_task
  70. ... def add(x, y):
  71. ... return x + y
  72. >>> app1 = Celery(broker='amqp://')
  73. >>> add.app is app1
  74. True
  75. >>> app2 = Celery(broker='redis://')
  76. >>> add.app is app2
  77. """
  78. def create_shared_task(**options):
  79. def __inner(fun):
  80. name = options.get('name')
  81. # Set as shared task so that unfinalized apps,
  82. # and future apps will load the task.
  83. _shared_task(lambda app: app._task_from_fun(fun, **options))
  84. # Force all finalized apps to take this task as well.
  85. for app in _get_active_apps():
  86. if app.finalized:
  87. with app._finalize_mutex:
  88. app._task_from_fun(fun, **options)
  89. # Returns a proxy that always gets the task from the current
  90. # apps task registry.
  91. def task_by_cons():
  92. app = current_app()
  93. return app.tasks[name or gen_task_name(app,
  94. fun.__name__, fun.__module__)]
  95. return Proxy(task_by_cons)
  96. return __inner
  97. if len(args) == 1 and callable(args[0]):
  98. return create_shared_task(**kwargs)(args[0])
  99. return create_shared_task(**kwargs)