
P'^c           @@ s  d  Z  d d l m Z 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 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 m Z m Z m Z m Z m Z m Z m Z m  Z  d d l! m" Z" m# Z# m$ Z$ m% 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/ d d l0 m1 Z1 d d l2 m3 Z3 m4 Z4 m5 Z5 d d l6 m7 Z7 d g Z8 e9 e d  Z: e- e;  Z< e< j= e< j> e< j? e< j@ f \ Z= Z> ZA Z@ eB aC eB aD d ZE d   ZF eF   e4 jG ZH e4 jI ZI e jJ jK ZL e7 jM ZM e7 jN ZN e7 jO ZP e jQ d% k ZR i d d 6d d 6d d 6i d d 6d d 6d d  6eB d! 6d" 6ZT d# eU f d$     YZV d S(&   u   
    celery.worker.job
    ~~~~~~~~~~~~~~~~~

    This module defines the :class:`Request` class,
    which specifies how tasks are executed.

i    (   t   absolute_importt   unicode_literalsN(   t   ExceptionInfo(   t   datetime(   t   ref(   t   kwdictt   reprcall(   t	   safe_reprt   safe_str(   t   signals(   t
   trace_taskt   trace_task_ret(	   t   Ignoret   TaskRevokedErrort   InvalidTaskErrort   SoftTimeLimitExceededt   TimeLimitExceededt   WorkerLostErrort
   Terminatedt   Retryt   Reject(   t   itemst	   monotonict   stringt   string_t(   t   fun_takes_kwargs(   t   noop(   t
   get_logger(   t   get_pickled_exception(   t   truncate(   t   maybe_iso8601t   timezonet   maybe_make_awarei   (   t   stateu   Requestu   pypy_version_infoi   c           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(    (    (    s3   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/job.pyt   __optimize__7   s    i   i   u   headersu   reply_tou   correlation_idu   exchangeu   routing_keyu   priorityu   redeliveredu   delivery_infot   Requestc           B@ s  e  Z d  Z e s d< Z n  d Z d Z d Z d Z d Z	 d Z
 e d= d= d= d= d= d= d= e d 	 Z e d    Z d    Z d!   Z d= d= d"  Z d#   Z d= d$  Z d%   Z d&   Z d'   Z d(   Z d)   Z d= e d*  Z d+   Z d,   Z e d-  Z d.   Z  e! d/  Z" e# d0  Z$ e! d1  Z% d2   Z& e& Z' d3   Z( e d4    Z) e d5    Z* e d6    Z+ e+ j, d7    Z+ e d8    Z- e- j, d9    Z- e d:    Z. e d;    Z/ RS(>   u   A request for task execution.u   appu   nameu   idu   argsu   kwargsu   on_acku   hostnameu   eventeru   connection_errorsu   tasku   etau   expiresu   request_dictu   acknowledgedu	   on_rejectu   utcu
   time_startu
   worker_pidu   _already_revokedu   _terminate_on_acku   _apply_resultu   _tzlocalu   __weakref__u   __dict__uN           Task %(name)s[%(id)s] succeeded in %(runtime)ss: %(return_value)s
    u;           Task %(name)s[%(id)s] %(description)s: %(exc)s
    u2           Task %(name)s[%(id)s] %(description)s
    u*           Task %(name)s[%(id)s] %(exc)s
    u$   Task %(name)s[%(id)s] retry: %(exc)sc         K@ s3  | |  _  | d } |  _ | d |  _ | j d g   |  _ | j d i   |  _ y |  j j Wn t k
 r~ t d   n Xt	 r t
 |  j  |  _ n  | j d  } | j d  } | j d t  } |  _ | |  _ |
 |  _ | p t j   |  _ | |  _ | p
d |  _ |	 p#|  j  j | |  _ t |  _ |  _ d  |  _ |  _ |  _ d  |  _ d  |  _ | d  k	 ry t |  |  _ Wn4 t t  t! f k
 r} t d	 j" | |    n X| rt# |  j |  j$  |  _ qn	 d  |  _ | d  k	 r_y t |  |  _% Wn4 t t  t! f k
 r:} t d
 j" | |    n X| rht# |  j% |  j$  |  _% qhn	 d  |  _% | r| j& pzi  } | j' pi  } | j( i | j) d 6| j d  d 6| j d  d 6i | j d  d 6| j d  d 6| j d | j d   d 6| j d  d 6d 6 n | j( t*  | |  _+ d  S(   Nu   tasku   idu   argsu   kwargsu'   Task keyword arguments is not a mappingu   etau   expiresu   utcu   invalid eta value {0!r}: {1}u    invalid expires value {0!r}: {1}u   headersu   reply_tou   correlation_idu   exchangeu   routing_keyu   priorityu   redeliveredu   delivery_info(    (,   t   appt   namet   idt   gett   argst   kwargsR   t   AttributeErrorR   t   NEEDS_KWDICTR   t   Falset   utct   on_ackt	   on_rejectt   sockett   gethostnamet   hostnamet   eventert   connection_errorst   taskst   taskt   acknowledgedt   _already_revokedt   Nonet
   time_startt
   worker_pidt   _terminate_on_ackt   _apply_resultt   _tzlocalR   t   etat
   ValueErrort	   TypeErrort   formatR    t   tzlocalt   expirest   delivery_infot
   propertiest   updatet   headerst   DEFAULT_FIELDSt   request_dict(   t   selft   bodyR5   R9   R:   R+   R;   RQ   t   messageR=   R6   t   optsR,   RF   RK   R4   t   excRL   RM   (    (    s3   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/job.pyt   __init__~   st    									
c         C@ s   |  j  d S(   Nu   delivery_info(   RQ   (   RR   (    (    s3   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/job.pyRL      s    c         @ s   t  |  j  } i d d 6d d 6|  j d 6|  j d 6|  j j d d  d 6t d 6|  j d	 6} |  j	 j
 } t | |    t    f d
   t |  D  } | j |  | S(   ud  Extend the tasks keyword arguments with standard task arguments.

        Currently these are `logfile`, `loglevel`, `task_id`,
        `task_name`, `task_retries`, and `delivery_info`.

        See :meth:`celery.task.base.Task.run` for more information.

        Magic keyword arguments are deprecated and will be removed
        in version 4.0.

        u   logfileu   loglevelu   task_idu	   task_nameu   retriesi    u   task_retriesu   task_is_eageru   delivery_infoc         3@ s-   |  ]# \ } } |   k r | | f Vq d  S(   N(    (   t   .0t   keyt   val(   t   supported_keys(    s3   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/job.pys	   <genexpr>   s    	N(   t   dictR0   R@   R-   R,   RQ   R.   R3   RL   R=   t   runR   R   RN   (   RR   R0   t   default_kwargst   funt   extend_with(    (   R[   s3   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/job.pyt   extend_with_default_kwargs   s    


"c   
      K@ sS  |  j  } |  j } |  j   r- t |   n  |  j } |  j } | j rW |  j   } n  |  j } | j	 i | d 6t
 d 6|  j d 6|  j j d  d 6 | j d d  \ } } | p | j } | p | j } | j t d |  j | |  j | | f d |  j d	 |  j d
 |  j d |  j d | d | d | }	 |	 d k	 rFt |	  n |	 |  _ |	 S(   u   Used by the worker to send this task to the pool.

        :param pool: A :class:`celery.concurrency.base.TaskPool` instance.

        :raises celery.exceptions.TaskRevokedError: if the task was revoked
            and ignored.

        u   hostnameu   is_eageru   delivery_infou   tasksetu   groupu	   timelimitR/   t   accept_callbackt   timeout_callbackt   callbackt   error_callbackt   soft_timeoutt   timeoutt   correlation_idN(   NN(   R-   R=   t   revokedR   R9   R0   t   accept_magic_kwargsRa   RQ   RN   R3   RL   R.   R@   t
   time_limitt   soft_time_limitt   apply_asyncR   R,   R/   t   on_acceptedt
   on_timeoutt
   on_successt
   on_failureR   RD   (
   RR   t   poolR0   t   uuidR=   R9   t   requestRg   Rf   t   result(    (    s3   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/job.pyt   execute_using_pool   s6    							
					!c         C@ s   |  j    r d S|  j j s) |  j   n  |  j } |  j j rM |  j   } n  |  j } | j i | d 6| d 6|  j	 d 6t
 d 6|  j d 6 t |  j |  j |  j | | d |  j	 d |  j j d	 |  j } |  j   | S(
   u   Execute the task in a :func:`~celery.app.trace.trace_task`.

        :keyword loglevel: The loglevel used by the task.
        :keyword logfile: The logfile used by the task.

        Nu   loglevelu   logfileu   hostnameu   is_eageru   delivery_infoR9   t   loaderR+   (   Ri   R=   t	   acks_latet   acknowledgeR0   Rj   Ra   RQ   RN   R9   R3   RL   R
   R-   R/   R+   Rw   (   RR   t   loglevelt   logfileR0   Rt   t   retval(    (    s3   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/job.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(   RK   R   t   nowt   tzinfot   revoked_taskst   addR-   t   True(   RR   R~   (    (    s3   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/job.pyt   maybe_expire)  s
    	c         C@ s   t  j | p d  } |  j rJ | j |  j |  |  j d t | t  n | | f |  _ |  j	 d  k	 r |  j	   } | d  k	 r | j |  q n  d  S(   Nu   TERMu
   terminated(   t   _signalst   signumRA   t   terminate_jobRB   t   _announce_revokedR   R3   RC   RD   R@   t	   terminate(   RR   Rr   t   signalt   obj(    (    s3   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/job.pyR   1  s    	c      
   C@ s   t  |   |  j d d | d | d | |  j rT |  j j j |  j | d |  n  |  j   t |  _	 t
 |  j d |  d | d | d | d  S(   Nu   task-revokedt
   terminatedR   t   expiredRt   (   t
   task_readyt
   send_eventt   store_errorsR=   t   backendt   mark_as_revokedR-   Ry   R   R?   t   send_revoked(   RR   t   reasonR   R   R   (    (    s3   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/job.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(   R3   R?   R   RK   R   R-   R   t   infoR,   R   R@   (   RR   R   (    (    s3   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/job.pyRi   H  s    		c         K@ sD   |  j  r@ |  j  j r@ |  j j r@ |  j  j | d |  j | n  d  S(   NRs   (   R:   t   enabledR=   t   send_eventst   sendR-   (   RR   t   typet   fields(    (    s3   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/job.pyR   W  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(   RB   RA   t   task_acceptedR=   Rx   Ry   R   R&   t   debugR,   R-   RC   R@   R   (   RR   t   pidt   time_accepted(    (    s3   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/job.pyRn   [  s    		
c         C@ s   t  |   | r8 t d | |  j |  j  t |  } n% t d | |  j |  j  t |  } |  j r |  j j	 j
 |  j | d |  n  |  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]Rt   N(   R   t   warnR,   R-   R   t   errorR   R   R=   R   t   mark_as_failureRx   Ry   (   RR   t   softRg   RV   (    (    s3   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/job.pyRo   h  s    
	"c         C@ s8  t  | t  r@ t  | j t t f  r3 | j  n  |  j |  St |   |  j j rc |  j	   n  |  j
 r |  j
 j r |   } |  j r | |  j p d } |  j d d t |  d | n  t r4| p |   } |  j r | |  j p d } t |  j j   i |  j d 6|  j d 6|  j |  d 6| d 6 n  d	 S(
   u6   Handler called if the task was successfully processed.i    u   task-succeededRu   t   runtimeu   idu   nameu   return_valueu   runtimeN(   t
   isinstanceR   t	   exceptiont
   SystemExitt   KeyboardInterruptRq   R   R=   Rx   Ry   R:   R   RA   R   R   R(   R   t   success_msgt   stripR-   R,   t   repr_result(   RR   t	   ret_valueR~   t   nowfunR   (    (    s3   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/job.pyRp   z  s(    	
	c         C@ s   |  j  j r |  j   n  |  j d d t | j j  d t | j  t	 r t
 |  j j   i |  j d 6|  j d 6| j d 6 n  d S(   u-   Handler called if the task should be retried.u   task-retriedR   t	   tracebacku   idu   nameu   excN(   R=   Rx   Ry   R   R   R   RV   R   R   R(   R   t	   retry_msgR   R-   R,   (   RR   t   exc_info(    (    s3   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/job.pyt   on_retry  s    c         C@ s   t  |   t } | j s | j } t | t  r> |  j |  S|  j r t | t  rx |  j	 j
 j |  j | d |  q t | t  r |  j d t t |  t  t } q n  |  j	 j r |  j   q n  |  j | d | d S(   u/   Handler called if the task raised an exception.Rt   u
   terminatedt   send_failed_eventN(   R   R   t   internalR   R   R   R   R   R   R=   R   R   R-   R   R   R   R3   Rx   Ry   t
   _log_error(   RR   R   R   RV   (    (    s3   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/job.pyRq     s"    
			c         C@ s  t  | j  | _ | j } t |  t | j  | j | j t |  j  t |  j  f \ } } } } } }	 |  j	 }
 |
 j
 r t | |
 j
  r t t j d  d f \ } } } } n t t j d } } } |  j } | rt | j t  rt d | j f   qt | j t  rN|  j } d } t j } t } |  j d | j j  qt | j t  r|  j } d } t j } d  } t } |  j   q|  j } d } t j } n  | r|  j d d	 | d
 | n  i |  j d 6|  j  d 6|  j! d 6| d 6| d 6| d 6|	 d 6| d 6} t" j# | | j$   | d | d i i |  j  d 6|  j! d 6| d 6|	 d 6|  j d 6| d 6d 6| r|
 j% | | j  n  d  S(   Nu   raised expectedu   raised unexpectedu   Process got: %su   rejectedt   requeueu   ignoredu   INTERNAL ERRORu   task-failedR   R   u   hostnameu   idu   nameu   excu	   tracebacku   argsu   kwargsu   descriptionR   t   extrau   internalu   data(&   R   R   R   R   R   R   R   R/   R0   R=   t   throwsR   R3   R$   R'   R@   R   t   ERRORt	   error_msgt   MemoryErrorR   t   rejected_msgt   WARNt   rejectR   R   t   ignored_msgRy   t   internal_error_msgt   CRITICALR   R9   R-   R,   R"   t   logR   t   send_error_email(   RR   t   einfoR   t   eobjR   R   R   R   t   sargst   skwargsR=   t   do_send_mailt   severityt   descriptionRI   t   context(    (    s3   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/job.pyR     sl    		$	$						





c         C@ s,   |  j  s( |  j t |  j  t |  _  n  d S(   u   Acknowledge task.N(   R>   R5   R"   R;   R   (   RR   (    (    s3   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/job.pyRy     s    	c         C@ s/   |  j  s+ |  j t |  j |  t |  _  n  d  S(   N(   R>   R6   R"   R;   R   (   RR   R   (    (    s3   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/job.pyR   
  s    	c         C@ s>   t  | t  s t |  } n  t |  | k r: t |  S| S(   N(   R   R   R   t   lenR   (   RR   Ru   t   maxlen(    (    s3   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/job.pyR     s    c         C@ s   i	 |  j  d 6|  j d 6| r& |  j n t |  j  d 6| rE |  j n t |  j  d 6|  j d 6|  j d 6|  j d 6|  j d 6|  j	 d	 6S(
   Nu   idu   nameu   argsu   kwargsu   hostnameu
   time_startu   acknowledgedu   delivery_infou
   worker_pid(
   R-   R,   R/   R   R0   R9   RA   R>   RL   RB   (   RR   t   safe(    (    s3   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/job.pyR     s    




c         C@ sI   d j  |  |  j r$ d j  |  j  n d |  j rB d j  |  j  n d  S(   Nu   {0.name}[{0.id}]{1}{2}u
    eta:[{0}]u    u    expires:[{0}](   RI   RF   RK   (   RR   (    (    s3   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/job.pyt   __str__!  s    c         C@ s4   d j  t |   j |  j t |  j |  j |  j   S(   Nu   <{0} {1}: {2}>(   RI   R   t   __name__R-   R   R,   R/   R0   (   RR   (    (    s3   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/job.pyt   __repr__)  s    c         C@ s+   |  j  d  k r$ |  j j j |  _  n  |  j  S(   N(   RE   R@   R+   t   conft   CELERY_TIMEZONE(   RR   (    (    s3   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/job.pyRJ   .  s    c         C@ s   |  j  j p |  j  j S(   N(   R=   t   ignore_resultt   store_errors_even_if_ignored(   RR   (    (    s3   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/job.pyR   4  s    c         C@ s   |  j  S(   N(   R-   (   RR   (    (    s3   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/job.pyt   task_id9  s    c         C@ s   | |  _  d  S(   N(   R-   (   RR   t   value(    (    s3   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/job.pyR   >  s    c         C@ s   |  j  S(   N(   R,   (   RR   (    (    s3   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/job.pyt	   task_nameB  s    c         C@ s   | |  _  d  S(   N(   R,   (   RR   R   (    (    s3   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/job.pyR   G  s    c         C@ s   |  j  d S(   Nu   reply_to(   RQ   (   RR   (    (    s3   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/job.pyt   reply_toK  s    c         C@ s   |  j  d S(   Nu   correlation_id(   RQ   (   RR   (    (    s3   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/job.pyRh   P  s    (   u   appu   nameu   idu   argsu   kwargsu   on_acku   hostnameu   eventeru   connection_errorsu   tasku   etau   expiresu   request_dictu   acknowledgedu	   on_rejectu   utcu
   time_startu
   worker_pidu   _already_revokedu   _terminate_on_acku   _apply_resultu   _tzlocalu   __weakref__u   __dict__N(0   R   t
   __module__t   __doc__t   IS_PYPYt	   __slots__R   R   R   R   R   R   R   R@   RW   t   propertyRL   Ra   Rv   R}   R   R   R   Ri   R   Rn   Ro   R   Rp   R   Rq   R   R   Ry   R3   R   t   RESULT_MAXLENR   R   R   t	   shortinfoR   RJ   R   R   t   setterR   R   Rh   (    (    (    s3   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/job.pyR*   X   s`        		F		(								G			(   i   i   (W   R   t
   __future__R    R   R$   R7   t   syst   billiard.einfoR   R   t   weakrefR   t   kombu.utilsR   R   t   kombu.utils.encodingR   R   t   celeryR	   t   celery.app.traceR
   R   t   celery.exceptionsR   R   R   R   R   R   R   R   R   t   celery.fiveR   R   R   R   t   celery.platformsR   t   celery.utilsR   t   celery.utils.functionalR   t   celery.utils.logR   t   celery.utils.serializationR   t   celery.utils.textR   t   celery.utils.timeutilsR   R   R    t    R!   t   __all__t   hasattrR   R   R"   R   R   t   warningR   R   R3   R(   R&   R   R)   R4   t   tz_utct   tz_or_localt   task_revokedR   R   R   R   Ri   R   t   version_infoR2   R@   RP   t   objectR*   (    (    (    s3   /tmp/pip-unpacked-wheel-gV1wwp/celery/worker/job.pyt   <module>	   s`   @"							