Ë
    íéñi¡$  ã                  óì   — d dl mZ 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Zddl	m
Z
mZ  ej                  e«      ZdZdZ G d„ dej"                  j$                  «      Z G d„ d	ej&                  «      Zy)
é    )ÚannotationsNé   )Ú
exceptionsÚutilsgš™™™™™¹?c                  ó    ‡ — e Zd Zdˆ fd„Zˆ xZS )ÚPubSubWorkerThreadc                óh   •— 	 t         ‰| �  «        y # t        $ r t        j                  «        ‚ w xY w©N)ÚsuperÚrunÚ	ExceptionÚ_threadÚinterrupt_main)ÚselfÚ	__class__s    €úS/var/www/pod-logistic/pod-ai/venv/lib/python3.12/site-packages/portalocker/redis.pyr   zPubSubWorkerThread.run   s/   ø€ ð	Ü‰G‰K�MøÜò 	Ü×"Ñ"Ô$Øð	ús   ƒ ’1©ÚreturnÚNone)Ú__name__Ú
__module__Ú__qualname__r   Ú__classcell__©r   s   @r   r   r      s   ø„ ÷ñ ó    r   c                  ó8  ‡ — e Zd ZU dZded<   ded<   ded<   ded	<   d
ed<   dZded<   ded<    edd¬«      Zded<   ddddee	df	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d ˆ fd„Z
d!d„Zd"d„Zed#d„«       Z	 	 	 	 	 	 d$d„Z	 	 	 d%	 	 	 	 	 	 	 d&d„Z	 	 	 	 	 	 d'd„Zd(d„Zd(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`)

    zdict[str, typing.Any]Úredis_kwargszPubSubWorkerThread | NoneÚthreadÚstrÚchannelÚfloatÚtimeoutúredis.client.Redis[str] | NoneÚ
connectionNzredis.client.PubSub | NoneÚpubsubÚboolÚclose_connectioné
   T)Úhealth_check_intervalÚdecode_responsesz&typing.ClassVar[dict[str, typing.Any]]ÚDEFAULT_REDIS_KWARGSFc	                ó"  •— | | _         d | _        || _        || _        || _        || _        |xs
 t        «       | _        | j                  j                  «       D ]!  \  }	}
| j                  j                  |	|
«       Œ# t        ‰| �1  |||¬«       y )N)r#   Úcheck_intervalÚfail_when_locked)r(   r   r!   r%   Úthread_sleep_timeÚunavailable_timeoutÚdictr   r,   ÚitemsÚ
setdefaultr   Ú__init__)r   r!   r%   r#   r.   r/   r0   r1   r   ÚkeyÚvaluer   s              €r   r5   zRedisLock.__init__P   s˜   ø€ ð %/ ˆÔàˆŒØˆŒØ$ˆŒØ!2ˆÔØ#6ˆÔ Ø(Ò2¬D«FˆÔà×3Ñ3×9Ñ9Ó;ò 	5‰JˆC�Ø×Ñ×(Ñ(¨¨eÕ4ð	5ô 	‰ÑØØ)Ø-ð 	õ 	
r   c                óŽ   — | j                   s.t        j                  j                  di | j                  ¤Ž| _         | j                   S )N© )r%   ÚredisÚclientÚRedisr   ©r   s    r   Úget_connectionzRedisLock.get_connectionn   s2   € Ø�ŠÜ#Ÿl™l×0Ñ0ÑE°4×3DÑ3DÑEˆDŒOà�‰Ðr   c                ó\  — |j                  d«      dk7  ry |j                  d«      }|sy 	 t        j                  |«      }| j                  €J ‚| j                  j                  |d   t        t        j                  «       «      «       y # t        $ r t        j                  d|«       Y y w xY w)NÚtypeÚmessageÚdatazTypeError while parsing: %rÚresponse_channel)
ÚgetÚjsonÚloadsÚ	TypeErrorÚloggerÚdebugr%   Úpublishr    Útime)r   rA   Úraw_datarB   s       r   Úchannel_handlerzRedisLock.channel_handlert   s—   € Ø�;‰;�vÓ )Ò+Øà—;‘;˜vÓ&ˆÙØð	Ü—:‘:˜hÓ'ˆDð
 �‰Ð*Ð*Ð*Ø�‰×Ñ Ð%7Ñ 8¼#¼d¿i¹i»kÓ:JÕKøô ò 	Ü�L‰LÐ6¸Ô@Ùð	ús   «B	 Â	B+Â*B+c                ó    — | j                   › d�S )Nz-lock)r!   r=   s    r   Úclient_namezRedisLock.client_name…   s   € à—,‘,�˜uÐ%Ð%r   c              #  ó\  K  — |€d}|€| j                   }t        j                  «       |z   }d}|st        j                  «       |k  rdd}|dkD  r|n| j                   }|dt        j                  «       z   z  }t        j                  |«       d–— |rŒKt        j                  «       |k  rŒcy y ­w)Nç        TFr   g      à?)r0   rK   Ú	monotonicÚrandomÚsleep)r   r#   r.   ÚdeadlineÚfirstÚeffective_intervalÚ
