
    Ng                          d dl Z d dlZd dlZd dlZd dlZd dlZd dlmZ ddlm	Z	m
Z
  ej        e          ZdZdZ G d dej                  Z G d d	e
j                  ZdS )
    N)client   )
exceptionsutilsg?c                        e Zd Z fdZ xZS )PubSubWorkerThreadc                     	 t                                                       d S # t          $ r t          j                      w xY wN)superrun	Exception_threadinterrupt_main)self	__class__s    M/var/www/html/ai-engine/env/lib/python3.11/site-packages/portalocker/redis.pyr   zPubSubWorkerThread.run   sJ    	GGKKMMMMM 	 	 	"$$$	s	    % A)__name__
__module____qualname__r   __classcell__r   s   @r   r   r      s8                    r   c                       e Zd ZU dZej        eej        f         ed<   ej	        e
         ed<   eed<   eed<   ej	        ej                 ed<   dZej	        ej                 ed<   eed	<    ed
          Zej        ej        eej        f                  ed<   ddddeedfdedej	        ej                 dej	        e         dej	        e         dej	        e         dededej	        ej                 f fdZdej        fdZd Zed             Z	 	 	 ddej	        e         dej	        e         dej	        e         dd fdZd Zd Zd Z xZS )	RedisLockaD  
    An extremely reliable Redis lock based on pubsub with a keep-alive thread

    As opposed to most Redis locking systems based on key/value pairs,
    this locking method is based on the pubsub system. The big advantage is
    that if the connection gets killed due to network issues, crashing
    processes or otherwise, it will still immediately unlock instead of
    waiting for a lock timeout.

    To make sure both sides of the lock know about the connection state it is
    recommended to set the `health_check_interval` when creating the redis
    connection..

    Args:
        channel: the redis channel to use as locking key.
        connection: an optional redis connection if you already have one
        or if you need to specify the redis connection
        timeout: timeout when trying to acquire a lock
        check_interval: check interval while waiting
        fail_when_locked: after the initial lock failed, return an error
            or lock the file. This does not wait for the timeout.
        thread_sleep_time: sleep time between fetching messages from redis to
            prevent a busy/wait loop. In the case of lock conflicts this
            increases the time it takes to resolve the conflict. This should
            be smaller than the `check_interval` to be useful.
        unavailable_timeout: If the conflicting lock is properly connected
            this should never exceed twice your redis latency. Note that this
            will increase the wait time possibly beyond your `timeout` and is
            always executed if a conflict arises.
        redis_kwargs: The redis connection arguments if no connection is
            given. The `DEFAULT_REDIS_KWARGS` are used as default, if you want
            to override these you need to explicitly specify a value (e.g.
            `health_check_interval=0`)

    redis_kwargsthreadchanneltimeout
connectionNpubsubclose_connection
   )health_check_intervalDEFAULT_REDIS_KWARGSFcheck_intervalfail_when_lockedthread_sleep_timeunavailable_timeoutc	                 D   | | _         d | _        || _        || _        || _        || _        |pt                      | _        | j        	                                D ] \  }	}
| j        
                    |	|
           !t                                          |||           d S )N)r   r%   r&   )r!   r   r   r   r'   r(   dictr   r$   items
