trace.py 15 KB

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