trace.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. # -*- coding: utf-8 -*-
  2. """
  3. celery.app.trace
  4. ~~~~~~~~~~~~~~~~
  5. This module defines how the task execution is traced:
  6. errors are recorded, handlers are applied and so on.
  7. """
  8. from __future__ import absolute_import
  9. # ## ---
  10. # This is the heart of the worker, the inner loop so to speak.
  11. # It used to be split up into nice little classes and methods,
  12. # but in the end it only resulted in bad performance and horrible tracebacks,
  13. # so instead we now use one closure per task class.
  14. import os
  15. import socket
  16. import sys
  17. from warnings import warn
  18. from billiard.einfo import ExceptionInfo
  19. from kombu.utils import kwdict
  20. from celery import current_app
  21. from celery import states, signals
  22. from celery._state import _task_stack
  23. from celery.app import set_default_app
  24. from celery.app.task import Task as BaseTask, Context
  25. from celery.exceptions import Ignore, Reject, Retry
  26. from celery.utils.log import get_logger
  27. from celery.utils.objects import mro_lookup
  28. from celery.utils.serialization import (
  29. get_pickleable_exception,
  30. get_pickleable_etype,
  31. )
  32. __all__ = ['TraceInfo', 'build_tracer', 'trace_task', 'eager_trace_task',
  33. 'setup_worker_optimizations', 'reset_worker_optimizations']
  34. _logger = get_logger(__name__)
  35. send_prerun = signals.task_prerun.send
  36. send_postrun = signals.task_postrun.send
  37. send_success = signals.task_success.send
  38. STARTED = states.STARTED
  39. SUCCESS = states.SUCCESS
  40. IGNORED = states.IGNORED
  41. REJECTED = states.REJECTED
  42. RETRY = states.RETRY
  43. FAILURE = states.FAILURE
  44. EXCEPTION_STATES = states.EXCEPTION_STATES
  45. IGNORE_STATES = frozenset([IGNORED, RETRY, REJECTED])
  46. #: set by :func:`setup_worker_optimizations`
  47. _tasks = None
  48. _patched = {}
  49. def task_has_custom(task, attr):
  50. """Return true if the task or one of its bases
  51. defines ``attr`` (excluding the one in BaseTask)."""
  52. return mro_lookup(task.__class__, attr, stop=(BaseTask, object),
  53. monkey_patched=['celery.app.task'])
  54. class TraceInfo(object):
  55. __slots__ = ('state', 'retval')
  56. def __init__(self, state, retval=None):
  57. self.state = state
  58. self.retval = retval
  59. def handle_error_state(self, task, eager=False):
  60. store_errors = not eager
  61. if task.ignore_result:
  62. store_errors = task.store_errors_even_if_ignored
  63. return {
  64. RETRY: self.handle_retry,
  65. FAILURE: self.handle_failure,
  66. }[self.state](task, store_errors=store_errors)
  67. def handle_retry(self, task, store_errors=True):
  68. """Handle retry exception."""
  69. # the exception raised is the Retry semi-predicate,
  70. # and it's exc' attribute is the original exception raised (if any).
  71. req = task.request
  72. type_, _, tb = sys.exc_info()
  73. try:
  74. reason = self.retval
  75. einfo = ExceptionInfo((type_, reason, tb))
  76. if store_errors:
  77. task.backend.mark_as_retry(
  78. req.id, reason.exc, einfo.traceback, request=req,
  79. )
  80. task.on_retry(reason.exc, req.id, req.args, req.kwargs, einfo)
  81. signals.task_retry.send(sender=task, request=req,
  82. reason=reason, einfo=einfo)
  83. return einfo
  84. finally:
  85. del(tb)
  86. def handle_failure(self, task, store_errors=True):
  87. """Handle exception."""
  88. req = task.request
  89. type_, _, tb = sys.exc_info()
  90. try:
  91. exc = self.retval
  92. einfo = ExceptionInfo()
  93. einfo.exception = get_pickleable_exception(einfo.exception)
  94. einfo.type = get_pickleable_etype(einfo.type)
  95. if store_errors:
  96. task.backend.mark_as_failure(
  97. req.id, exc, einfo.traceback, request=req,
  98. )
  99. task.on_failure(exc, req.id, req.args, req.kwargs, einfo)
  100. signals.task_failure.send(sender=task, task_id=req.id,
  101. exception=exc, args=req.args,
  102. kwargs=req.kwargs,
  103. traceback=tb,
  104. einfo=einfo)
  105. return einfo
  106. finally:
  107. del(tb)
  108. def build_tracer(name, task, loader=None, hostname=None, store_errors=True,
  109. Info=TraceInfo, eager=False, propagate=False, app=None,
  110. IGNORE_STATES=IGNORE_STATES):
  111. """Return a function that traces task execution; catches all
  112. exceptions and updates result backend with the state and result
  113. If the call was successful, it saves the result to the task result
  114. backend, and sets the task status to `"SUCCESS"`.
  115. If the call raises :exc:`~@Retry`, it extracts
  116. the original exception, uses that as the result and sets the task state
  117. to `"RETRY"`.
  118. If the call results in an exception, it saves the exception as the task
  119. result, and sets the task state to `"FAILURE"`.
  120. Return a function that takes the following arguments:
  121. :param uuid: The id of the task.
  122. :param args: List of positional args to pass on to the function.
  123. :param kwargs: Keyword arguments mapping to pass on to the function.
  124. :keyword request: Request dict.
  125. """
  126. # If the task doesn't define a custom __call__ method
  127. # we optimize it away by simply calling the run method directly,
  128. # saving the extra method call and a line less in the stack trace.
  129. fun = task if task_has_custom(task, '__call__') else task.run
  130. loader = loader or app.loader
  131. backend = task.backend
  132. ignore_result = task.ignore_result
  133. track_started = task.track_started
  134. track_started = not eager and (task.track_started and not ignore_result)
  135. publish_result = not eager and not ignore_result
  136. hostname = hostname or socket.gethostname()
  137. loader_task_init = loader.on_task_init
  138. loader_cleanup = loader.on_process_cleanup
  139. task_on_success = None
  140. task_after_return = None
  141. if task_has_custom(task, 'on_success'):
  142. task_on_success = task.on_success
  143. if task_has_custom(task, 'after_return'):
  144. task_after_return = task.after_return
  145. store_result = backend.store_result
  146. backend_cleanup = backend.process_cleanup
  147. pid = os.getpid()
  148. request_stack = task.request_stack
  149. push_request = request_stack.push
  150. pop_request = request_stack.pop
  151. push_task = _task_stack.push
  152. pop_task = _task_stack.pop
  153. on_chord_part_return = backend.on_chord_part_return
  154. prerun_receivers = signals.task_prerun.receivers
  155. postrun_receivers = signals.task_postrun.receivers
  156. success_receivers = signals.task_success.receivers
  157. from celery import canvas
  158. signature = canvas.maybe_signature # maybe_ does not clone if already
  159. def trace_task(uuid, args, kwargs, request=None):
  160. R = I = None
  161. kwargs = kwdict(kwargs)
  162. try:
  163. push_task(task)
  164. task_request = Context(request or {}, args=args,
  165. called_directly=False, kwargs=kwargs)
  166. push_request(task_request)
  167. try:
  168. # -*- PRE -*-
  169. if prerun_receivers:
  170. send_prerun(sender=task, task_id=uuid, task=task,
  171. args=args, kwargs=kwargs)
  172. loader_task_init(uuid, task)
  173. if track_started:
  174. store_result(
  175. uuid, {'pid': pid, 'hostname': hostname}, STARTED,
  176. request=task_request,
  177. )
  178. # -*- TRACE -*-
  179. try:
  180. R = retval = fun(*args, **kwargs)
  181. state = SUCCESS
  182. except Reject as exc:
  183. I, R = Info(REJECTED, exc), ExceptionInfo(internal=True)
  184. state, retval = I.state, I.retval
  185. except Ignore as exc:
  186. I, R = Info(IGNORED, exc), ExceptionInfo(internal=True)
  187. state, retval = I.state, I.retval
  188. except Retry as exc:
  189. I = Info(RETRY, exc)
  190. state, retval = I.state, I.retval
  191. R = I.handle_error_state(task, eager=eager)
  192. except Exception as exc:
  193. if propagate:
  194. raise
  195. I = Info(FAILURE, exc)
  196. state, retval = I.state, I.retval
  197. R = I.handle_error_state(task, eager=eager)
  198. [signature(errback, app=app).apply_async((uuid, ))
  199. for errback in task_request.errbacks or []]
  200. except BaseException as exc:
  201. raise
  202. else:
  203. # callback tasks must be applied before the result is
  204. # stored, so that result.children is populated.
  205. [signature(callback, app=app).apply_async((retval, ))
  206. for callback in task_request.callbacks or []]
  207. if publish_result:
  208. store_result(
  209. uuid, retval, SUCCESS, request=task_request,
  210. )
  211. if task_on_success:
  212. task_on_success(retval, uuid, args, kwargs)
  213. if success_receivers:
  214. send_success(sender=task, result=retval)
  215. # -* POST *-
  216. if state not in IGNORE_STATES:
  217. if task_request.chord:
  218. on_chord_part_return(task)
  219. if task_after_return:
  220. task_after_return(
  221. state, retval, uuid, args, kwargs, None,
  222. )
  223. if postrun_receivers:
  224. send_postrun(sender=task, task_id=uuid, task=task,
  225. args=args, kwargs=kwargs,
  226. retval=retval, state=state)
  227. finally:
  228. pop_task()
  229. pop_request()
  230. if not eager:
  231. try:
  232. backend_cleanup()
  233. loader_cleanup()
  234. except (KeyboardInterrupt, SystemExit, MemoryError):
  235. raise
  236. except Exception as exc:
  237. _logger.error('Process cleanup failed: %r', exc,
  238. exc_info=True)
  239. except MemoryError:
  240. raise
  241. except Exception as exc:
  242. if eager:
  243. raise
  244. R = report_internal_error(task, exc)
  245. return R, I
  246. return trace_task
  247. def trace_task(task, uuid, args, kwargs, request={}, **opts):
  248. try:
  249. if task.__trace__ is None:
  250. task.__trace__ = build_tracer(task.name, task, **opts)
  251. return task.__trace__(uuid, args, kwargs, request)[0]
  252. except Exception as exc:
  253. return report_internal_error(task, exc)
  254. def _trace_task_ret(name, uuid, args, kwargs, request={}, app=None, **opts):
  255. return trace_task((app or current_app).tasks[name],
  256. uuid, args, kwargs, request, app=app, **opts)
  257. trace_task_ret = _trace_task_ret
  258. def _fast_trace_task(task, uuid, args, kwargs, request={}):
  259. # setup_worker_optimizations will point trace_task_ret to here,
  260. # so this is the function used in the worker.
  261. return _tasks[task].__trace__(uuid, args, kwargs, request)[0]
  262. def eager_trace_task(task, uuid, args, kwargs, request=None, **opts):
  263. opts.setdefault('eager', True)
  264. return build_tracer(task.name, task, **opts)(
  265. uuid, args, kwargs, request)
  266. def report_internal_error(task, exc):
  267. _type, _value, _tb = sys.exc_info()
  268. try:
  269. _value = task.backend.prepare_exception(exc)
  270. exc_info = ExceptionInfo((_type, _value, _tb), internal=True)
  271. warn(RuntimeWarning(
  272. 'Exception raised outside body: {0!r}:\n{1}'.format(
  273. exc, exc_info.traceback)))
  274. return exc_info
  275. finally:
  276. del(_tb)
  277. def setup_worker_optimizations(app):
  278. global _tasks
  279. global trace_task_ret
  280. # make sure custom Task.__call__ methods that calls super
  281. # will not mess up the request/task stack.
  282. _install_stack_protection()
  283. # all new threads start without a current app, so if an app is not
  284. # passed on to the thread it will fall back to the "default app",
  285. # which then could be the wrong app. So for the worker
  286. # we set this to always return our app. This is a hack,
  287. # and means that only a single app can be used for workers
  288. # running in the same process.
  289. app.set_current()
  290. set_default_app(app)
  291. # evaluate all task classes by finalizing the app.
  292. app.finalize()
  293. # set fast shortcut to task registry
  294. _tasks = app._tasks
  295. trace_task_ret = _fast_trace_task
  296. from celery.worker import job as job_module
  297. job_module.trace_task_ret = _fast_trace_task
  298. job_module.__optimize__()
  299. def reset_worker_optimizations():
  300. global trace_task_ret
  301. trace_task_ret = _trace_task_ret
  302. try:
  303. delattr(BaseTask, '_stackprotected')
  304. except AttributeError:
  305. pass
  306. try:
  307. BaseTask.__call__ = _patched.pop('BaseTask.__call__')
  308. except KeyError:
  309. pass
  310. from celery.worker import job as job_module
  311. job_module.trace_task_ret = _trace_task_ret
  312. def _install_stack_protection():
  313. # Patches BaseTask.__call__ in the worker to handle the edge case
  314. # where people override it and also call super.
  315. #
  316. # - The worker optimizes away BaseTask.__call__ and instead
  317. # calls task.run directly.
  318. # - so with the addition of current_task and the request stack
  319. # BaseTask.__call__ now pushes to those stacks so that
  320. # they work when tasks are called directly.
  321. #
  322. # The worker only optimizes away __call__ in the case
  323. # where it has not been overridden, so the request/task stack
  324. # will blow if a custom task class defines __call__ and also
  325. # calls super().
  326. if not getattr(BaseTask, '_stackprotected', False):
  327. _patched['BaseTask.__call__'] = orig = BaseTask.__call__
  328. def __protected_call__(self, *args, **kwargs):
  329. stack = self.request_stack
  330. req = stack.top
  331. if req and not req._protected and \
  332. len(stack) == 1 and not req.called_directly:
  333. req._protected = 1
  334. return self.run(*args, **kwargs)
  335. return orig(self, *args, **kwargs)
  336. BaseTask.__call__ = __protected_call__
  337. BaseTask._stackprotected = True