sleep_times          r   Ú_timeout_generatorzRedisLock._timeout_generator‰   s§   è ø€ ð ˆ?ØˆGØÐ!Ø!×3Ñ3ˆNÜ—>‘>Ó# gÑ-ˆØˆÙ”t—~‘~Ó'¨(Ò2ØˆEð " AÒ%ñ à×+Ñ+ð ð
 ,¨s´V·]±]³_Ñ/DÑEˆJÜ�J‰J�zÔ"ØŠGò ”t—~‘~Ó'¨(Õ2ùs   ‚BB,ÂB,Â*B,c                óB  — t        j                  || j                  d«      }t        j                  || j                  d«      }t        j                  || j                  «      }| j
                  rJ d«       ‚| j                  «       }| j                  ||«      }|D �]o  }|j                  | j                  «      d   d   }|r@t        j                  d|| j                  «       | j                  || j                  «      rŒed}|sñ|j                  | j                  «       |j                  «       | _         | j
                  j                   di | j                  | j"                  i¤Ž t%        | j
                  | j&                  ¬«      | _        | j(                  j+                  «        t-        j.                  d«       |j                  | j                  «      d   d   }|dk(  r| c S | j1                  «        |s�Œ^t3        j4                  «       ‚ t3        j4                  «       ‚)	NrQ   zThis lock is already activer   r   z Found %d lock subscribers for %s)rX   ç{®Gáz„?r9   )r   Úcoalescer#   r.   r/   r&   r>   rY   Úpubsub_numsubr!   rH   rI   Úcheck_or_kill_lockr1   Úclient_setnamerO   Ú	subscriberM   r   r0   r   ÚstartrK   rT   Úreleaser   ÚAlreadyLocked)r   r#   r.   r/   r%   Útimeout_generatorÚ_Úsubscriberss           r   ÚacquirezRedisLock.acquire�   sÜ  € ô —.‘. ¨$¯,©,¸Ó<ˆÜŸ™ØØ×ÑØó
ˆô
 !Ÿ>™>ØØ×!Ñ!ó
