o
    Df                     @   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y1   dZY nw 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                       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.T
consistentNc                    sD   t  j|i | | jd u r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__r	   O/home/ubuntu/webapp/venv/lib/python3.10/site-packages/celery/backends/consul.pyr   %   s
   
zConsulBackend.__init__c                 K   s>   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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   Consulr   r   r!   )r   r	   r	   r   r   A   s   zConsulBackend.clientc                 C   s&   t |}| jd u r|S | j d| S )N/)r   r   r   keyr	   r	   r   _key_to_consul_keyF   s   z ConsulBackend._key_to_consul_keyc                 C   sJ   |  |}td| z|  j|\}}|d W S  ty$   Y d S w )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 )N)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)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%   r2   session_namer   
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   supports_autoexpirer!   r   r   r   r   r&   r   r-   r:   r.   __classcell__r	   r	   r   r   r      s    	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    