σ
P'^c        	   @@  sw  d  Z  d d l m Z d d l Z d d l Z d d l Z d d l m Z d d l m	 Z	 d d l
 m Z d d l m Z d d l m Z m Z d d	 l m Z m Z d d
 l m Z d d l m Z d d l m Z m Z d d l m Z m Z m Z d d l m  Z  d d l! m" Z" d d l# m$ Z$ m% Z% d d d d d d g Z& e  e'  Z( e j) j* Z+ e j, j* Z- e j. j* Z/ e j0 Z0 e j1 Z1 e j2 Z2 e j3 Z3 e j4 Z4 e j5 Z5 e j6 Z6 e7 e2 e4 e3 g  Z8 d a: i  Z; d   Z< d e= f d     YZ> d d e? e> e@ e@ d e8 d  ZA i  d  ZB i  d d  ZC eC aD i  d  ZE d d  ZF d   ZG d   ZH d    ZI d!   ZJ d S("   s    
    celery.app.trace
    ~~~~~~~~~~~~~~~~

    This module defines how the task execution is traced:
    errors are recorded, handlers are applied and so on.

i    (   t   absolute_importN(   t   warn(   t   ExceptionInfo(   t   EncodeError(   t   kwdict(   t   current_appt   group(   t   statest   signals(   t   _task_stack(   t   set_default_app(   t   Taskt   Context(   t   Ignoret   Rejectt   Retry(   t
   get_logger(   t
   mro_lookup(   t   get_pickleable_exceptiont   get_pickleable_etypet	   TraceInfot   build_tracert
   trace_taskt   eager_trace_taskt   setup_worker_optimizationst   reset_worker_optimizationsc         C@  s%   t  |  j | d t t f d d g S(   sa   Return true if the task or one of its bases
    defines ``attr`` (excluding the one in BaseTask).t   stopt   monkey_patcheds   celery.app.task(   R   t	   __class__t   BaseTaskt   object(   t   taskt   attr(    (    s2   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/trace.pyt   task_has_custom?   s    c           B@  s>   e  Z d Z d d  Z e d  Z e d  Z e d  Z	 RS(   t   statet   retvalc         C@  s   | |  _  | |  _ d  S(   N(   R"   R#   (   t   selfR"   R#   (    (    s2   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/trace.pyt   __init__I   s    	c         C@  sG   | } | j  r | j } n  i |  j t 6|  j t 6|  j | d | S(   Nt   store_errors(   t   ignore_resultt   store_errors_even_if_ignoredt   handle_retryt   RETRYt   handle_failuret   FAILURER"   (   R$   R   t   eagerR&   (    (    s2   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/trace.pyt   handle_error_stateM   s    	

c   	   	   C@  sΗ   | j  } t j   \ } } } z |  j } t | | | f  } | rm | j j | j | j | j	 d | n  | j
 | j | j | j | j |  t j j d | d | d | d |  | SWd ~ Xd S(   s   Handle retry exception.t   requestt   sendert   reasont   einfoN(   R/   t   syst   exc_infoR#   R   t   backendt   mark_as_retryt   idt   exct	   tracebackt   on_retryt   argst   kwargsR   t
   task_retryt   send(	   R$   R   R&   t   reqt   type_t   _t   tbR1   R2   (    (    s2   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/trace.pyR)   W   s    			%c   	      C@  sτ   | j  } t j   \ } } } zΛ |  j } t   } t | j  | _ t | j  | _ | r | j	 j
 | j | | j d | n  | j | | j | j | j |  t j j d | d | j d | d | j d | j d | d |  | SWd	 ~ Xd	 S(
   s   Handle exception.R/   R0   t   task_idt	   exceptionR;   R<   R9   R2   N(   R/   R3   R4   R#   R   R   RD   R   t   typeR5   t   mark_as_failureR7   R9   t
   on_failureR;   R<   R   t   task_failureR>   (	   R$   R   R&   R?   R@   RA   RB   R8   R2   (    (    s2   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/trace.pyR+   k   s$    				"	(   R"   R#   N(
   t   __name__t
   __module__t	   __slots__t   NoneR%   t   FalseR.   t   TrueR)   R+   (    (    (    s2   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/trace.pyR   F   s
   
c
         @  sι  t   d  r  n  j  | p*  j }  j }
  j }  j   o\  j o\ |   oj |   p| t j    | j  | j	  d  d  t   d  rΈ  j  n  t   d  rΣ  j  n  |
 j  |
 j  t j     j } | j  | j  t j  t j  |
 j 	 t j j  t j j  t j j  d d l m } | j  t t       f d  
 d           	 
                f d  } | S(	   s7  Return a function that traces task execution; catches all
    exceptions and updates result backend with the state and result

    If the call was successful, it saves the result to the task result
    backend, and sets the task status to `"SUCCESS"`.

    If the call raises :exc:`~@Retry`, it extracts
    the original exception, uses that as the result and sets the task state
    to `"RETRY"`.

    If the call results in an exception, it saves the exception as the task
    result, and sets the task state to `"FAILURE"`.

    Return a function that takes the following arguments:

        :param uuid: The id of the task.
        :param args: List of positional args to pass on to the function.
        :param kwargs: Keyword arguments mapping to pass on to the function.
        :keyword request: Request dict.

    t   __call__t
   on_successt   after_returni    (   t   canvasc         @  s    r   n    | |  } | j   d  } | r} t g  |  j pH g  D] }  | d  ^ qI d  j | f  n  | | | j | j f S(   NR-   t   app(   R.   R   t   errbackst   apply_asyncR"   R#   (   R/   R8   t   uuidR"   t   call_errbackst   It   Rt   errback(   t   InfoRS   R-   t	   propagatet	   signatureR   (    s2   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/trace.pyt   on_errorΕ   s    4c         @  s·  d  } } } } t |  } yK   t | p7 i  d | d t d | }  |  zX r t d  d |  d  d | d |  n   |     rΔ  |  i  d 6 d 6t d	 | n  y  | |   } } t } Wnt k
 r(}	  t |	  t	 d
 t
  } } | j | j } } n;t k
 rl}	  t |	  t	 d
 t
  } } | j | j } } nχt k
 r₯}	 
 | |	 |  t d t \ } } } } nΎt k
 rΥ}	 
 | |	 |   \ } } } } nt k
 rκ}	   nyXy j j }
 |
 rΧt  j j  d k r·g  g  } } xL |
 D]D }  | d  } t | t  rc| j |  q,| j |  q,Wx | D] } | j | f  q{W| rΤt |  j | f  qΤqΧ |
 d d  j |  n   rφ |  | t d	 | n  Wn1 t k
 r*}	 
 | |	 |   \ } } } } n9 X rG | |  | |  n   rct d  d |  n  |   k r°| j r	  | |  n   r° | | |  | | d   q°n  Wd  z>  rρt d  d |  d  d | d | d | d |  n  Wd         sfy       Wqft t  t! f k
 r:  qft k
 rb}	 t" j# d |	 d t
 qfXn  XXWnA t! k
 r  n. t k
 r¬}	  r  n  t$  |	  } n X| | f S(   NR;   t   called_directlyR<   R0   RC   R   t   pidt   hostnameR/   t   internalRW   i   RS   i    t   resultR#   R"   s   Process cleanup failed: %rR4   (%   RL   R   R   RM   t   send_prerunt   STARTEDt   SUCCESSR   t   REJECTEDR   RN   R"   R#   R   t   IGNOREDR   R*   t	   Exceptiont   BaseExceptionR/   t	   callbackst   lent
   isinstanceR   t   appendRU   t   delayR   t   send_successt   chordt   send_postrunt   KeyboardInterruptt
   SystemExitt   MemoryErrort   _loggert   errort   report_internal_error(   RV   R;   R<   R/   RY   RX   R#   R"   t   task_requestR8   Rk   t   sigst   groupst   sigt   group_(   t   IGNORE_STATESR[   RS   t   backend_cleanupR-   t   funRa   t   loader_cleanupt   loader_task_initt   on_chord_part_returnR^   R`   t   pop_requestt   pop_taskt   postrun_receiverst   prerun_receiverst   publish_resultt   push_requestt	   push_taskR]   t   store_resultt   success_receiversR   t   task_after_returnt   task_on_successt   track_started(    s2   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/trace.pyR   Ρ   sͺ    




'! "	 N(    R!   t   runt   loaderR5   R'   R   t   sockett   gethostnamet   on_task_initt   on_process_cleanupRL   RP   RQ   R   t   process_cleanupt   ost   getpidt   request_stackt   pusht   popR	   R   R   t   task_prerunt	   receiverst   task_postrunt   task_successt   celeryRR   t   maybe_signatureR,   RN   (   t   nameR   R   Ra   R&   R[   R-   R\   RS   R~   R5   R'   R   RR   R   (    (   R~   R[   RS   R   R-   R   Ra   R   R   R   R^   R`   R   R   R   R   R\   R   R   R   R]   R   R   R   R   R   R   s2   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/trace.pyR      sB    														$]vc         K@  sl   yH |  j  d  k r- t |  j |  |  |  _  n  |  j  | | | |  d SWn t k
 rg } t |  |  SXd  S(   Ni    (   t	   __trace__RL   R   R’   Ri   Rx   (   R   RV   R;   R<   R/   t   optsR8   (    (    s2   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/trace.pyR   J  s    c      	   K@  s2   | p	 t  } t | j |  | | | | d | | S(   NRS   (   R   R   t   tasks(   R’   RV   R;   R<   R/   RS   R€   (    (    s2   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/trace.pyt   _trace_task_retS  s    c         C@  s   t  |  j | | | |  d S(   Ni    (   t   _tasksR£   (   R   RV   R;   R<   R/   (    (    s2   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/trace.pyt   _fast_trace_taskZ  s    c         K@  s2   | j  d t  t |  j |  |  | | | |  S(   NR-   (   t
   setdefaultRN   R   R’   (   R   RV   R;   R<   R/   R€   (    (    s2   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/trace.pyR   `  s    c         C@  sw   t  j   \ } } } zW |  j j | d  } t | | | f d t } t t d j | | j	    | SWd  ~ Xd  S(   Nt   pickleRb   s)   Exception raised outside body: {0!r}:
{1}(
   R3   R4   R5   t   prepare_exceptionR   RN   R   t   RuntimeWarningt   formatR9   (   R   R8   t   _typet   _valuet   _tbR4   (    (    s2   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/trace.pyRx   f  s    c         C@  s[   t    |  j   t |   |  j   |  j a t a d d l m } t | _ | j	   d  S(   Ni    (   t   job(
   t   _install_stack_protectiont   set_currentR
   t   finalizeR§   R¨   t   trace_task_rett   celery.workerR±   t   __optimize__(   RS   t
   job_module(    (    s2   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/trace.pyR   s  s    


		c          C@  sr   t  a y t t d  Wn t k
 r* n Xy t j d  t _ Wn t k
 rT n Xd d l	 m
 }  t  |  _ d  S(   Nt   _stackprotecteds   BaseTask.__call__i    (   R±   (   R¦   R΅   t   delattrR   t   AttributeErrort   _patchedR   RO   t   KeyErrorRΆ   R±   (   RΈ   (    (    s2   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/trace.pyR     s    c          @  sK   t  t d t  sG t j t d <    f d   }  |  t _ t t _ n  d  S(   NRΉ   s   BaseTask.__call__c         @  sg   |  j  } | j } | rW | j rW t |  d k rW | j rW d | _ |  j | |   S  |  | |  S(   Ni   (   R   t   topt
   _protectedRl   R_   R   (   R$   R;   R<   t   stackR?   (   t   orig(    s2   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/trace.pyt   __protected_call__°  s    			(   t   getattrR   RM   RO   RΌ   RN   RΉ   (   RΒ   (    (   RΑ   s2   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/trace.pyR²     s
    	(K   t   __doc__t
   __future__R    R   R   R3   t   warningsR   t   billiard.einfoR   t   kombu.exceptionsR   t   kombu.utilsR   R    R   R   R   R   t   celery._stateR	   t
   celery.appR
   t   celery.app.taskR   R   R   t   celery.exceptionsR   R   R   t   celery.utils.logR   t   celery.utils.objectsR   t   celery.utils.serializationR   R   t   __all__RI   Rv   R   R>   Rd   R   Rr   R   Rp   Re   Rf   Rh   Rg   R*   R,   t   EXCEPTION_STATESt	   frozensetR~   RL   R§   RΌ   R!   R   R   RN   RM   R   R   R¦   R΅   R¨   R   Rx   R   R   R²   (    (    (    s2   /tmp/pip-unpacked-wheel-gV1wwp/celery/app/trace.pyt   <module>	   s^   								=	Ε				