ó
Ì	g]c           @   s«  d  Z  d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l m Z e j	 e
 ƒ Z d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d	 „  ƒ  YZ d
 e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e f d „  ƒ  YZ d e j j f d „  ƒ  YZ d e f d „  ƒ  YZ d S(    s,  Implements `AMQPConnectionWorkflow` - the default workflow of performing
multiple TCP/[SSL]/AMQP connection attempts with timeouts and retries until one
succeeds or all attempts fail.

Defines the interface `AbstractAMQPConnectionWorkflow` that facilitates
implementing custom connection workflows.

iÿÿÿÿN(   t   __version__t   AMQPConnectorExceptionc           B   s   e  Z d  Z RS(   s   Base exception for this module(   t   __name__t
   __module__t   __doc__(    (    (    sL   /srv/kernel/kteam-tools/dashboard/pika/adapters/utils/connection_workflow.pyR      s   t   AMQPConnectorStackTimeoutc           B   s   e  Z d  Z RS(   s:   Overall TCP/[SSL]/AMQP stack connection attempt timed out.(   R   R   R   (    (    (    sL   /srv/kernel/kteam-tools/dashboard/pika/adapters/utils/connection_workflow.pyR      s   t   AMQPConnectorAbortedc           B   s   e  Z d  Z RS(   s    Asynchronous request was aborted(   R   R   R   (    (    (    sL   /srv/kernel/kteam-tools/dashboard/pika/adapters/utils/connection_workflow.pyR      s   t   AMQPConnectorWrongStatec           B   s   e  Z d  Z RS(   sj   AMQPConnector operation requested in wrong state, such as aborting after
    completion was reported.
    (   R   R   R   (    (    (    sL   /srv/kernel/kteam-tools/dashboard/pika/adapters/utils/connection_workflow.pyR   "   s   t   AMQPConnectorPhaseErrorBasec           B   s    e  Z d  Z d „  Z d „  Z RS(   sM   Wrapper for exception that occurred during a particular bring-up phase.

    c         G   s#   t  t |  ƒ j | Œ  | |  _ d S(   sÈ   

        :param BaseException exception: error that occurred while waiting for a
            subclass-specific protocol bring-up phase to complete.
        :param args: args for parent class
        N(   t   superR   t   __init__t	   exception(   t   selfR   t   args(    (    sL   /srv/kernel/kteam-tools/dashboard/pika/adapters/utils/connection_workflow.pyR
   -   s    c         C   s   d j  |  j j |  j ƒ S(   Ns   {}: {!r}(   t   formatt	   __class__R   R   (   R   (    (    sL   /srv/kernel/kteam-tools/dashboard/pika/adapters/utils/connection_workflow.pyt   __repr__7   s    (   R   R   R   R
   R   (    (    (    sL   /srv/kernel/kteam-tools/dashboard/pika/adapters/utils/connection_workflow.pyR   (   s   	
t   AMQPConnectorSocketConnectErrorc           B   s   e  Z d  Z RS(   s*   Error connecting TCP socket to remote peer(   R   R   R   (    (    (    sL   /srv/kernel/kteam-tools/dashboard/pika/adapters/utils/connection_workflow.pyR   ;   s   t    AMQPConnectorTransportSetupErrorc           B   s   e  Z d  Z RS(   sO   Error setting up transport after TCP connected but before AMQP handshake.

    (   R   R   R   (    (    (    sL   /srv/kernel/kteam-tools/dashboard/pika/adapters/utils/connection_workflow.pyR   ?   s   t   AMQPConnectorAMQPHandshakeErrorc           B   s   e  Z d  Z RS(   s   Error during AMQP handshake(   R   R   R   (    (    (    sL   /srv/kernel/kteam-tools/dashboard/pika/adapters/utils/connection_workflow.pyR   E   s   t   AMQPConnectionWorkflowAbortedc           B   s   e  Z d  Z RS(   s%   AMQP Connection workflow was aborted.(   R   R   R   (    (    (    sL   /srv/kernel/kteam-tools/dashboard/pika/adapters/utils/connection_workflow.pyR   I   s   t    AMQPConnectionWorkflowWrongStatec           B   s   e  Z d  Z RS(   su   AMQP Connection Workflow operation requested in wrong state, such as
    aborting after completion was reported.
    (   R   R   R   (    (    (    sL   /srv/kernel/kteam-tools/dashboard/pika/adapters/utils/connection_workflow.pyR   M   s   t   AMQPConnectionWorkflowFailedc           B   s    e  Z d  Z d „  Z d „  Z RS(   s5   Indicates that AMQP connection workflow failed.

    c         G   s)   t  t |  ƒ j | Œ  t | ƒ |  _ d S(   s˜   
        :param sequence exceptions: Exceptions that occurred during the
            workflow.
        :param args: args to pass to base class

        N(   R	   R   R
   t   tuplet
   exceptions(   R   R   R   (    (    sL   /srv/kernel/kteam-tools/dashboard/pika/adapters/utils/connection_workflow.pyR
   X   s    c         C   sN   d j  |  j j t |  j ƒ |  j d t |  j ƒ d k rG |  j d n d  ƒ S(   NsG   {}: {} exceptions in all; last exception - {!r}; first exception - {!r}iÿÿÿÿi   i    (   R   R   R   t   lenR   t   None(   R   (    (    sL   /srv/kernel/kteam-tools/dashboard/pika/adapters/utils/connection_workflow.pyR   b   s    
(   R   R   R   R
   R   (    (    (    sL   /srv/kernel/kteam-tools/dashboard/pika/adapters/utils/connection_workflow.pyR   S   s   	
t   AMQPConnectorc           B   sž   e  Z d  Z d Z d Z d Z d Z d Z d Z d Z	 d „  Z
 d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d d „ Z RS(   s;   Performs a single TCP/[SSL]/AMQP connection workflow.

    i    i   i   i   i   i   i   c         C   sj   | |  _  | |  _ d |  _ d |  _ d |  _ d |  _ d |  _ d |  _ d |  _	 d |  _
 |  j |  _ d S(   sª  

        :param callable conn_factory: A function that takes
            `pika.connection.Parameters` as its only arg and returns a brand new
            `pika.connection.Connection`-based adapter instance each time it is
            called. The factory must instantiate the connection with
            `internal_connection_workflow=False`.
        :param pika.adapters.utils.nbio_interface.AbstractIOServices nbio:

        N(   t   _conn_factoryt   _nbioR   t   _addr_recordt   _conn_paramst   _on_donet   _tcp_timeout_reft   _stack_timeout_reft	   _task_reft   _sockt
   _amqp_connt   _STATE_INITt   _state(   R   t   conn_factoryt   nbio(    (    sL   /srv/kernel/kteam-tools/dashboard/pika/adapters/utils/connection_workflow.pyR
   w   s    										c         C   s~  |  j  |  j k r- t d j |  j  ƒ ƒ ‚ n  | |  _ | |  _ | |  _ |  j |  _  t j |  j d  Œ  |  _	 |  j	 j
 t j j t j d ƒ t j j |  j j |  j	 ƒ |  j	 j t ƒ |  j d } t j d t | ƒ |  j j |  j	 | d |  j ƒ|  _ d |  _ |  j j d k	 r;|  j j |  j j |  j ƒ |  _ n  d |  _  |  j j! d k	 rz|  j j |  j j! |  j" ƒ |  _  n  d S(   s—  Asynchronously perform a single TCP/[SSL]/AMQP connection attempt.

        :param tuple addr_record: a single resolved address record compatible
            with `socket.getaddrinfo()` format.
        :param pika.connection.Parameters conn_params:
        :param callable on_done: Function to call upon completion of the
            workflow: `on_done(pika.connection.Connection | BaseException)`. If
            exception, it's going to be one of the following:
                `AMQPConnectorSocketConnectError`
                `AMQPConnectorTransportSetupError`
                `AMQPConnectorAMQPHandshakeError`
                `AMQPConnectorAborted`

        s)   Already in progress or finished; state={}i   i   i   s    Pika version %s connecting to %rt   on_doneN(#   R'   R&   R   R   R   R   R    t
   _STATE_TCPt   socketR$   t
   setsockoptt   pikat   compatt   SOL_TCPt   TCP_NODELAYt   tcp_socket_optst   set_sock_optst   tcp_optionst   setblockingt   Falset   _LOGt   infoR    R   t   connect_sockett   _on_tcp_connection_doneR#   R   R!   t   socket_timeoutt
   call_latert   _on_tcp_connection_timeoutR"   t   stack_timeoutt   _on_overall_timeout(   R   t   addr_recordt   conn_paramsR*   t   addr(    (    sL   /srv/kernel/kteam-tools/dashboard/pika/adapters/utils/connection_workflow.pyt   start“   s2    			
						c         C   s  |  j  |  j k r! t d ƒ ‚ n  |  j  |  j k rB t d ƒ ‚ n  |  j |  _  |  j ƒ  t j d |  j j	 |  j
 ƒ |  j d
 k rµ t j d ƒ |  j j t j |  j t ƒ  ƒ ƒ nf |  j j sä t j d ƒ |  j j d d ƒ n7 t j d ƒ |  j  |  j k st d	 j |  j  ƒ ƒ ‚ d
 S(   sš  Abort the workflow asynchronously. The completion callback will be
        called with an instance of AMQPConnectorAborted.

        NOTE: we can't cancel/close synchronously because aborting pika
        Connection and its transport requires an asynchronous operation.

        :raises AMQPConnectorWrongState: If called after completion has been
            reported or the workflow not started yet.
        s   Cannot abort before starting.s*   Cannot abort after completion was reportedsC   AMQPConnector: beginning client-initiated asynchronous abort; %r/%ssX   AMQPConnector.abort(): no connection, so just scheduling completion report via I/O loop.s*   AMQPConnector.abort(): closing Connection.i@  s3   Client-initiated abort of AMQP Connection Workflow.sC   AMQPConnector.abort(): closing of Connection was already initiated.s9   Connection is closing, but not in TIMEOUT state; state={}N(   R'   R&   R   t   _STATE_DONEt   _STATE_ABORTINGt   _deactivateR7   R8   R   t   hostR   R%   R   t   debugR   t   add_callback_threadsafet	   functoolst   partialt   _report_completion_and_cleanupR   t
   is_closingt   closet   _STATE_TIMEOUTt   AssertionErrorR   (   R   (    (    sL   /srv/kernel/kteam-tools/dashboard/pika/adapters/utils/connection_workflow.pyt   abortÄ   s,    

		c         C   sf   |  j  ƒ  |  j d k	 r2 |  j j ƒ  d |  _ n  d |  _ d |  _ d |  _ d |  _ |  j |  _	 d S(   sq   Cancel asynchronous tasks and clean up to assist garbage collection.

        Transition to STATE_DONE.

        N(
   RF   R$   R   RN   R   R   R   R    RD   R'   (   R   (    (    sL   /srv/kernel/kteam-tools/dashboard/pika/adapters/utils/connection_workflow.pyt   _closeò   s    
				c         C   s£   |  j  d k s' t d j |  j ƒ ƒ ‚ |  j d k	 rO |  j j ƒ  d |  _ n  |  j d k	 rw |  j j ƒ  d |  _ n  |  j d k	 rŸ |  j j ƒ  d |  _ n  d S(   s$   Cancel asynchronous tasks.

        s:   _deactivate called with self._amqp_conn not None; state={}N(	   R%   R   RP   R   R'   R!   t   cancelR"   R#   (   R   (    (    sL   /srv/kernel/kteam-tools/dashboard/pika/adapters/utils/connection_workflow.pyRF     s    c         C   sS   t  | t ƒ r" t j d | ƒ n t j d | ƒ |  j } |  j ƒ  | | ƒ d S(   s´   Clean up and invoke client's `on_done` callback.

        :param pika.connection.Connection | BaseException result: value to pass
            to user's `on_done` callback.
        s%   AMQPConnector - reporting failure: %rs%   AMQPConnector - reporting success: %rN(   t
   isinstancet   BaseExceptionR7   t   errorR8   R    RR   (   R   t   resultR*   (    (    sL   /srv/kernel/kteam-tools/dashboard/pika/adapters/utils/connection_workflow.pyRL     s    	
c         C   sD   d |  _ t t j d j |  j j |  j ƒ ƒ ƒ } |  j	 | ƒ d S(   st   Handle TCP connection timeout

        Reports AMQPConnectorSocketConnectError with socket.timeout inside.

        s)   TCP connection attempt timed out: {!r}/{}N(
   R   R!   R   R,   t   timeoutR   R   RG   R   RL   (   R   RV   (    (    sL   /srv/kernel/kteam-tools/dashboard/pika/adapters/utils/connection_workflow.pyR=   ,  s
    	c         C   sD  d |  _ |  j } |  j |  _ | |  j k r¯ d j |  j j |  j t	 |  j j
 ƒ ƒ } t j | ƒ |  j j s‰ t d j |  j ƒ ƒ ‚ |  j j s« |  j j d | ƒ n  d S| |  j k rè t t d j |  j j |  j ƒ ƒ ƒ } nK | |  j k sý t ‚ t t d j |  j j |  j t	 |  j j
 ƒ ƒ ƒ ƒ } |  j | ƒ d S(   sË  Handle overall TCP/[SSL]/AMQP connection attempt timeout by reporting
        `Timeout` error to the client.

        Reports AMQPConnectorSocketConnectError if timeout occurred during
            socket TCP connection attempt.
        Reports AMQPConnectorTransportSetupError if timeout occurred during
            tramsport [SSL] setup attempt.
        Reports AMQPConnectorAMQPHandshakeError if timeout occurred during
            AMQP handshake.

        s0   Timeout while setting up AMQP to {!r}/{}; ssl={}s   Unexpected open state of {!r}i@  Ns*   Timeout while connecting socket to {!r}/{}s5   Timeout while setting up transport to {!r}/{}; ssl={}(   R   R"   R'   RO   t   _STATE_AMQPR   R   RG   R   t   boolt   ssl_optionsR7   RV   R%   t   is_openRP   RM   RN   R+   R   R   t   _STATE_TRANSPORTR   RL   (   R   t
   prev_statet   msgRV   (    (    sL   /srv/kernel/kteam-tools/dashboard/pika/adapters/utils/connection_workflow.pyR?   9  s2    		c         C   s6  d |  _ |  j d k	 r1 |  j j ƒ  d |  _ n  | d k	 rj t j d | |  j ƒ |  j t | ƒ ƒ d St j	 d |  j
 ƒ |  j |  _ d } } |  j j d k	 rá |  j j j } |  j j j } | d k rá |  j j } qá n  |  j j d t j |  j |  j ƒ d |  j
 d | d | d |  j ƒ |  _ d |  _
 d S(	   s  Handle completion of asynchronous socket connection attempt.

        Reports AMQPConnectorSocketConnectError if TCP socket connection
            failed.

        :param None|BaseException exc: None on success; exception object on
            failure

        s*   TCP Connection attempt failed: %r; dest=%rNs)   TCP connection to broker established: %r.t   protocol_factoryt   sockt   ssl_contextt   server_hostnameR*   (   R   R#   R!   RS   R7   RV   R   RL   R   RH   R$   R]   R'   R   R[   t   contextRc   RG   R   t   create_streaming_connectionRJ   RK   R   t    _on_transport_establishment_done(   R   t   excRb   Rc   (    (    sL   /srv/kernel/kteam-tools/dashboard/pika/adapters/utils/connection_workflow.pyR:   g  s4    
	

	c         C   s¸   d |  _ t | t ƒ r] t j d | |  j j |  j t	 |  j j
 ƒ ƒ |  j t | ƒ ƒ d St j d | ƒ | \ } |  _ |  j |  _ |  j j |  j d t ƒ|  j j |  j ƒ d S(   sQ  Handle asynchronous completion of
        `AbstractIOServices.create_streaming_connection()`

        Reports AMQPConnectorTransportSetupError if transport ([SSL]) setup
            failed.

        :param sequence|BaseException result: On success, a two-tuple
            (transport, protocol); on failure, exception instance.

        sC   Attempt to create the streaming transport failed: %r; %r/%s; ssl=%sNs"   Streaming transport linked up: %r.t   remove_default(   R   R#   RT   RU   R7   RV   R   RG   R   RZ   R[   RL   R   R8   R%   RY   R'   t   add_on_open_error_callbackt   _on_amqp_handshake_donet   Truet   add_on_open_callback(   R   RW   t
   _transport(    (    sL   /srv/kernel/kteam-tools/dashboard/pika/adapters/utils/connection_workflow.pyRf   ”  s    		c         C   s9  t  j d |  j | |  j j |  j ƒ d |  _ |  j |  j k rL t	 ƒ  } nÜ |  j |  j
 k r— t t d j |  j j |  j t |  j j ƒ ƒ ƒ ƒ } n‘ |  j |  j k r| d k rÝ t  j d |  j j |  j | ƒ | } q(t  j d |  j j |  j | ƒ t | ƒ } n t  j d |  j | | ƒ d S|  j | ƒ d S(   sµ  Handle completion of AMQP connection handshake attempt.

        NOTE: we handle two types of callbacks - success with just connection
        arg as well as the open-error callback with connection and error

        Reports AMQPConnectorAMQPHandshakeError if AMQP handshake failed.

        :param pika.connection.Connection connection:
        :param BaseException | None error: None on success, otherwise
            failure

        sJ   AMQPConnector: AMQP handshake attempt completed; state=%s; error=%r; %r/%ss,   Timeout during AMQP handshake{!r}/{}; ssl={}s8   AMQPConnector: AMQP connection established for %r/%s: %rs=   AMQPConnector: AMQP connection handshake failed for %r/%s: %rsg   AMQPConnector: Ignoring AMQP handshake completion notification due to wrong state=%s; error=%r; conn=%rN(   R7   RH   R'   R   RG   R   R   R%   RE   R   RO   R   R   R   RZ   R[   RY   RL   (   R   t
   connectionRV   RW   (    (    sL   /srv/kernel/kteam-tools/dashboard/pika/adapters/utils/connection_workflow.pyRj   ¶  s:    
		N(   R   R   R   R&   R+   R]   RY   RO   RE   RD   R
   RC   RQ   RR   RF   RL   R=   R?   R:   Rf   R   Rj   (    (    (    sL   /srv/kernel/kteam-tools/dashboard/pika/adapters/utils/connection_workflow.pyR   j   s&   		1	.					.	-	"t   AbstractAMQPConnectionWorkflowc           B   s    e  Z d  Z d „  Z d „  Z RS(   sM   Interface for implementing a custom TCP/[SSL]/AMQP connection workflow.

    c         C   s
   t  ‚ d S(   s¦  Asynchronously perform the workflow until success or all retries
        are exhausted. Called by the adapter.

        :param sequence connection_configs: A sequence of one or more
            `pika.connection.Parameters`-based objects. Will attempt to connect
            using each config in the given order.
        :param callable connector_factory: call it without args to obtain a new
            instance of `AMQPConnector` for each connection attempt.
            See `AMQPConnector` for details.
        :param native_loop: Native I/O loop passed by app to the adapter or
            obtained by the adapter by default.
        :param callable on_done: Function to call upon completion of the
            workflow:
            `on_done(pika.connection.Connection |
                     AMQPConnectionWorkflowFailed |
                     AMQPConnectionWorkflowAborted)`.
            `Connection`-based adapter on success,
            `AMQPConnectionWorkflowFailed` on failure,
            `AMQPConnectionWorkflowAborted` if workflow was aborted.

        :raises AMQPConnectionWorkflowWrongState: If called in wrong state, such
            as after starting the workflow.
        N(   t   NotImplementedError(   R   t   connection_configst   connector_factoryt   native_loopR*   (    (    sL   /srv/kernel/kteam-tools/dashboard/pika/adapters/utils/connection_workflow.pyRC   ñ  s    c         C   s
   t  ‚ d S(   s·  Abort the workflow asynchronously. The completion callback will be
        called with an instance of AMQPConnectionWorkflowAborted.

        NOTE: we can't cancel/close synchronously because aborting pika
        Connection and its transport requires an asynchronous operation.

        :raises AMQPConnectionWorkflowWrongState: If called in wrong state, such
            as before starting or after completion has been reported.
        N(   Rp   (   R   (    (    sL   /srv/kernel/kteam-tools/dashboard/pika/adapters/utils/connection_workflow.pyRQ     s    
(   R   R   R   RC   RQ   (    (    (    sL   /srv/kernel/kteam-tools/dashboard/pika/adapters/utils/connection_workflow.pyRo   ì  s   	t   AMQPConnectionWorkflowc           B   s§   e  Z d  Z e j Z e j Z d Z d Z	 d Z
 d Z e d „ Z d „  Z d „  Z d „  Z d	 „  Z d
 „  Z d „  Z d „  Z d „  Z d „  Z d „  Z d „  Z RS(   s  Implements Pika's default workflow for performing multiple TCP/[SSL]/AMQP
    connection attempts with timeouts and retries until one succeeds or all
    attempts fail.

    The workflow:
        while not success and retries remain:
            1. For each given config (pika.connection.Parameters object):
                A. Perform DNS resolution of the config's host.
                B. Attempt to establish TCP/[SSL]/AMQP for each resolved address
                   until one succeeds, in which case we're done.
            2. If all configs failed but retries remain, resume from beginning
               after the given retry pause. NOTE: failure of DNS resolution
               is equivalent to one cycle and will be retried after the pause
               if retries remain.

    i    i   i   i   c         C   s|   d |  _ d |  _ | |  _ d |  _ d |  _ d |  _ d |  _ d |  _ d |  _	 d |  _
 d |  _ g  |  _ |  j |  _ d S(   sã  
        :param int | float retry_pause: Non-negative number of seconds to wait
            before retrying the config sequence. Meaningful only if retries is
            greater than 0. Defaults to 2 seconds.
        :param bool _until_first_amqp_attempt: INTERNAL USE ONLY; ends workflow
            after first AMQP handshake attempt, regardless of outcome (success
            or failure). The automatic connection logic in
            `pika.connection.Connection` enables this because it's not
            designed/tested to reset all state properly to handle more than one
            AMQP handshake attempt.

        TODO: Do we need getaddrinfo timeout?
        TODO: Would it be useful to implement exponential back-off?

        N(   R   t   _attempts_remainingt   _retry_pauset   _until_first_amqp_attemptR   t   _current_config_indext   _connection_configst   _connector_factoryR    t
   _connectorR#   t   _addrinfo_itert   _connection_errorsR&   R'   (   R   Rw   (    (    sL   /srv/kernel/kteam-tools/dashboard/pika/adapters/utils/connection_workflow.pyR
   3  s    												c         C   s   | |  _  d S(   sî  Called by the conneciton adapter only on pika's
        `AMQPConnectionWorkflow` instance to provide it the adapter-specific
        `AbstractIOServices` object before calling the `start()` method.

        NOTE: Custom workflow implementations should use the native I/O loop
        directly because `AbstractIOServices` is private to Pika
        implementation and its interface may change without notice.

        :param pika.adapters.utils.nbio_interface.AbstractIOServices nbio:

        N(   R   (   R   R)   (    (    sL   /srv/kernel/kteam-tools/dashboard/pika/adapters/utils/connection_workflow.pyt   set_io_services]  s    c         C   s  |  j  |  j k r- t d j |  j  ƒ ƒ ‚ n  y t | ƒ Wn( t k
 re } t d j | ƒ ƒ ‚ n X| s„ t d j | ƒ ƒ ‚ n  | |  _ | |  _	 | |  _
 | d j |  _ | d j |  _ |  j |  _  t j d ƒ |  j j d t j |  j d t ƒƒ |  _ d S(	   sf  Override `AbstractAMQPConnectionWorkflow.start()`.

        NOTE: This implementation uses `connection_attempts` and `retry_delay`
        values from the last element of the given `connection_configs` sequence
        as the overall number of connection attempts of the entire
        `connection_configs` sequence and pause between each sequence.

        s)   Already in progress or finished; state={}s3   connection_configs does not support iteration: {!r}s"   connection_configs is empty: {!r}.iÿÿÿÿs1   Starting AMQP Connection workflow asynchronously.i    t   firstN(   R'   R&   R   R   t   itert	   Exceptiont	   TypeErrort
   ValueErrorRy   Rz   R    t   connection_attemptsRu   t   retry_delayRv   t   _STATE_ACTIVER7   RH   R   R<   RJ   RK   t   _start_new_cycle_asyncRk   R#   (   R   Rq   Rr   Rs   R*   RV   (    (    sL   /srv/kernel/kteam-tools/dashboard/pika/adapters/utils/connection_workflow.pyRC   k  s*    				c         C   sÄ   |  j  |  j k r! t d ƒ ‚ n! |  j  |  j k rB t d ƒ ‚ n  |  j |  _  |  j ƒ  t j d ƒ |  j d k r¦ t j
 d ƒ |  j j t j |  j t ƒ  ƒ ƒ n t j
 d ƒ |  j j ƒ  d S(   s<   Override `AbstractAMQPConnectionWorkflow.abort()`.

        s   Cannot abort before starting.s*   Cannot abort after completion was reportedsF   AMQPConnectionWorkflow: beginning client-initiated asynchronous abort.s`   AMQPConnectionWorkflow.abort(): no connector, so just scheduling completion report via I/O loop.s=   AMQPConnectionWorkflow.abort(): requesting connector.abort().N(   R'   R&   R   RD   RE   RF   R7   R8   R{   R   RH   R   RI   RJ   RK   RL   R   RQ   (   R   (    (    sL   /srv/kernel/kteam-tools/dashboard/pika/adapters/utils/connection_workflow.pyRQ   —  s    
	c         C   sY   |  j  ƒ  d |  _ d |  _ d |  _ d |  _ d |  _ d |  _ d |  _ |  j	 |  _
 d S(   sr   Cancel asynchronous tasks and clean up to assist garbage collection.

        Transition to _STATE_DONE.

        N(   RF   R   Ry   R   Rz   R    R{   R|   R}   RD   R'   (   R   (    (    sL   /srv/kernel/kteam-tools/dashboard/pika/adapters/utils/connection_workflow.pyRR   ²  s    
							c         C   s,   |  j  d k	 r( |  j  j ƒ  d |  _  n  d S(   s$   Cancel asynchronous tasks.

        N(   R#   R   RS   (   R   (    (    sL   /srv/kernel/kteam-tools/dashboard/pika/adapters/utils/connection_workflow.pyRF   Ä  s    c         C   sS   t  | t ƒ r" t j d | ƒ n t j d | ƒ |  j } |  j ƒ  | | ƒ d S(   sÃ   Clean up and invoke client's `on_done` callback.

        :param pika.connection.Connection | AMQPConnectionWorkflowFailed result:
            value to pass to user's `on_done` callback.
        s.   AMQPConnectionWorkflow - reporting failure: %rs.   AMQPConnectionWorkflow - reporting success: %rN(   RT   RU   R7   RV   R8   R    RR   (   R   RW   R*   (    (    sL   /srv/kernel/kteam-tools/dashboard/pika/adapters/utils/connection_workflow.pyRL   Ì  s    	
c         C   s¿   d |  _ |  j d k s' t |  j ƒ ‚ |  j d k rf t |  j ƒ } t j d | ƒ |  j | ƒ d S|  j d 8_ t j	 d |  j ƒ d |  _
 |  j j | r¦ d n |  j |  j ƒ |  _ d S(   sQ  Start a new workflow cycle (if any more attempts are left) beginning
        with the first Parameters object in self._connection_configs. If out of
        attempts, report `AMQPConnectionWorkflowFailed`.

        :param bool first: if True, don't delay; otherwise delay next attempt by
            `self._retry_pause` seconds.
        i    s$   AMQP connection workflow failed: %r.Ni   sQ   Beginning a new AMQP connection workflow cycle; attempts remaining after this: %s(   R   R#   Ru   RP   R   R}   R7   RV   RL   RH   Rx   R   R<   Rv   t   _try_next_config_async(   R   R   RV   (    (    sL   /srv/kernel/kteam-tools/dashboard/pika/adapters/utils/connection_workflow.pyR‡   Ü  s    	
		c         C   sí   d |  _ |  j d k r$ d |  _ n |  j d 7_ |  j t |  j ƒ k rl t j d ƒ |  j d t ƒ d S|  j |  j } t j d | j	 | j
 ƒ |  j d k sª t ‚ |  j j d | j	 d | j
 d	 |  j d
 |  j d |  j ƒ |  _ d S(   sw   Attempt to connect using the next Parameters config. If there are no
        more configs, start a new cycle.

        i    i   s-   _try_next_config_async: starting a new cycle.R   Ns   _try_next_config_async: %r:%sRG   t   portt   socktypet   protoR*   (   R   R#   Rx   R   Ry   R7   RH   R‡   R6   RG   R‰   RP   R   t   getaddrinfot
   _SOCK_TYPEt   _IPPROTOt   _on_getaddrinfo_async_done(   R   t   params(    (    sL   /srv/kernel/kteam-tools/dashboard/pika/adapters/utils/connection_workflow.pyRˆ   ø  s"    					c         C   s   d |  _ t | t ƒ rL t j d | ƒ |  j j | ƒ |  j d t	 ƒ d St j
 d t | ƒ ƒ t | ƒ |  _ |  j ƒ  d S(   sä   Handles completion callback from asynchronous `getaddrinfo()`.

        :param list | BaseException addrinfos_or_exc: resolved address records
            returned by `getaddrinfo()` or an exception object from failure.
        s   getaddrinfo failed: %r.R   Ns   getaddrinfo returned %s records(   R   R#   RT   RU   R7   RV   R}   t   appendR‡   R6   RH   R   R€   R|   t   _try_next_resolved_address(   R   t   addrinfos_or_exc(    (    sL   /srv/kernel/kteam-tools/dashboard/pika/adapters/utils/connection_workflow.pyR     s    	c         C   sŽ   y t  |  j ƒ } Wn) t k
 r> t j d ƒ |  j ƒ  d SXt j d | ƒ |  j ƒ  |  _ |  j j d | d |  j	 |  j
 d |  j ƒ d S(   s}   Try connecting using next resolved address. If there aren't any left,
        continue with next Parameters config.

        s8   _try_next_resolved_address: continuing with next config.Ns-   Attempting to connect using address record %rR@   RA   R*   (   t   nextR|   t   StopIterationR7   RH   Rˆ   Rz   R{   RC   Ry   Rx   t   _on_connector_done(   R   R@   (    (    sL   /srv/kernel/kteam-tools/dashboard/pika/adapters/utils/connection_workflow.pyR’   )  s    
c         C   s  d |  _ t j d | ƒ t | t ƒ rö |  j j | ƒ t | t ƒ r„ |  j	 |  j
 k sq t d j |  j	 ƒ ƒ ‚ |  j t ƒ  ƒ q|  j ré t | t ƒ ré t j d ƒ t | j t j j ƒ rÊ t } n t |  j ƒ } |  j | ƒ q|  j ƒ  n |  j | ƒ d S(   sÌ   Handle completion of connection attempt by `AMQPConnector`.

        :param pika.connection.Connection | BaseException conn_or_exc: See
            `AMQPConnector.start()` for exception details.

        s$   Connection attempt completed with %rs&   Expected _STATE_ABORTING, but got {!r}sc   Ending AMQP connection workflow after first failed AMQP handshake due to _until_first_amqp_attempt.N(   R   R{   R7   RH   RT   RU   R}   R‘   R   R'   RE   RP   R   RL   R   Rw   R   R   R.   R   t   ConnectionOpenAbortedR   R’   (   R   t   conn_or_excRV   (    (    sL   /srv/kernel/kteam-tools/dashboard/pika/adapters/utils/connection_workflow.pyR–   ?  s(    				(   R   R   R   R,   t   SOCK_STREAMR   t   IPPROTO_TCPRŽ   R&   R†   RE   RD   R6   R
   R~   RC   RQ   RR   RF   RL   R‡   Rˆ   R   R’   R–   (    (    (    sL   /srv/kernel/kteam-tools/dashboard/pika/adapters/utils/connection_workflow.pyRt     s&   		*		,								(   R   RJ   t   loggingR,   t   pika.compatR.   t   pika.exceptionst   pika.tcp_socket_optsR    t	   getLoggerR   R7   R   R   R   R   R   R   R   R   R   R   R   R   t   objectR   R/   t   AbstractBaseRo   Rt   (    (    (    sL   /srv/kernel/kteam-tools/dashboard/pika/adapters/utils/connection_workflow.pyt   <module>   s.   ÿ ƒ-