
    `fy                        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dd
lmZ ddlmZmZmZ ddlmZ ddlmZ n# e$ r d	xZxZxZxZxZxZZY nw xY wdZdZdZ e
e          Z G d de          Zd	S )z3The CosmosDB/SQL backend for Celery (experimental).    )cached_property)bytes_to_str)
_parse_url)ImproperlyConfigured)
get_logger   )KeyValueStoreBackendN)DocumentClient)ConnectionPolicyConsistencyLevelPartitionKind)HTTPFailure)RetryOptions)CosmosDBSQLBackendi  i  c                        e Zd ZdZ	 	 	 	 	 	 d fd	Zed             Zed             Zd Z	d Z
ed             Zed	             Zd
 Zed             Zd Zd Zd Zd Z xZS )r   z CosmosDB/SQL backend for Celery.Nc                     t                      j        |i | t          t          d          | j        j        }	|                     |          \  | _        | _        |p|	d         | _	        |p|	d         | _
        	 t          t          |p|	d                   | _        n# t          $ r t          d          w xY w|p|	d         | _        |p|	d         | _        d S )NzIYou need to install the pydocumentdb library to use the CosmosDB backend.cosmosdbsql_database_namecosmosdbsql_collection_namecosmosdbsql_consistency_levelz"Unknown CosmosDB consistency levelcosmosdbsql_max_retry_attemptscosmosdbsql_max_retry_wait_time)super__init__pydocumentdbr   appconfr   	_endpoint_key_database_name_collection_namegetattrr   _consistency_levelAttributeError_max_retry_attempts_max_retry_wait_time)selfurldatabase_namecollection_nameconsistency_levelmax_retry_attemptsmax_retry_wait_timeargskwargsr   	__class__s             M/var/www/html/env/lib/python3.11/site-packages/celery/backends/cosmosdbsql.pyr   zCosmosDBSQLBackend.__init__!   s4    	$)&)))&$% % % x}$(OOC$8$8!	  .,- 	
  0./ 		M&- ! 645'7 '7D##  	M 	M 	M&'KLLL	M  312 	 
   423 	!!!s   >"B! !B;c                     t          |          \  }}}}}}}|r|st          d          |sd}|dk    rdnd}| d| d| }||fS )NzInvalid URLi  httpshttpz://:)r   r   )clsr'   _hostportpasswordschemeendpoints           r0   r   zCosmosDBSQLBackend._parse_urlM   s}    +5c??(4q(Aq 	68 	6&}555 	D CKKV......!!    c                     t                      }t          | j        | j                  |_        t	          | j        d| j        i|| j                  }|                     |           | 	                    |           |S )zReturn the CosmosDB/SQL client.

        If this is the first call to the property, the client is created and
        the database and collection are initialized if they don't yet exist.

        )max_retry_attempt_countmax_wait_time_in_seconds	masterKey)connection_policyr*   )
r   r   r$   r%   r
   r   r   r"   _create_database_if_not_exists _create_collection_if_not_exists)r&   rA   clients      r0   _clientzCosmosDBSQLBackend._client[   s     -..)5$($<%)%>*@ *@ *@&  N$)$/"5	7 7 7 	++F333--f555r<   c                     	 |                     d| j        i           t                              d| j                   d S # t          $ r}|j        t          k    r Y d }~d S d }~ww xY w)NidzCreated CosmosDB database %s)CreateDatabaser   LOGGERinfor   status_codeERROR_EXISTSr&   rD   exs      r0   rB   z1CosmosDBSQLBackend._create_database_if_not_existss   s    	-!!4)<"=>>>
 KK6+- - - - -	  	 	 	~-- .-----	s   A   
A&
A!!A&c                    	 |                     | j        | j        dgt          j        dd           t
                              d| j        | j                   d S # t          $ r}|j	        t          k    r Y d }~d S d }~ww xY w)Nz/id)pathskind)rG   partitionKeyz!Created CosmosDB collection %s/%s)CreateCollection_database_linkr    r   HashrI   rJ   r   r   rK   rL   rM   s      r0   rC   z3CosmosDBSQLBackend._create_collection_if_not_exists}   s    	D###,,17*7*<"> ">? ?@ @ @ KK;+T-BD D D D D	  	 	 	~-- .-----	s   1A 
B%A<<Bc                     d| j         z   S )Nzdbs/)r   r&   s    r0   rT   z!CosmosDBSQLBackend._database_link   s    +++r<   c                 &    | j         dz   | j        z   S )Nz/colls/)rT   r    rW   s    r0   _collection_linkz#CosmosDBSQLBackend._collection_link   s    "Y.1FFFr<   c                     | j         dz   |z   S )Nz/docs/)rY   r&   keys     r0   _get_document_linkz%CosmosDBSQLBackend._get_document_link   s    $x/#55r<   c                 T    |r|                                 rt          d          d|iS )Nz(Key cannot be none, empty or whitespace.rR   )isspace
ValueError)r5   r\   s     r0   _get_partition_keyz%CosmosDBSQLBackend._get_partition_key   s5     	Ickkmm 	IGHHH$$r<   c                 n   t          |          }t                              d| j        | j        |           	 | j                            |                     |          |                     |                    }|	                    d          S # t          $ r}|j        t          k    r Y d}~dS d}~ww xY w)zxRead the value stored at the given key.

        Args:
              key: The key for which to read the value.

        z"Getting CosmosDB document %s/%s/%svalueN)r   rI   debugr   r    rE   ReadDocumentr]   ra   getr   rK   ERROR_NOT_FOUND)r&   r\   documentrN   s       r0   rf   zCosmosDBSQLBackend.get   s     39($*?	F 	F 	F		)|00'',,'',,. .H <<(((  	 	 	~0044444	s   AB 
B4B//B4c                     t          |          }t                              d| j        | j        |           | j                            | j        ||d|                     |                     dS )zStore a value for a given key.

        Args:
              key: The key at which to store the value.
              value: The value to store.

        z#Creating CosmosDB document %s/%s/%s)rG   rc   N)	r   rI   rd   r   r    rE   CreateDocumentrY   ra   )r&   r\   rc   s      r0   setzCosmosDBSQLBackend.set   s     3:($*?	F 	F 	F 	##!''##C((	* 	* 	* 	* 	*r<   c                        fd|D             S )zqRead all the values for the provided keys.

        Args:
              keys: The list of keys to read.

        c                 :    g | ]}                     |          S  )rf   ).0r\   r&   s     r0   
<listcomp>z+CosmosDBSQLBackend.mget.<locals>.<listcomp>   s#    ...#...r<   rn   )r&   keyss   ` r0   mgetzCosmosDBSQLBackend.mget   s     /.......r<   c                     t          |          }t                              d| j        | j        |           | j                            |                     |          |                     |                     dS )zlDelete the value at a given key.

        Args:
              key: The key of the value to delete.

        z#Deleting CosmosDB document %s/%s/%sN)	r   rI   rd   r   r    rE   DeleteDocumentr]   ra   r[   s     r0   deletezCosmosDBSQLBackend.delete   s     3:($*?	F 	F 	F 	####C((##C((	* 	* 	* 	* 	*r<   )NNNNNN)__name__
__module____qualname____doc__r   classmethodr   r   rE   rB   rC   rT   rY   r]   ra   rf   rk   rr   ru   __classcell__)r/   s   @r0   r   r      sN       ** #!%#'$(%)*5 *5 *5 *5 *5 *5X " " ["   _.- - -D D D , , _, G G _G6 6 6 % % [%) ) ),* * *"/ / /* * * * * * *r<   r   )ry   kombu.utilsr   kombu.utils.encodingr   kombu.utils.urlr   celery.exceptionsr   celery.utils.logr   baser	   r   pydocumentdb.document_clientr
   pydocumentdb.documentsr   r   r   pydocumentdb.errorsr   pydocumentdb.retry_optionsr   ImportError__all__rg   rL   rv   rI   r   rn   r<   r0   <module>r      s   9 9 ' ' ' ' ' ' - - - - - - & & & & & & 2 2 2 2 2 2 ' ' ' ' ' ' & & & & & &=;;;;;;XXXXXXXXXX//////7777777 = = =8<=L => =$4 =} ==&= " 	H		|* |* |* |* |*- |* |* |* |* |*s    A	 	AA