o
    wvXj¡  ã                   @   s˜   d Z ddl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 zddlZW n ey9   dZY nw 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.
é    )Úabsolute_importÚunicode_literals)Ú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.TNÚ
consistentc                    sB   t t| ƒj|i |¤Ž | jd u rttƒ‚| jdi t| jƒ¤Ž d S )N© )	Úsuperr	   Ú__init__Úconsulr   ÚCONSUL_MISSINGÚ_init_from_paramsr   Úurl)ÚselfÚargsÚkwargs©Ú	__class__r   úS/var/www/html/myproject/venv/lib/python3.10/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   ÚConsulr   Úclient)r   Úhostnamer   Úvirtual_hostÚparamsr   r   r   r   1   s   ÿÿzConsulBackend._init_from_paramsc                 C   s$   t |ƒ}| jd u r|S d | j|¡S )Nz{0}/{1})r   r   Úformat©r   Úkeyr   r   r   Ú_key_to_consul_key8   s   z ConsulBackend._key_to_consul_keyc                 C   sH   |   |¡}t d|¡ z| jj |¡\}}|d W S  ty#   Y d S w )Nz"Trying to fetch key %s from ConsulÚValue)r&   r   r   r   ÚkvÚgetÚ	TypeError)r   r%   Ú_Údatar   r   r   r)   <   s   

ÿzConsulBackend.getc                 c   s   � |D ]}|   |¡V  qd S )N)r)   )r   Úkeysr%   r   r   r   ÚmgetE   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)ÚnameÚbehaviorÚttlzCreated Consul session %szWriting key %s to Consul)r%   ÚvalueÚacquire)
r   r&   r   r   Úexpiresr   ÚsessionÚcreater(   Úput)r   r%   r3   ÚstateÚsession_nameÚ
session_idr   r   r   ÚsetI   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/   d   s   
zConsulBackend.delete)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úsupports_autoexpirer   r   r   r   r   r&   r)   r.   r<   r/   Ú__classcell__r   r   r   r   r	      s    	r	   )r@   Ú
__future__r   r   Úkombu.utils.encodingr   Úkombu.utils.urlr   Úcelery.backends.baser   Úcelery.exceptionsr   Úcelery.utils.logr   r   ÚImportErrorr=   r   Ú__all__r   r	   r   r   r   r   Ú<module>   s    ÿ