
    h{0                        d Z ddlmZ ddlmZ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mZ dd
lmZmZ ddlmZ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 ddl m!Z! ddl"m#Z# ddl$m%Z% ddl&m'Z' n# e($ r dZ#Y nw xY w	 ddl"m)Z)m*Z* n# e($ r dZ)dZ*Y nw xY wdZ+ ee,          Z- G d de          Z. G d de.          Z/dS )z5Google Cloud Storage result store backend for Celery.    )ThreadPoolExecutor)datetime	timedelta)getpid)RLock)bytes_to_str)
dictfilter)url_to_parts)maybe_signature)
ChordErrorImproperlyConfigured)GroupResultallow_join_result)
get_logger   )KeyValueStoreBackendNretry)Conflict)if_exception_type)storage)Client)DEFAULT_RETRY)	firestorefirestore_admin_v1)
GCSBackendc                   x     e Zd ZdZ fdZd Zd Zd Zd Ze	d             Z
e	d             Zd	 Zd
 Zd Z xZS )GCSBackendBasez)Google Cloud Storage task result backend.c                    t           st          d           t                      j        di | t	                      | _        t                      | _        t          | _	        d | _
        | j        j        }| j        r.|                                 } |j        di t!          |           |                    d          | _        | j        st          d          |                    d          | _        | j        st          d          |                    dd                              d          | _        t-          |                    d	d
                    | _        t1          |                    d          pd          | _        | j        dk     rt          d| j         d          | j        r+|                                 st          d| j                   d S d S )Nz8You must install google-cloud-storage to use gcs backend
gcs_bucketz:Missing bucket name: specify gcs_bucket to use gcs backendgcs_projectz6Missing project:specify gcs_project to use gcs backendgcs_base_path /gcs_threadpool_maxsize
   gcs_ttlr   zInvalid ttl: z# must be greater than or equal to 0z>Missing lifecycle rule to use gcs backend with ttl on bucket:  )r   r   super__init__r   _client_lockr   _pidr   _retry_policy_clientappconfurl_params_from_urlupdater	   getbucket_nameprojectstrip	base_pathint_threadpool_maxsizefloatttl _is_bucket_lifecycle_rule_exists)selfkwargsr0   