Ðð
 —;’;Ð=Ð =Ó=ˆØ×(Ñ(Ó*ˆ
à ×3Ñ3°G¸^ÓLÐØ"ó &	1ˆAØ$×2Ñ2°4·<±<Ó@ÀÑCÀAÑFˆKáÜ—‘Ø6ØØ—L‘Lôð ×*Ñ*ØØ×,Ñ,ôð à"#�Kñ Ø×)Ñ)¨$×*:Ñ*:Ô;Ø(×/Ñ/Ó1�”Ø%�—‘×%Ñ%ÑM¨¯©°t×7KÑ7KÐ(LÒMÜ0Ø—K‘KØ#×5Ñ5ô�”ð —‘×!Ñ!Ô#Ü—
‘
˜4Ô Ø(×6Ñ6°t·|±|ÓDÀQÑGÈÑJ�Ø !Ò#Ø’Kð —L‘L”NãÜ ×.Ñ.Ó0Ð0ðM&	1ôP ×&Ñ&Ó(Ð(r   c           	     ó~  — | j                   › dt        j                  «       › �}|j                  «       }|j                  |«       |j	                  | j                   t        j                  t        |d¬«      «      «       t        | j                  |dz  «      }| j                  ||«      D ]&  }|j                  |¬«      sŒ|j                  «         y |j                  d«      D ]W  }|j                  d«      | j                  k(  sŒ"t         j#                  d	|«       |j%                  |j                  d
«      «       ŒY y )Nú-Úping)rC   rA   r)   )r#   Tr&   Únamez$Killing unavailable redis client: %rÚid)r!   rS   r&   r`   rJ   rE   Údumpsr2   Úminr0   rY   Úget_messageÚcloseÚclient_listrD   rO   rH   ÚwarningÚclient_kill_filter)r   r%   r#   rC   r&   r.   re   Úclient_s           r   r^   zRedisLock.check_or_kill_lockÜ   s$  € ð #Ÿl™l˜^¨1¬V¯]©]«_Ð,=Ð>Ðà×"Ñ"Ó$ˆØ×ÑÐ)Ô*Ø×ÑØ�L‰LÜ�J‰JÜØ%5Ø"ôóô	
ô ˜T×3Ñ3°W¸r±\ÓBˆØ×(Ñ(ØØó
ò 	ˆAð ×!Ñ!¨.Ð!Õ9Ø—‘”Ùð	ð "×-Ñ-¨hÓ7ò 	ˆGØ�{‰{˜6Ó" d×&6Ñ&6Ó6Ü—‘ÐEÀwÔOØ×-Ñ-Ø—K‘K Ó%õð	ð r   c                ób  — | j                   rP| j                   j                  «        | j                   j                  «        d | _         t        j                  d«       | j
                  rG| j
                  j                  | j                  «       | j
                  j                  «        d | _        y y )Nr[   )	r   ÚstopÚjoinrK   rT   r&   Úunsubscriber!   rp   r=   s    r   rb   zRedisLock.release  su   € Ø�;Š;Ø�K‰K×ÑÔØ�K‰K×ÑÔØˆDŒKÜ�J‰J�tÔà�;Š;Ø�K‰K×#Ñ# D§L¡LÔ1Ø�K‰K×ÑÔØˆD�Kð r   c                ó$   — | j                  «        y r
   )rb   r=   s    r   Ú__del__zRedisLock.__del__  s   € Ø�‰�r   )r!   r    r%   r$   r#   úfloat | Noner.   r{   r/   úbool | Noner0   r"   r1   r"   r   zdict[str, typing.Any] | Noner   r   )r   úredis.client.Redis[str])rA   zdict[str, str]r   r   )r   r    )r#   r{   r.   r{   r   ztyping.Iterator[int])NNN)r#   r{   r.   r{   r/   r|   r   r   )r%   r}   r#   r"   r   r|   r   )r   r   r   Ú__doc__Ú__annotations__r&   r2   r,   ÚDEFAULT_THREAD_SLEEP_TIMEÚDEFAULT_UNAVAILABLE_TIMEOUTr5   r>   rM   ÚpropertyrO   rY   rg   r^   rb   rz   r   r   s   @r   r   r      se  ø… ñ"ðH (Ó'Ø%Ó%ØƒLØƒNØ.Ó.Ø)-€FÐ&Ó-ØÓáCGØ ØôDÐÐ@ó ð 6:Ø $Ø'+Ø(-Ø#<Ø%@Ø59ð
àð
ð 3ð
ð ð	
ð
 %ð
ð &ð
ð !ð
ð #ð
ð 3ð
ð 
õ
ó<óLð" ò&ó ð&ðØ#ðØ5Aðà	óð, !%Ø'+Ø(,ð	=)àð=)ð %ð=)ð &ð	=)ð
 
ó=)ð~#à+ð#ð ð#ð 
ó	#óJ
÷r   r   )Ú
__future__r   r   rE   ÚloggingrS   rK   Útypingr:   Ú r   r   Ú	getLoggerr   rH   r�   r€   r;   r   ÚLockBaser   r9   r   r   ú<module>r‰      sf   ðå "ã Û Û Û Û Û ã ç à	ˆ×	Ñ	˜8Ó	$€àÐ ØÐ ô˜Ÿ™×8Ñ8ô ôp�—‘õ pr   