U
    Z¨+dè  ã                   @   sŠ   d 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
 zddlZW n ek
rd   dZY nX e
eƒZdZd	ZG d
d„ deƒZdS )zŽConsul result store backend.

- :class:`ConsulBackend` implements KeyValueStoreBackend to store results
    in the key-value store of Consul.
é    )Úbytes_to_str)Ú	parse_url)ÚKeyValueStoreBackend)ÚImproperlyConfigured)Ú
get_loggerN)ÚConsulBackendz^You need to install the python-consul library in order to use the Consul result store backend.c                       sh   e Zd ZdZeZdZdZdZ‡ fdd„Zdd„ Z	d	d
„ Z
dd„ Zdd„ Zdd„ Zdd„ Zdd„ Z‡  ZS )r   z'Consul.io K/V store backend for Celery.TZ
consistentNc                    s<   t ƒ j||Ž | jd kr ttƒ‚d | _| jf t| jƒŽ d S ©N)	ÚsuperÚ__init__Úconsulr   ÚCONSUL_MISSINGÚ
one_clientÚ_init_from_paramsr   Úurl)ÚselfÚargsÚkwargs©Ú	__class__© ú:/tmp/pip-unpacked-wheel-ucduq0nd/celery/backends/consul.pyr
   %   s
    
zConsulBackend.__init__c                 K   s:   t  d||¡ || _|| _|| _| dd ¡r6|  ¡ | _d S )Nz,Setting on Consul client to connect to %s:%dr   )ÚloggerÚdebugÚpathÚhostnameÚportÚgetÚclientr   )r   r   r   Zvirtual_hostÚparamsr   r   r   r   3   s     ÿzConsulBackend._init_from_paramsc                 C   s   | j ptj| j| j| jdS )N)Úhostr   Úconsistency)r   r   ZConsulr   r   r    )r   r   r   r   r   A   s    þzConsulBackend.clientc                 C   s&   t |ƒ}| jd kr|S | j› d|› S )Nú/)r   r   ©r   Úkeyr   r   r   Ú_key_to_consul_keyF   s    z ConsulBackend._key_to_consul_keyc                 C   sN   |   |¡}t d|¡ z|  ¡ j |¡\}}|d W S  tk
rH   Y nX d S )Nz"Trying to fetch key %s from ConsulÚValue)r$   r   r   r   Úkvr   Ú	TypeError)r   r#   Ú_Údatar   r   r   r   J   s    

zConsulBackend.getc                 c   s   |D ]}|   |¡V  qd S r   )r   )r   Úkeysr#   r   r   r   ÚmgetS   s    zConsulBackend.mgetc                 C   sh   t |ƒ}|  |¡}t d|| j¡ |  ¡ }|jj|d| jd}t d|¡ t d|¡ |jj	|||dS )aM  Set a key in Consul.

        Before creating the key it will create a session inside Consul
        where it creates a session with a TTL

        The key created afterwards will reference to the session's ID.

        If the session expires it will remove the key so that results
        can auto expire from the K/V store
        z.Trying to create Consul session %s with TTL %dÚdelete)ÚnameZbehaviorZttlzCreated Consul session %szWriting key %s to Consul)r#   ÚvalueÚacquire)
r   r$   r   r   Úexpiresr   ÚsessionÚcreater&   Úput)r   r#   r.   Zsession_namer   Z
session_idr   r   r   ÚsetW   s    
 ÿþzConsulBackend.setc                 C   s&   |   |¡}t d|¡ |  ¡ j |¡S )NzRemoving key %s from Consul)r$   r   r   r   r&   r,   r"   r   r   r   r,   q   s    
zConsulBackend.delete)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Zsupports_autoexpirer    r   r
   r   r   r$   r   r+   r4   r,   Ú__classcell__r   r   r   r   r      s   	r   )r8   Zkombu.utils.encodingr   Zkombu.utils.urlr   Zcelery.backends.baser   Zcelery.exceptionsr   Zcelery.utils.logr   r   ÚImportErrorr5   r   Ú__all__r   r   r   r   r   r   Ú<module>   s   
