
    `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
mZ 	 dd	lZn# e$ r d	ZY nw xY w	 dd	lZn# e$ r d	ZY nw xY wd
ZdZ G d de          Zd	S )z#Elasticsearch result store backend.    )datetimebytes_to_str)
_parse_url)states)ImproperlyConfigured   )KeyValueStoreBackendN)ElasticsearchBackendzVYou need to install the elasticsearch library to use the Elasticsearch result backend.c                        e Zd ZdZdZdZdZdZdZdZ	dZ
dZdZd	Zd fd
	Zd Zd Zd Zd Zd Zd Zd Z fdZ fdZd Zd Zd Zed             Z xZS )r   zElasticsearch Backend.

    Raises:
        celery.exceptions.ImproperlyConfigured:
            if module :pypi:`elasticsearch` is not available.
    celeryNhttp	localhosti#  F
      c                     t                      j        |i | || _        | j        j        j        }t          t          t                    d x}x}x}x}x}	x}
}|rOt          |          \  }}}	}
}}}|dk    rd }|r.|
                    d          }|                    d          \  }}}|p| j        | _        |p| j        | _        |p| j        | _        |p| j        | _        |	p| j        | _        |
p| j        | _        |p| j        | _         |d          p| j        | _         |d          }||| _         |d          }||| _         |dd          | _        d | _        d S )Nelasticsearch/elasticsearch_retry_on_timeoutelasticsearch_timeoutelasticsearch_max_retrieselasticsearch_save_meta_as_textT)super__init__urlappconfgetr   r   E_LIB_MISSINGr   strip	partitionindexdoc_typeschemehostportusernamepasswordes_retry_on_timeout
es_timeoutes_max_retrieses_save_meta_as_text_server)selfr   argskwargs_getr"   r#   r$   r%   r&   r'   r(   path_r*   r+   	__class__s                   O/var/www/html/env/lib/python3.11/site-packages/celery/backends/elasticsearch.pyr   zElasticsearchBackend.__init__1   s   $)&)))x}  &}555HLLLL6LDL4L(X 	9>Hoo;FD$(D!(( 9zz#%)^^C%8%8"q((dj
 1DM+%DI	%DI	 1DM 1DM D122Nd6N 	  T122
!(DO9::%"0D$(D)JD$Q$Q!    c                     t          |t          j        j                  r|j        dv rdS t          |t          j        j                  rdS dS )N>           N/A  TF)
isinstancer   
exceptionsApiErrorstatus_codeTransportError)r.   excs     r5   exception_safe_to_retryz,ElasticsearchBackend.exception_safe_to_retryZ   sQ    c=3<== 	 "BBBtcM4CDD 	4ur6   c                     	 |                      |          }	 |d         r|d         d         S d S # t          t          f$ r Y d S w xY w# t          j        j        $ r Y d S w xY w)Nfound_sourceresult)r1   	TypeErrorKeyErrorr   r?   NotFoundError)r.   keyress      r5   r   zElasticsearchBackend.geth   s    	))C..Cw< 4y>(334 4x(   '5 	 	 	DD	s,   A 0 AA AA A A c                     | j         r'| j                            | j        || j                   S | j                            | j        |          S N)r"   idr#   )r"   rP   )r#   serverr   r"   r.   rL   s     r5   r1   zElasticsearchBackend._gets   s]    = 
	;??j #    ;??j #   r6   c                     |d                     t          j                                                    d d                   d}	 |                     ||           d S # t
          j        j        $ r |                     |||           Y d S w xY w)Nz{}Z)rH   z
@timestamp)rP   body)	formatr   utcnow	isoformat_indexr   r?   ConflictError_update)r.   rL   valuestaterU   s        r5   _set_with_statez$ElasticsearchBackend._set_with_state   s    ,,!!++--crc2 
 
	+KK       '5 	+ 	+ 	+LLdE******	+s   A +BBc                 0    |                      ||d           S N)r^   )r.   rL   r\   s      r5   setzElasticsearchBackend.set   s    ##C555r6   c           	         d |                                 D             }| j        r1 | j        j        dt	          |          | j        | j        |ddid|S  | j        j        dt	          |          | j        |ddid|S )Nc                 4    i | ]\  }}t          |          |S  r   .0kvs      r5   
<dictcomp>z/ElasticsearchBackend._index.<locals>.<dictcomp>   $    <<<tq!Q<<<r6   op_typecreaterP   r"   r#   rU   paramsrP   r"   rU   rn   rd   )itemsr#   rQ   r"   r   )r.   rP   rU   r0   s       r5   rY   zElasticsearchBackend._index   s    <<tzz||<<<= 	$4;$ ##j!8,     %4;$ ##j!8,	 
   r6   c                    d |                                 D             }	 |                     |          }|                    d          s | j        ||fi |S n)# t          j        j        $ r  | j        ||fi |cY S w xY w	 |                     |d         d                   }|d         t          j	        k    rddiS |d         t          j
        v r|t          j        v rddiS n# t          t          f$ r Y nw xY w|                    dd	          }|                    d
d	          }| j        r5 | j        j        dt#          |          | j        | j        d|i||dd|}	n. | j        j        dt#          |          | j        d|i||dd|}	|	d         dk    rkt          j                            dt)          j        ddt)          j                    dt)          j        | j        | j        | j                            d          |	S )au  Update state in a conflict free manner.

        If state is defined (not None), this will not update ES server if either:
        * existing state is success
        * existing state is a ready state and current state in not a ready state

        This way, a Retry state cannot override a Success or Failure, and chord_unlock
        will not retry indefinitely.
        c                 4    i | ]\  }}t          |          |S rd   r   re   s      r5   ri   z0ElasticsearchBackend._update.<locals>.<dictcomp>   rj   r6   )rL   rF   rG   rH   statusnoop_seq_nor	   _primary_termdoc)if_primary_term	if_seq_norm   ro   z(conflicting update occurred concurrentlyr=   zHTTP/1.1r   Nrd   )rp   r1   r   rY   r   r?   rK   decode_resultr   SUCCESSREADY_STATESUNREADY_STATESrI   rJ   r#   rQ   updater   r"   rZ   elastic_transportApiResponseMetaHttpHeaders
NodeConfigr$   r%   r&   )
r.   rP   rU   r]   r0   res_getmeta_present_on_backendseq_no	prim_termrM   s
             r5   r[   zElasticsearchBackend._update   s    =<tzz||<<<	3iiBi''G;;w'' 7"t{2t66v6667 '5 	3 	3 	34;r422622222	3
	*&*&8&89KH9U&V&V# 'x0FNBB &))(2f6IIIeW]WlNlNl &)) 8$ 	 	 	D	 Y**KK33	 = 	$$+$ ##jT]+46JJ   CC %$+$ ##jT]+46JJ	 
  C x=F""*88:!1#z2C2O2Q2QSTVgVr6:k49diWY WYZ Z[_	a a a
 
s#   9A #B B!C' 'C;:C;c                    | j         r!t                                          |          S t          |t                    s!t                                          |          S |                    d          r$|                     |d                   d         |d<   |                    d          r$|                     |d                   d         |d<   |S )NrH      	traceback)r,   r   encoder>   dictr   _encode)r.   datar4   s     r5   r   zElasticsearchBackend.encode   s    $ 		77>>$'''dD)) ,ww~~d+++xx!! A!%d8n!=!=a!@Xxx$$ G$(LLk1B$C$CA$F[!Kr6   c                    | j         r!t                                          |          S t          |t                    s!t                                          |          S |                    d          r*t                                          |d                   |d<   |                    d          r*t                                          |d                   |d<   |S )NrH   r   )r,   r   decoder>   r   r   )r.   payloadr4   s     r5   r   zElasticsearchBackend.decode   s    $ 		77>>'***gt,, /ww~~g...{{8$$ F$)GGNN783D$E$E!{{;'' L',ww~~gk6J'K'K$Nr6   c                        fd|D             S )Nc                 :    g | ]}                     |          S rd   )r   )rf   rL   r.   s     r5   
<listcomp>z-ElasticsearchBackend.mget.<locals>.<listcomp>  s#    ...#...r6   rd   )r.   keyss   ` r5   mgetzElasticsearchBackend.mget  s    ........r6   c                     | j         r)| j                            | j        || j                    d S | j                            | j        |           d S rO   )r#   rQ   deleter"   rR   s     r5   r   zElasticsearchBackend.delete  sV    = 	9KTZC$-PPPPPKTZC88888r6   c                     d}| j         r| j        r| j         | j        f}t          j        | j         d| j         d| j         | j        | j        | j	        |          S )z$Connect to the Elasticsearch server.Nz://:)retry_on_timeoutmax_retriestimeout	http_auth)
r'   r(   r   Elasticsearchr$   r%   r&   r)   r+   r*   )r.   r   s     r5   _get_serverz ElasticsearchBackend._get_server
  su    	= 	7T] 	76I*{66ty664966!5+O
 
 
 	
r6   c                 P    | j         |                                 | _         | j         S r`   )r-   r   )r.   s    r5   rQ   zElasticsearchBackend.server  s%    <++--DL|r6   r`   )__name__
__module____qualname____doc__r"   r#   r$   r%   r&   r'   r(   r)   r*   r+   r   rD   r   r1   r^   ra   rY   r[   r   r   r   r   r   propertyrQ   __classcell__)r4   s   @r5   r   r      sd         EHFDDHHJN' ' ' ' ' 'R  	 	 	  + + + 6 6 6  (@ @ @D
 
 
 
 

 
 
 
 
/ / /9 9 9
 
 
   X    r6   r   )r   r   kombu.utils.encodingr   kombu.utils.urlr   r   r   celery.exceptionsr   baser
   r   ImportErrorr   __all__r   r   rd   r6   r5   <module>r      s7   ) )       - - - - - - & & & & & &       2 2 2 2 2 2 & & & & & &   MMM    $} } } } }/ } } } } }s   - 77A   A
	A