url_params	__class__s       S/var/www/html/auto_sub_bot/venv/lib/python3.11/site-packages/celery/backends/gcs.pyr*   zGCSBackendBase.__init__-   s    	&J   	""6"""!GGHH	*x}8 	2..00JDK11*Z0011188L11 	&L   xx..| 	&H   /266<<SAA#&txx0H"'M'M#N#N ),,1228a<<&MMMM   X 	88:: *2#/2 2  	 	     c                     t          |          }|                     |          }	 |                    | j                  S # t          j        j        $ r Y d S w xY wNr   )r   	_get_blobdownload_as_bytesr-   r   blobNotFoundr>   keyrH   s      rB   r4   zGCSBackendBase.getU   sf    3~~c""	))0B)CCC|$ 	 	 	44	s   A AAc                     t          |          }|                     |          }| j        r.t          j                    t          | j                  z   |_        |                    || j                   d S )Nsecondsr   )	r   rF   r<   r   utcnowr   custom_timeupload_from_stringr-   )r>   rK   valuerH   s       rB   setzGCSBackendBase.set]   so    3~~c""8 	O'009TX3N3N3NNDT-?@@@@@rC   c                     t          |          }|                     |          }|                                r|                    | j                   d S d S rE   )r   rF   existsdeleter-   rJ   s      rB   rV   zGCSBackendBase.deleted   sW    3~~c"";;== 	2KKd0K11111	2 	2rC   c                     t                      5 }t          |                    | j        |                    cd d d            S # 1 swxY w Y   d S N)r   listmapr4   )r>   keyspools      rB   mgetzGCSBackendBase.mgetj   s    !! 	2T40011	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2 	2s   (AAAc                    | j         5  | j        r*| j        t                      k    r| j        cddd           S t	          | j                  | _        t                      | _        t          j                            | j	        | j	        d          }| j        j
        }|                    d|           |j        j                            d|           | j        cddd           S # 1 swxY w Y   dS )zReturns a storage client.Nr6      )pool_connectionspool_maxsizemax_retrieszhttps://)r+   r.   r,   r   r   r6   requestsadaptersHTTPAdapterr:   _httpmount_auth_requestsession)r>   adapterclient_https      rB   clientzGCSBackendBase.clientn   sJ   
  	  	 | $	VXX 5 5|	  	  	  	  	  	  	  	  "$,777DLDI '33!%!9!5 4  G
 ,,Kj'222%-33JHHH<#	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	  	 s   %C(B!C((C,/C,c                 @    | j                             | j                  S rX   )rm   bucketr5   r>   s    rB   ro   zGCSBackendBase.bucket   s    {!!$"2333rC   c                 `    | j         r| j          d| n|}| j                            |          S )Nr$   )r8   ro   rH   )r>   rK   key_bucket_paths      rB   rF   zGCSBackendBase._get_blob   s9    7;~NT^33c3333{000rC   c                 z    | j         }|                                 |j        D ]}|d         d         dk    r dS dS )NactiontypeDeleteTF)ro   reloadlifecycle_rules)r>   ro   rules      rB   r=   z/GCSBackendBase._is_bucket_lifecycle_rule_exists   sM    * 	 	DH~f%11tt 2urC   c                 V    t          | j                  }|j        |j        d|j        S )N)r    r"   )r
   r1   hostnamepathquery)r>   	url_partss     rB   r2   zGCSBackendBase._params_from_url   s9     **	 $,&^
 
 o
 	
rC   )__name__
__module____qualname____doc__r*   r4   rS   rV   r]   propertyrm   ro   rF   r=   r2   __classcell__rA   s   @rB   r   r   *   s        33& & & & &P  A A A2 2 22 2 2     X . 4 4 X41 1 1  
 
 
 
 
 
 
rC   r   c                        e Zd ZdZdZdZdZdZdZ fdZ	e
d             Zd Z fd	Zd
edefdZd Zd Zd Zd Zd Z xZS )r   zWGoogle Cloud Storage task result backend.

    Uses Firestore for chord ref count.
    Tcelerychord_count
expires_atc                 b   t           rt          st          d           t                      j        di | t                      | _        d | _        | j        j	        
                    d| j                  | _        |                                 st          d| j         d| j                   d S )Nz:You must install google-cloud-firestore to use gcs backendfirestore_projectzHMissing TTL policy to use gcs backend with ttl on Firestore collection: z
 project: r(   )r   r   r   r)   r*   r   _firestore_lock_firestore_clientr/   r0   r4   r6   r    _is_firestore_ttl_policy_enabled_collection_name)r>   r?   rA   s     rB   r*   zGCSBackend.__init__   s     	0 	&L   	""6"""$ww!%!%!2!2"
 "
 4466 	&5)-)>5 5 25 5  	 	rC   c                    | j         5  | j        r*| j        t                      k    r| j        cddd           S t	          j        | j                  | _        t                      | _        ddd           n# 1 swxY w Y   | j        S )zReturns a firestore client.Nr_   )r   r   r,   r   r   r   r   rp   s    rB   firestore_clientzGCSBackend.firestore_client   s    
 ! 	! 	!% .$)vxx*?*?-	! 	! 	! 	! 	! 	! 	! 	! &/%5.& & &D" DI	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! 	! %%s   %A82A88A<?A<c                 >   t          j                    }d| j         d| j         d| j         }t          j        |          }|                    |          }|j        }|o<|j        t           j	        j
        j        j        t           j	        j
        j        j        hv S )Nz	projects/z&/databases/(default)/collectionGroups/z/fields/)name)request)r   FirestoreAdminClientr   r   _field_expiresGetFieldRequest	get_field
ttl_configstateField	TtlConfigStateACTIVECREATING)r>   rm   r   r   fieldr   s         rB   r   z+GCSBackend._is_firestore_ttl_policy_enabled   s    #8::-. - -595J- -*- - 	
 %4$???   11%
 
