
P'^c           @@ sT  d  Z  d d l m Z m 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 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 m Z m Z m Z m Z m Z m Z d d l m  Z  m! Z! d d	 l" 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- m. Z. d d l/ m0 Z0 d d l1 m2 Z2 d g Z3 e4 e d  Z5 e( e6  Z7 e7 j8 e7 j9 e7 j: e7 j; f \ Z8 Z9 Z< Z; e= a> e= a? d   Z@ e@   e- jA ZA e jB jC ZD e2 jE ZE e2 jF ZF e2 jG ZH e  d eI f d     Y ZJ e eH eF d  ZK d S(   uV   This module defines the :class:`Request` class, that specifies
how tasks are executed.i    (   t   absolute_importt   unicode_literalsN(   t   datetime(   t   ref(   t   TERM_SIGNAME(   t	   safe_reprt   safe_str(   t   cached_property(   t   signals(   t
   trace_taskt   trace_task_ret(	   t   Ignoret   TaskRevokedErrort   InvalidTaskErrort   SoftTimeLimitExceededt   TimeLimitExceededt   WorkerLostErrort
   Terminatedt   Retryt   Reject(   t   python_2_unicode_compatiblet   string(   t   maybet   noop(   t
   get_logger(   t   gethostname(   t   maybe_iso8601t   timezonet   maybe_make_aware(   t   get_pickled_exceptioni   (   t   stateu   Requestu   pypy_version_infoc           C@ s(   t  j t j  a t  j t j  a d  S(   N(   t   loggert   isEnabledFort   loggingt   DEBUGt   _does_debugt   INFOt
   _does_info(    (    (    s7   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/request.pyt   __optimize__,   s    t   Requestc           B@ s   e  Z d  Z e Z d9 Z d9 Z d: Z e Z	 d9 Z
 d9 Z d9 Z e sK d; Z n  e d9 d9 d9 d9 d9 d9 e d9 d9 e e e e d  Z e d    Z d   Z d9 d9 d  Z d   Z d9 d  Z d   Z d    Z d!   Z d"   Z d#   Z d$   Z d%   Z  e e d&  Z! d'   Z" e d(  Z# e d)  Z$ d*   Z% d+   Z& d,   Z' e d-    Z( e d.    Z) e d/    Z* e* j+ d0    Z* e d1    Z, e, j+ d2    Z, e d3    Z- e d4    Z. e/ d5    Z0 e/ d6    Z1 e/ d7    Z2 e/ d8    Z3 RS(<   u   A request for task execution.u   appu   typeu   nameu   idu   root_idu	   parent_idu   on_acku   bodyu   hostnameu   eventeru   connection_errorsu   tasku   etau   expiresu   request_dictu	   on_rejectu   utcu   content_typeu   content_encodingu   argsrepru
   kwargsrepru   _decodedu   __weakref__u   __dict__c         K@ s=  | d  k r | j } n  |
 d  k r0 | j }
 n  | |  _ | |  _ |
 |  _ | |  _ | |  _ | rv d  |  _ |  _ n | j | j |  _ |  _ | d |  _	 | d } |  _
 |  _ | j d  |  _ | j d  |  _ d | k r | d p |  j |  _ n  d | k r| d |  _ n  | j d d  |  _ | j d	 d  |  _ | |  _ |	 |  _ | pat   |  _ | |  _ | pyd |  _ | p|  j j | |  _ | j d
  } | d  k	 ry | |  } Wn4 t t t f k
 r} t d j | |    n X| | |  j  |  _  n	 d  |  _  | j d  } | d  k	 ry | |  } Wn4 t t t f k
 r|} t d j | |    n X| | |  j  |  _! n	 d  |  _! | j" pi  } | j# pi  } | j$ i | j d  d 6| j d  d 6i | j d  d 6| j d  d 6| j d  d 6| j d  d 6d 6 | |  _% d  S(   Nu   idu   tasku   root_idu	   parent_idu   shadowu	   timelimitu   argsrepru    u
   kwargsrepru   etau   invalid ETA value {0!r}: {1}u   expiresu    invalid expires value {0!r}: {1}u   reply_tou   correlation_idu   exchangeu   routing_keyu   priorityu   redeliveredu   delivery_info(    (&   t   Nonet   headerst   bodyt   appt   messaget   utct   _decodedt   content_typet   content_encodingt   idt   typet   namet   gett   root_idt	   parent_idt   time_limitst   argsreprt
   kwargsreprt   on_ackt	   on_rejectR   t   hostnamet   eventert   connection_errorst   taskst   taskt   AttributeErrort
   ValueErrort	   TypeErrorR   t   formatt   tzlocalt   etat   expirest   delivery_infot
   propertiest   updatet   request_dict(   t   selfR,   R:   R<   R=   R+   R>   RK   R@   R;   R*   R)   t   decodedR-   R   R   t   optsR2   RF   t   excRG   RH   RI   (    (    s7   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/request.pyt   __init__S   sr    											c         C@ s   |  j  d S(   Nu   delivery_info(   RK   (   RL   (    (    s7   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/request.pyRH      s    c         K@ s   |  j  } |  j } |  j   r- t |   n  |  j \ } } | j t d |  j | |  j |  j	 |  j
 |  j f d |  j d |  j d |  j d |  j d | p | j d | p | j d | } t t |  |  _ | S(	   u  Used by the worker to send this task to the pool.

        Arguments:
            pool (~celery.concurrency.base.TaskPool): The execution pool
                used to execute this request.

        Raises:
            celery.exceptions.TaskRevokedError: if the task was revoked.
        t   argst   accept_callbackt   timeout_callbackt   callbackt   error_callbackt   soft_timeoutt   timeoutt   correlation_id(   R1   R@   t   revokedR   R7   t   apply_asyncR
   R2   RK   R*   R/   R0   t   on_acceptedt
   on_timeoutt
   on_successt
   on_failuret   soft_time_limitt
   time_limitR   R   t   _apply_result(   RL   t   poolt   kwargst   task_idR@   R`   R_   t   result(    (    s7   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/request.pyt   execute_using_pool   s$    
							c         C@ s   |  j    r d S|  j j s) |  j   n  |  j } |  j \ } } } | j i | d 6| d 6|  j d 6t d 6| d 6| d 6| p i   t	 |  j |  j
 | | | d |  j d	 |  j j d
 |  j d } |  j   | S(   u   Execute the task in a :func:`~celery.app.trace.trace_task`.

        Arguments:
            loglevel (int): The loglevel used by the task.
            logfile (str): The logfile used by the task.
        Nu   loglevelu   logfileu   hostnameu   is_eageru   argsu   kwargsR<   t   loaderR+   i    (   RY   R@   t	   acks_latet   acknowledgeRK   t   _payloadRJ   R<   t   FalseR	   R1   R+   Rg   (   RL   t   loglevelt   logfilet   requestRQ   Rc   t   embedt   retval(    (    s7   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/request.pyt   execute   s    	
c         C@ sH   |  j  rD t j |  j  j  } | |  j  k rD t j |  j  t Sn  d S(   u%   If expired, mark the task as revoked.N(   RG   R   t   nowt   tzinfot   revoked_taskst   addR1   t   True(   RL   Rr   (    (    s7   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/request.pyt   maybe_expire   s
    	c         C@ s   t  j | p t  } |  j rJ | j |  j |  |  j d t | t  n | | f |  _	 |  j
 d  k	 r |  j
   } | d  k	 r | j |  q n  d  S(   Nu
   terminated(   t   _signalst   signumR   t
   time_startt   terminate_jobt
   worker_pidt   _announce_revokedRv   Rk   t   _terminate_on_ackRa   R(   t	   terminate(   RL   Rb   t   signalt   obj(    (    s7   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/request.pyR      s    	c      
   C@ s   t  |   |  j d d | d | d | |  j j j |  j | d |  d |  j |  j   t |  _	 t
 |  j d |  d | d | d | d  S(   Nu   task-revokedt
   terminatedRy   t   expiredRn   t   store_result(   t
   task_readyt
   send_eventR@   t   backendt   mark_as_revokedR1   t   store_errorsRi   Rv   t   _already_revokedt   send_revoked(   RL   t   reasonR   Ry   R   (    (    s7   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/request.pyR}      s    

	c         C@ sz   t  } |  j r t S|  j r+ |  j   } n  |  j t k rv t d |  j |  j  |  j	 | rb d n d t  d |  t St  S(   u%   If revoked, skip task and mark state.u   Discarding revoked task: %s[%s]u   expiredu   revokedN(   Rk   R   Rv   RG   Rw   R1   Rt   t   infoR3   R}   R(   (   RL   R   (    (    s7   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/request.pyRY      s    		c         K@ sD   |  j  r@ |  j  j r@ |  j j r@ |  j  j | d |  j | n  d  S(   Nt   uuid(   R=   t   enabledR@   t   send_eventst   sendR1   (   RL   R2   t   fields(    (    s7   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/request.pyR     s    !c         C@ s   | |  _  | |  _ t |   |  j j s5 |  j   n  |  j d  t rd t d |  j	 |  j
 |  n  |  j d k	 r |  j |  j   n  d S(   u4   Handler called when task is accepted by worker pool.u   task-startedu   Task accepted: %s[%s] pid:%rN(   R|   Rz   t   task_acceptedR@   Rh   Ri   R   R#   t   debugR3   R1   R~   R(   R   (   RL   t   pidt   time_accepted(    (    s7   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/request.pyR[     s    		
c         C@ s   t  |   | r8 t d | |  j |  j  t |  } n% t d | |  j |  j  t |  } |  j j j	 |  j | d |  d |  j
 |  j j r |  j   n  d S(   u%   Handler called if the task times out.u)   Soft time limit (%ss) exceeded for %s[%s]u)   Hard time limit (%ss) exceeded for %s[%s]Rn   R   N(   R   t   warnR3   R1   R   t   errorR   R@   R   t   mark_as_failureR   Rh   Ri   (   RL   t   softRW   RO   (    (    s7   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/request.pyR\     s    
c         K@ s   | \ } } } | rL t  | j t t f  r9 | j  n  |  j | d t St |   |  j j ro |  j	   n  |  j
 d d | d | d S(   u6   Handler called if the task was successfully processed.t	   return_oku   task-succeededRe   t   runtimeN(   t
   isinstancet	   exceptiont
   SystemExitt   KeyboardInterruptR^   Rv   R   R@   Rh   Ri   R   (   RL   t   failed__retval__runtimeRc   t   failedRp   R   (    (    s7   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/request.pyR]   2  s    
c         C@ sK   |  j  j r |  j   n  |  j d d t | j j  d t | j  d S(   u-   Handler called if the task should be retried.u   task-retriedR   t	   tracebackN(	   R@   Rh   Ri   R   R   R   RO   R   R   (   RL   t   exc_info(    (    s7   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/request.pyt   on_retry@  s
    c         C@ s  t  |   t | j t  r5 t d | j f   nD t | j t  r] |  j d | j j  St | j t  ry |  j   S| j } t | t	  r |  j
 |  St | t  r |  j d t t |  t  t } nA t | t  s | r|  j j j |  j | d |  d |  j n  |  j j r|  j j d d  t k } |  j j oRt | t  } | rt|  j d |  t } q|  j   n  | r|  j d d t t | j   d	 | j n  | st d
 | d | j  n  d S(   u/   Handler called if the task raised an exception.u   Process got: %st   requeueu
   terminatedRn   R   u   redeliveredu   task-failedR   R   u   Task handler raised error: %rR   N(!   R   R   R   t   MemoryErrorR   t   rejectR   R   Ri   R   R   R   R}   Rv   R   Rk   R   R@   R   R   R1   R   Rh   RH   R4   R(   t   reject_on_worker_lostR   R   R   R   R   R   (   RL   R   t   send_failed_eventR   RO   R   R   (    (    s7   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/request.pyR^   I  sB    

			c         C@ s,   |  j  s( |  j t |  j  t |  _  n  d S(   u   Acknowledge task.N(   t   acknowledgedR:   R   R>   Rv   (   RL   (    (    s7   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/request.pyRi   z  s    	c         C@ sB   |  j  s> |  j t |  j |  t |  _  |  j d d | n  d  S(   Nu   task-rejectedR   (   R   R;   R   R>   Rv   R   (   RL   R   (    (    s7   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/request.pyR     s    		c         C@ sr   i |  j  d 6|  j d 6|  j d 6|  j d 6|  j d 6|  j d 6|  j d 6|  j d 6|  j d	 6|  j	 d
 6|  j
 d 6S(   Nu   idu   nameu   argsu   kwargsu   typeu   bodyu   hostnameu
   time_startu   acknowledgedu   delivery_infou
   worker_pid(   R1   R3   R8   R9   R2   R*   R<   Rz   R   RH   R|   (   RL   t   safe(    (    s7   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/request.pyR     s    









c         C@ sR   d j  |  j   |  j r* d j |  j  n d |  j rH d j |  j  n d g  S(   Nu    u
    ETA:[{0}]u    u    expires:[{0}](   t   joint	   humaninfoRF   RD   RG   (   RL   (    (    s7   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/request.pyt   __str__  s    	c         C@ s   d j  |   S(   Nu   {0.name}[{0.id}](   RD   (   RL   (    (    s7   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/request.pyR     s    c         C@ s+   d j  t |   j |  j   |  j |  j  S(   Nu   <{0}: {1} {2} {3}>(   RD   R2   t   __name__R   R8   R9   (   RL   (    (    s7   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/request.pyt   __repr__  s    c         C@ s+   |  j  d  k r$ |  j j j |  _  n  |  j  S(   N(   t   _tzlocalR(   R+   t   confR   (   RL   (    (    s7   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/request.pyRE     s    c         C@ s   |  j  j p |  j  j S(   N(   R@   t   ignore_resultt   store_errors_even_if_ignored(   RL   (    (    s7   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/request.pyR     s    c         C@ s   |  j  S(   N(   R1   (   RL   (    (    s7   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/request.pyRd     s    c         C@ s   | |  _  d  S(   N(   R1   (   RL   t   value(    (    s7   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/request.pyRd     s    c         C@ s   |  j  S(   N(   R3   (   RL   (    (    s7   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/request.pyt	   task_name  s    c         C@ s   | |  _  d  S(   N(   R3   (   RL   R   (    (    s7   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/request.pyR     s    c         C@ s   |  j  d S(   Nu   reply_to(   RK   (   RL   (    (    s7   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/request.pyt   reply_to  s    c         C@ s   |  j  d S(   Nu   correlation_id(   RK   (   RL   (    (    s7   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/request.pyRX     s    c         C@ s   |  j  r |  j S|  j j S(   N(   R.   R*   R,   t   payload(   RL   (    (    s7   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/request.pyRj     s    c         C@ s   |  j  \ } } } | j d  S(   Nu   chord(   Rj   R4   (   RL   t   _Ro   (    (    s7   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/request.pyt   chord  s    c         C@ s   |  j  \ } } } | j d  S(   Nu   errbacks(   Rj   R4   (   RL   R   Ro   (    (    s7   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/request.pyt   errbacks  s    c         C@ s   |  j  d S(   Nu   group(   RK   (   RL   (    (    s7   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/request.pyt   group  s    N(   NN(   u   appu   typeu   nameu   idu   root_idu	   parent_idu   on_acku   bodyu   hostnameu   eventeru   connection_errorsu   tasku   etau   expiresu   request_dictu	   on_rejectu   utcu   content_typeu   content_encodingu   argsrepru
   kwargsrepru   _decodedu   __weakref__u   __dict__(4   R   t
   __module__t   __doc__Rk   R   R(   Rz   R|   R7   R   R~   Ra   R   t   IS_PYPYt	   __slots__R   Rv   R   R   RP   t   propertyRH   Rf   Rq   Rw   R   R}   RY   R   R[   R\   R]   R   R^   Ri   R   R   R   R   R   RE   R   Rd   t   setterR   R   RX   R   Rj   R   R   R   (    (    (    s7   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/request.pyR'   =   sl        				I	 									1				c   	      @ s{   d d l  m  | j  | j  | j  | j   | o@ | j  d |  f           f	 d     Y} | S(   Ni    (   R
   R'   c           @ s;   e  Z       f d    Z     f d   Z RS(   c         @ s   |  j  } |  j s |  k r9 |  j   r9 t |   n  |  j \ } }    d |  j | |  j |  j |  j |  j	 f d |  j
 d |  j d |  j d |  j d | p  d | p  d | } t  |  |  _ | S(	   NRQ   RR   RS   RT   RU   RV   RW   RX   (   R1   RG   RY   R   R7   R2   RK   R*   R/   R0   R[   R\   R]   R^   R   Ra   (   RL   Rb   Rc   Rd   R`   R_   Re   (   RZ   t   default_soft_time_limitt   default_time_limitR   Rt   t   trace(    s7   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/request.pyRf     s"    	!					c         @ s   | \ } } } | rL t  | j t t f  r9 | j  n  |  j | d t S |     ri |  j   n   r |  j d d | d | n  d  S(   NR   u   task-succeededRe   R   (   R   R   R   R   R^   Rv   Ri   R   (   RL   R   Rc   R   Rp   R   (   Rh   t   eventsR   (    s7   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/request.pyR]     s    	
(   R   R   Rf   R]   (    (	   Rh   RZ   R   R   R   R   Rt   R   R   (    s7   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/request.pyR'     s   (   t   celery.app.traceR
   R`   R_   RZ   Rh   R   (	   t   baseR@   Rb   R<   R=   R   Rt   R   R'   (    (	   Rh   RZ   R   R   R   R   Rt   R   R   s7   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/request.pyt   create_request_cls  s    				4)(L   R   t
   __future__R    R   R!   t   sysR   t   weakrefR   t   billiard.commonR   t   kombu.utils.encodingR   R   t   kombu.utils.objectsR   t   celeryR   R   R	   R
   t   celery.exceptionsR   R   R   R   R   R   R   R   R   t   celery.fiveR   R   t   celery.platformsRx   t   celery.utils.functionalR   R   t   celery.utils.logR   t   celery.utils.nodenamesR   t   celery.utils.timeR   R   R   t   celery.utils.serializationR   t    R   t   __all__t   hasattrR   R   R   R   R   t   warningR   R   Rk   R%   R#   R&   t   tz_or_localt   task_revokedR   R   R   R   RY   Rt   t   objectR'   R   (    (    (    s7   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/request.pyt   <module>   sL   @						 