HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //usr/lib/python3/dist-packages/uaclient/__pycache__/lock.cpython-310.pyc
o

nHJe@�@s�ddlZddlZddlZddlZddlmZmZmZddlm	Z	ddl
mZe�e�
e��Zdadd�ZGdd�d�ZGd	d
�d
e�ZdS)�N)�config�
exceptions�util)�notices)�NoticecCstrt�dSdS�N)�clear_lock_file�r	r	�//usr/lib/python3/dist-packages/uaclient/lock.py�clear_lock_file_if_presents
�rc@s4eZdZdZdejdefdd�Zdd�Zdd	�Z	d
S)�SingleAttemptLocka�
    Context manager for gaining exclusive access to the lock file.
    Create a lock file if absent. The lock file will contain a pid of the
    running process, and a customer-visible description of the lock holder.

    :param lock_holder: String with the service name or command which is
        holding the lock. This lock_holder string will be customer visible in
        status.json.
    :raises: LockHeldError if lock is held.
    �cfg�lock_holdercGs||_||_dSr�r
r)�selfr
r�_argsr	r	r
�__init__$s
zSingleAttemptLock.__init__cCsj|j��\}}|dkrtj|j||d��|j�dd�t��|j��t	j
tj|jd�t
�|jjd�adS)Nr)�lock_requestr�pid�lockz{}:{})�	operation)r
�check_lock_infor�
LockHeldErrorr�write_cache�format�os�getpidr�addr�OPERATION_IN_PROGRESS�	functools�partial�delete_cache_keyr)r�lock_pid�cur_lock_holderr	r	r
�	__enter__(s���zSingleAttemptLock.__enter__cCs|j�d�dadS)Nr)r
r!r)r�	_exc_type�
_exc_value�
_tracebackr	r	r
�__exit__:szSingleAttemptLock.__exit__N)
�__name__�
__module__�__qualname__�__doc__r�UAConfig�strrr$r(r	r	r	r
rs
rc	sHeZdZdZddd�dejdededef�fd	d
�Z�fdd�Z	�Z
S)
�SpinLocka�
    Context manager for gaining exclusive access to the lock file. In contrast
    to the SingleAttemptLock, the SpinLock will try several times to acquire
    the lock before giving up. The number of times to try and how long to sleep
    in between tries is configurable.

    :param lock_holder: String with the service name or command which is
        holding the lock. This lock_holder string will be customer visible in
        status.json.
    :param sleep_time: Number of seconds to sleep before retrying if the lock
        is already held.
    :param max_retries: Maximum number of times to try to grab the lock before
        giving up and raising a LockHeldError.
    :raises: LockHeldError if lock is held after (sleep_time * max_retries)
    �
�)�
sleep_time�max_retriesr
rr2r3cs t�j||d�||_||_dS)Nr)�superrr2r3)rr
rr2r3r��	__class__r	r
rQs
zSpinLock.__init__c
s�t�d|j�d}	zt���WdStjy@}z!t�d|d|j�|d7}||jkr0|�t	�
|j�WYd}~nd}~wwq
)Nzspin lock starting for %srTz$SpinLock Attempt %d. %s. Spinning...�)�LOG�debugrr4r$rr�msgr3�time�sleepr2)r�tries�er5r	r
r$]s"
�
���zSpinLock.__enter__)r)r*r+r,rr-r.�intrr$�
__classcell__r	r	r5r
r/@s�����r/)r�loggingrr;�uaclientrrr�uaclient.filesr�uaclient.files.noticesr�	getLogger�replace_top_level_logger_namer)r8rrrr/r	r	r	r
�<module>s(