
O'^c           @@  s=  d  Z  d d l m 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 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 g Z e e  Z e j e j e j  e j! f \ Z Z Z  Z! d Z" d Z# d e$ f d     YZ% d S(   s3   
kombu.mixins
============

Useful mixin classes.

i    (   t   absolute_importN(   t   contextmanager(   t   partial(   t   count(   t   sleepi   (   t   ignore_errors(   t   range(   t   Consumer(   t
   get_logger(   t   cached_propertyt   nested(   t	   safe_repr(   t   TokenBuckett   ConsumerMixins?   Connection to broker lost, trying to re-establish connection...s8   Broker connection error, trying again in %s seconds: %r.c           B@  s   e  Z d  Z d Z e Z d   Z d   Z d   Z	 d   Z
 d   Z d   Z d   Z e d    Z d	 d
  Z e d    Z d d d	 d  Z d   Z d   Z e d    Z e d    Z d   Z e d    Z e d    Z e d    Z RS(   s  Convenience mixin for implementing consumer programs.

    It can be used outside of threads, with threads, or greenthreads
    (eventlet/gevent) too.

    The basic class would need a :attr:`connection` attribute
    which must be a :class:`~kombu.Connection` instance,
    and define a :meth:`get_consumers` method that returns a list
    of :class:`kombu.Consumer` instances to use.
    Supporting multiple consumers is important so that multiple
    channels can be used for different QoS requirements.

    **Example**:

    .. code-block:: python


        class Worker(ConsumerMixin):
            task_queue = Queue('tasks', Exchange('tasks'), 'tasks'))

            def __init__(self, connection):
                self.connection = None

            def get_consumers(self, Consumer, channel):
                return [Consumer(queues=[self.task_queue],
                                 callbacks=[self.on_task])]

            def on_task(self, body, message):
                print('Got task: {0!r}'.format(body))
                message.ack()

    **Additional handler methods**:

        * :meth:`extra_context`

            Optional extra context manager that will be entered
            after the connection and consumers have been set up.

            Takes arguments ``(connection, channel)``.

        * :meth:`on_connection_error`

            Handler called if the connection is lost/ or
            is unavailable.

            Takes arguments ``(exc, interval)``, where interval
            is the time in seconds when the connection will be retried.

            The default handler will log the exception.

        * :meth:`on_connection_revived`

            Handler called as soon as the connection is re-established
            after connection failure.

            Takes no arguments.

        * :meth:`on_consume_ready`

            Handler called when the consumer is ready to accept
            messages.

            Takes arguments ``(connection, channel, consumers)``.
            Also keyword arguments to ``consume`` are forwarded
            to this handler.

        * :meth:`on_consume_end`

            Handler called after the consumers are cancelled.
            Takes arguments ``(connection, channel)``.

        * :meth:`on_iteration`

            Handler called for every iteration while draining
            events.

            Takes no arguments.

        * :meth:`on_decode_error`

            Handler called if a consumer was unable to decode
            the body of a message.

            Takes arguments ``(message, exc)`` where message is the
            original message object.

            The default handler will log the error and
            acknowledge the message, so if you override make
            sure to call super, or perform these steps yourself.

    c         C@  s   t  d   d  S(   Ns   Subclass responsibility(   t   NotImplementedError(   t   selfR   t   channel(    (    s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/mixins.pyt   get_consumers   s    c         C@  s   d  S(   N(    (   R   (    (    s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/mixins.pyt   on_connection_revived   s    c         K@  s   d  S(   N(    (   R   t
   connectionR   t	   consumerst   kwargs(    (    s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/mixins.pyt   on_consume_ready   s    c         C@  s   d  S(   N(    (   R   R   R   (    (    s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/mixins.pyt   on_consume_end   s    c         C@  s   d  S(   N(    (   R   (    (    s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/mixins.pyt   on_iteration   s    c         C@  s3   t  d | | j | j t | j   | j   d  S(   Ns;   Can't decode message body: %r (type:%r encoding:%r raw:%r')(   t   errort   content_typet   content_encodingR   t   bodyt   ack(   R   t   messaget   exc(    (    s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/mixins.pyt   on_decode_error   s    c         C@  s   t  t | | d d d  S(   Nt   exc_infoi   (   t   warnt   W_CONN_ERROR(   R   R   t   interval(    (    s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/mixins.pyt   on_connection_error   s    c         c@  s	   d  Vd  S(   N(    (   R   R   R   (    (    s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/mixins.pyt   extra_context   s    i   c         C@  s   |  j  } |  j j |  j j } xw |  j s yF | j |  r] x0 |  j d d   D] } qP Wn t | j	 |   Wq" | k
 r t
 t d d q" Xq" Wd  S(   Nt   limitR!   i   (   t   restart_limitR   t   connection_errorst   channel_errorst   should_stopt   can_consumet   consumet   NoneR   t   expected_timeR"   t   W_CONN_LOST(   R   t   _tokensR(   t   errorst   _(    (    s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/mixins.pyt   run   s    		c         k@  s_   |  j    M \ } } } |  j | |  ) |  j | | | |  | | | f VWd  QXWd  QXd  S(   N(   R   R&   R   (   R   R   R   R   R   (    (    s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/mixins.pyt   consumer_context   s    c   
   
   k@  s   d } |  j  |    \ } } } x | r6 t |  p< t   D] }	 |  j rP Pn  |  j   y | j d |  Wnb t j k
 r | j   | | 7} | r | | k r   q q= t j	 k
 r |  j s   q q= Xd  Vd } q= WWd  QXt
 d  d  S(   Ni    t   timeouts   consume exiting(   R5   R   R   R+   R   t   drain_eventst   socketR6   t   heartbeat_checkR   t   debug(
   R   R'   R6   t   safety_intervalR   t   elapsedt   connR   R   t   i(    (    s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/mixins.pyR-      s&    "	


		
c         C@  s   t  |  |  S(   s/   Use :func:`kombu.common.ignore_errors` instead.(   R   (   R   t   fun(    (    s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/mixins.pyt   maybe_conn_error   s    c         C@  s   |  j  j   S(   N(   R   t   clone(   R   (    (    s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/mixins.pyt   create_connection   s    c         c@  s4   |  j    " } | j |  j |  j  | VWd  QXd  S(   N(   RB   t   ensure_connectionR%   t   connect_max_retries(   R   R=   (    (    s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/mixins.pyt   establish_connection   s    
c         c@  s   |  j     } |  j   t d | j    | j } t t | d |  j } |  j |  j	 | |     } | | | f VWd  QXt
 d  |  j | |  Wd  QXt
 d  d  S(   Ns   Connected to %sR    s   Consumers cancelleds   Connection closed(   RE   R   t   infot   as_urit   default_channelR   R   R    t   _consume_fromR   R:   R   (   R   R=   R   t   clst   c(    (    s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/mixins.pyR      s    
	
c         G@  s
   t  |   S(   N(   R
   (   R   R   (    (    s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/mixins.pyRI      s    c         C@  s
   t  d  S(   Ni   (   R   (   R   (    (    s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/mixins.pyR(      s    c         C@  s
   |  j  j S(   N(   R   R)   (   R   (    (    s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/mixins.pyR)      s    c         C@  s
   |  j  j S(   N(   R   R*   (   R   (    (    s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/mixins.pyR*      s    N(   t   __name__t
   __module__t   __doc__R.   RD   t   FalseR+   R   R   R   R   R   R    R%   R   R&   R4   R5   R-   R@   RB   RE   R   RI   R	   R(   R)   R*   (    (    (    s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/mixins.pyR   (   s,   [										(&   RN   t
   __future__R    R8   t
   contextlibR   t	   functoolsR   t	   itertoolsR   t   timeR   t   commonR   t   fiveR   t	   messagingR   t   logR   t   utilsR	   R
   t   utils.encodingR   t   utils.limitsR   t   __all__RL   t   loggerR:   RF   R"   R   R0   R#   t   objectR   (    (    (    s.   /tmp/pip-unpacked-wheel-UAnTfW/kombu/mixins.pyt   <module>   s&   	*