j.$.4;$.4=3
 
 	
rC   c                     |                      |d                                                   }|                     |d            t                      j        ||fi |S )Nr   iQ )get_key_for_chorddecode_expire_chord_keyr)   _apply_chord_incr)r>   header_result_argsbodyr?   rK   rA   s        rB   r   zGCSBackend._apply_chord_incr   s`    $$%7%:;;BBDDsE***(uww();TLLVLLLrC   rK   returnc                    |                      |          }|                    | j        t          j        d          idt          j        t          t                    dddd                    }|j	        d         j
        S )	Nr   Tg      ?g     f@g       @)	predicateinitialmaximum
multipliertimeout)merger   r   )_firestore_documentrS   _field_countr   	Incrementr   Retryr   r   transform_resultsinteger_value)r>   rK   docresps       rB   incrzGCSBackend.incr   s    &&s++ww	 3A 6 67++H55    

 

 %a(66rC   c                 6   | j         }|j        }|sdS |                     |          }|                     |          }|j                            d          }	|	)|                     ||          }
|
dS t          |
          }	||	k    rt          	                    d|           dS ||	k    r|                     ||          }
|
dS t          |j        |          }|
j        }	 t                      5   ||j        j        d          }ddd           n# 1 swxY w Y   	 |                    |           n# t           $ rL}t                              d||           |                     |t'          d|                     Y d}~nd}~ww xY w# t           $ r}	 t)          |
                                          }d	                    ||          }n# t.          $ r t1          |          }Y nw xY wt                              d||           |                     |t'          |                     Y d}~nd}~ww xY w|
                                 |                     |           dS # |
                                 |                     |           w xY wdS )
a~  Chord part return callback.

        Called for each task in the chord.
        Increments the counter stored in Firestore.
        If the counter reaches the number of tasks in the chord, the callback
        is called.
        If the callback raises an exception, the chord is marked as errored.
        If the callback returns a value, the chord is marked as successful.
        N
chord_sizez/Chord counter incremented too many times for %rr/   T)r   	propagateChord %r raised: %rzCallback error: zDependency {0.id} raised {1!r})r/   groupr   r   chordr4   _restore_depslenloggerwarningr   join_nativer   r0   result_chord_join_timeoutdelay	Exception	exceptionchord_error_from_stackr   next_failed_join_reportformatStopIterationreprrV   _delete_chord_key)r>   r   r   resultr?   r/   gidrK   valsizedepscallbackjretexcculpritreasons                    rB   on_chord_part_returnzGCSBackend.on_chord_part_return   sI    hm 	F$$S))iinn}  ..<%%c733D|t99D::NNA3     D[[%%c733D|&w}#>>>H A,&((  ! # B"&  C              $NN3''''    $$%:CEEE// "#=c#=#=>>         J J J'"4#;#;#=#=>>G=DD FF % ' ' '!#YYFFF'   !6VDDD++Hj6H6HIIIIIIIIJ, &&s+++++ &&s++++K [s   $F 2D
F DF DF "D9 7I+ 9
FAF
I+ 
FI+ 
H=7GH8G1.H80G11AH83I+ 8H==I+ ++Jc           	      (   | j         }	 t          j        ||           }np# t          $ rc}t	          |j        |          }t                              d||           |                     |t          d|                     Y d }~d S d }~ww xY w|	 t          |          # t          $ rc}t	          |j        |          }t                              d||           |                     |t          d| d                     Y d }~nd }~ww xY w|S )N)backendr   r   zCannot restore group: zChord callback %r raised: %rzGroupResult z no longer exists)r/   r   restorer   r   r   r   r   r   r   
ValueError)r>   r   r   r/   r   r   r   s          rB   r   zGCSBackend._restore_deps7  se   h		&sD999DD 	 	 	&w}#>>>H2C==='';C;;<<   FFFFF	 < oo%   *7=cBBB  !?cJJJ++DcDDDEE        s.     
BABBB" "
D,AD

Dc                 X    |                      |          }|                                 d S rX   )r   rV   )r>   rK   r   s      rB   r   zGCSBackend._delete_chord_keyO  s&    &&s++

rC   c                     t          j                    t          |          z   }|                     |          }|                    | j        |id           dS )zSet TTL policy for a Firestore document.

        Firestore ttl data is typically deleted within 24 hours after its
        expiration date.
        rM   T)r   N)r   rO   r   r   rS   r   )r>   rK   expiresval_expiresr   s        rB   r   zGCSBackend._expire_chord_keyS  sX     o'')G*D*D*DD&&s++$k2$?????rC   c                     | j                             | j                                      t	          |                    S rX   )r   
collectionr   documentr   )r>   rK   s     rB   r   zGCSBackend._firestore_document]  s8    $//!
 

(<$$
%
%	&rC   )r   r   r   r   implements_incrsupports_native_joinr   r   r   r*   r   r   r   r   bytesr9   r   r   r   r   r   r   r   r   s   @rB   r   r      s        
 O   L!N    ( & & X&
 
 
"M M M M M
7 7# 7 7 7 7?, ?, ?,B  0  @ @ @& & & & & & &rC   r   )0r   concurrent.futuresr   r   r   osr   	threadingr   kombu.utils.encodingr   kombu.utils.functionalr	   kombu.utils.urlr
   celery.canvasr   celery.exceptionsr   r   celery.resultr   r   celery.utils.logr   baser   rd   google.api_corer   google.api_core.exceptionsr   google.api_core.retryr   google.cloudr   google.cloud.storager   google.cloud.storage.retryr   ImportErrorr   r   __all__r   r   r   r   r(   rC   rB   <module>r      sl   ; ; 1 1 1 1 1 1 ( ( ( ( ( ( ( (             - - - - - - - - - - - - ( ( ( ( ( ( ) ) ) ) ) ) > > > > > > > > 8 8 8 8 8 8 8 8 ' ' ' ' ' ' & & & & & &	OOO%%%%%%333333777777$$$$$$++++++8888888   GGG:::::::::   I
  
H		s
 s
 s
 s
 s
) s
 s
 s
l@& @& @& @& @& @& @& @& @& @&s$   (A; ;BB	B 	BB