setdefaultr   __init__)r   r   r   r   r%   r&   r'   r(   r   keyvaluer   s              r   r-   zRedisLock.__init__L   s     %/$!2#6 (2DFF399;; 	5 	5JC((e4444)- 	 	
 	
 	
 	
 	
r   returnc                 T    | j         st          j        di | j        | _         | j         S )N )r   r   Redisr   r   s    r   get_connectionzRedisLock.get_connectionj   s/     	@$l??T->??DOr   c                 v   |                     d          dk    rd S 	 t          j        |                     d                    }n,# t          $ r t                              d|           Y d S w xY w| j        J | j                            |d         t          t          j	                                         d S )NtypemessagedatazTypeError while parsing: %rresponse_channel)
getjsonloads	TypeErrorloggerdebugr   publishstrtime)r   r8   r9   s      r   channel_handlerzRedisLock.channel_handlerp   s    ;;v)++F	:gkk&1122DD 	 	 	LL6@@@FF	 ***%7 8#dikk:J:JKKKKKs   'A %A.-A.c                     | j          dS )Nz-lock)r   r4   s    r   client_namezRedisLock.client_name}   s    ,%%%%r   c                    t          j        || j        d          }t          j        || j        d          }t          j        || j                  }| j        r
J d            |                                 }|                     ||          }|D ]U}|                    | j	                  d         d         }|r?t                              d|| j	                   |                     || j                  rhd}|s|                    | j                   |                                | _         | j        j        di | j	        | j        i t%          | j        | j                  | _        | j                                         |                    | j	                  d         d         }|dk    r| c S |                                  |rt/          j        t.                    Wt/          j        t.                    )Ng        zThis lock is already activer   r   z Found %d lock subscribers for %s)
sleep_timer2   )r   coalescer   r%   r&   r    r5   _timeout_generatorpubsub_numsubr   r?   r@   check_or_kill_lockr(   client_setnamerF   	subscriberD   r   r'   r   startreleaser   AlreadyLocked)r   r   r%   r&   r   timeout_generator_subscriberss           r   acquirezRedisLock.acquire   s    .$,<<
 

 !>!
 

 ;== ====((**
 33G^LL" &	; &	;A$224<@@CAFK $6L   **,  $ "#K  #))$*:;;;(//11%%MMt7K(LMMM0K#5   !!###(66t|DDQGJ!##KKK LLNNN ; .z:::; &z222r   c           	         | j          dt          j                     }|                                }|                    |           |                    | j         t          j        t          |d                               t          | j	        |dz            }| 
                    ||          D ]/}|                    |          r|                                  dS 0|                    d          D ]c}|                    d          | j        k    rCt                               d	|           |                    |                    d
                     dd S )N-ping)r:   r8   r"   )r   Tr    namez$Killing unavailable redis client: %rid)r   randomr    rN   rA   r<   dumpsr*   minr'   rJ   get_messagecloseclient_listr;   rF   r?   warningclient_kill_filter)r   r   r   r:   r    r%   rS   client_s           r   rL   zRedisLock.check_or_kill_lock   so   "l>>V]__>>""$$)***LJ%5"   	
 	
 	
 T3Wr\BB((
 
 	 	A !!.!99 tt "--h77 	A 	AG{{6""d&666EwOOO--gkk$.?.?@@@tr   c                 >   | j         rM| j                                          | j                                          d | _         t          j        d           | j        rA| j                            | j                   | j                                         d | _        d S d S )Ng{Gz?)	r   stopjoinrC   sleepr    unsubscriber   r_   r4   s    r   rP   zRedisLock.release   s    ; 	KKDKJt; 	K##DL111KDKKK	 	r   c                 .    |                                   d S r
   )rP   r4   s    r   __del__zRedisLock.__del__   s    r   )NNN) r   r   r   __doc__typingDictrB   Any__annotations__Optionalr   floatr   r3   r    PubSubboolr*   r$   ClassVarDEFAULT_THREAD_SLEEP_TIMEDEFAULT_UNAVAILABLE_TIMEOUTr-   r5   rD   propertyrF   rU   rL   rP   rj   r   r   s   @r   r   r      so        " "H +c6:o....O.////LLLNNN-----1FFOFM*111JN$ K K K&/&+c6:o*FG    59*.1527#<%@59
 

 OFL1
 '	

 .
 !/$/
 !
 #
 ofk2
 
 
 
 
 
<    L L L & & X&
 +/1526	=3 =3'=3 .=3 !/$/	=3
 
=3 =3 =3 =3~  >
 
 
      r   r   )r   r<   loggingr[   rC   rl   redisr    r   r   	getLoggerr   r?   rv   ru   r   LockBaser   r2   r   r   <module>r}      s                       		8	$	$      2   Q Q Q Q Q Q Q Q Q Qr   