
    `f                         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n# e$ r dZY nw xY w e
e          ZdZd	Z G 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                   \     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                      j        |i | | j        t          t                    d | _         | j        di t          | j                   d S )N )	super__init__consulr   CONSUL_MISSING
one_client_init_from_paramsr   url)selfargskwargs	__class__s      H/var/www/html/env/lib/python3.11/site-packages/celery/backends/consul.pyr   zConsulBackend.__init__%   sg    $)&)));&~666 5548!4!455555    c                     t                               d||           || _        || _        || _        |                    dd           r|                                 | _        d S d S )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   sj    Ct	% 	% 	% 	 	 ::lD)) 	,"kkmmDOOO	, 	,r   c                 \    | j         p%t          j        | j        | j        | j                  S )N)hostr   consistency)r   r   Consulr   r   r&   )r   s    r   r!   zConsulBackend.clientA   s8     N&-T]59Y<@<L#N #N #N 	Nr   c                 J    t          |          }| j        |n| j         d| S )N/)r   r   r   keys     r   _key_to_consul_keyz ConsulBackend._key_to_consul_keyF   s0    3i'ss	-A-AC-A-AAr   c                     |                      |          }t                              d|           	 |                                 j                            |          \  }}|d         S # t          $ r Y d S 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   s}    %%c**93???	kkmm&**3//GAt=  	 	 	DD	s   6A) )
A76A7c              #   B   K   |D ]}|                      |          V  d S )N)r    )r   keysr+   s      r   mgetzConsulBackend.mgetS   s8       	  	 C((3--	  	 r   c                    t          |          }|                     |          }t                              d|| j                   |                                 }|j                            |d| j                  }t                              d|           t                              d|           |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 	1 	1^**4</3| + = =
 	0*===/555y}}E:}FFFr   c                     |                      |          }t                              d|           |                                 j                            |          S )NzRemoving key %s from Consul)r,   r   r   r!   r/   r7   r*   s     r   r7   zConsulBackend.deleteq   sI    %%c**2C88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      s        11FKD6 6 6 6 6, , ,N N N
B B B       G G 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 5 5 5 5 5 2 2 2 2 2 2 ' ' ' ' ' 'MMMM   FFF 
H		
$
Y, Y, Y, Y, Y,( Y, Y, Y, Y, Y,s   ' 11