a
    xd)                     @   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yb   dZY n0 e
eZdZd	ZG d
d deZdS )zConsul 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                       sd   e Zd ZdZeZd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  ZS )r   z'Consul.io K/V store backend for Celery.TNZ
consistentc                    s>   t  j|i | | jd u r$tt| jf i t| j d S N)super__init__consulr   CONSUL_MISSING_init_from_paramsr   url)selfargskwargs	__class__ N/var/www/html/Ranjet/env/lib/python3.9/site-packages/celery/backends/consul.pyr
   &   s    
zConsulBackend.__init__c                 K   s,   t d|| || _tj||| jd| _d S )Nz,Setting on Consul client to connect to %s:%d)hostportconsistency)loggerdebugpathr   ZConsulr   client)r   hostnamer   Zvirtual_hostparamsr   r   r   r   .   s    zConsulBackend._init_from_paramsc                 C   s&   t |}| jd u r|S | j d| S )N/)r   r   r   keyr   r   r   _key_to_consul_key5   s    z ConsulBackend._key_to_consul_keyc                 C   sJ   |  |}td| z| jj|\}}|d W S  tyD   Y n0 d S )Nz"Trying to fetch key %s from ConsulValue)r"   r   r   r   kvget	TypeError)r   r!   _datar   r   r   r%   9   s    

zConsulBackend.getc                 c   s   |D ]}|  |V  qd S r   )r%   )r   keysr!   r   r   r   mgetB   s    zConsulBackend.mgetc                 C   sd   t |}| |}td|| j | jjj|d| jd}td| td| | j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_nameZ
session_idr   r   r   setF   s    


zConsulBackend.setc                 C   s$   |  |}td| | jj|S )NzRemoving key %s from Consul)r"   r   r   r   r$   r+   r    r   r   r   r+   a   s    
zConsulBackend.delete)__name__
__module____qualname____doc__r   Zsupports_autoexpirer   r   r   r
   r   r"   r%   r*   r3   r+   __classcell__r   r   r   r   r      s   	r   )r7   Zkombu.utils.encodingr   Zkombu.utils.urlr   Zcelery.backends.baser   Zcelery.exceptionsr   Zcelery.utils.logr   r   ImportErrorr4   r   __all__r   r   r   r   r   r   <module>   s   
