
    h                         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
 	 ddlZ e
e      ZdZd	Z G d
 de      Zy# e$ r dZY !w xY w)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                   \     e Zd ZdZeZdZdZdZ fdZd Z	d Z
d Zd	 Zd
 Zd Zd Z xZS )r   z'Consul.io K/V store backend for Celery.T
consistentNc                     t        |   |i | | j                  t        t              d | _         | j                  di t        | j                         y )N )	super__init__consulr   CONSUL_MISSING
one_client_init_from_paramsr   url)selfargskwargs	__class__s      R/var/www/Befach/backend/env/lib/python3.12/site-packages/celery/backends/consul.pyr   zConsulBackend.__init__%   sO    $)&);;&~66 5488!45    c                     t         j                  d||       || _        || _        || _        |j                  dd       r| j                         | _        y y )Nz,Setting on Consul client to connect to %s:%dr   )loggerdebugpathhostnameportgetclientr   )r   r   r   virtual_hostparamss        r   r   zConsulBackend._init_from_params3   sM    Ct	% 	 	 ::lD)"kkmDO *r   c                     | j                   xs6 t        j                  | j                  | j                  | j
                        S )N)hostr   consistency)r   r   Consulr   r   r&   )r   s    r   r!   zConsulBackend.clientA   s7     N&--T]]59YY<@<L<L#N 	Nr   c                 V    t        |      }| j                  |S | j                   d| S )N/)r   r   r   keys     r   _key_to_consul_keyz ConsulBackend._key_to_consul_keyF   s/    3ii'sA		{!C5-AAr   c                     | j                  |      }t        j                  d|       	 | j                         j                  j                  |      \  }}|d   S # t        $ r Y y w xY w)Nz"Trying to fetch key %s from ConsulValue)r,   r   r   r!   kvr    	TypeError)r   r+   _datas       r   r    zConsulBackend.getJ   sb    %%c*93?	kkm&&**3/GAt=  		s   0A 	A&%A&c              #   @   K   |D ]  }| j                  |        y w)N)r    )r   keysr+   s      r   mgetzConsulBackend.mgetS   s     C((3- s   c                    t        |      }| j                  |      }t        j                  d|| j                         | j                         }|j                  j                  |d| j                        }t        j                  d|       t        j                  d|       |j                  j                  |||      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+   r;   session_namer!   
session_ids         r   setzConsulBackend.setW   s     $C(%%c*E!4<<	1^^**4</3|| + =
 	0*=/5yy}}E:}FFr   c                     | j                  |      }t        j                  d|       | j                         j                  j                  |      S )NzRemoving key %s from Consul)r,   r   r   r!   r/   r7   r*   s     r   r7   zConsulBackend.deleteq   s>    %%c*2C8{{}&&s++r   )__name__
__module____qualname____doc__r   supports_autoexpirer&   r   r   r   r!   r,   r    r5   rC   r7   __classcell__)r   s   @r   r   r      sF    1FKD6,N
B G4,r   r   )rH   kombu.utils.encodingr   kombu.utils.urlr   celery.backends.baser   celery.exceptionsr   celery.utils.logr   r   ImportErrorrE   r   __all__r   r   r   r   r   <module>rR      s`   
 . % 5 2 ' 
H	
$
Y,( Y,  Fs   > AA