File: //usr/lib/python3/dist-packages/uaclient/__pycache__/lock.cpython-310.pyc
o
nHJe@ � @ s� d dl Z d dlZd dlZd dlZd dlmZmZmZ d dlm Z d dl
mZ e�e�
e��Zdadd� ZG dd� d�ZG d d
� d
e�ZdS )� N)�config�
exceptions�util)�notices)�Noticec C s t rt � d S d S �N)�clear_lock_file� r r �//usr/lib/python3/dist-packages/uaclient/lock.py�clear_lock_file_if_present s
�r c @ s4 e Zd ZdZdejdefdd�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_holderc G s || _ || _d S r �r
r )�selfr
r �_argsr r r
�__init__$ s
zSingleAttemptLock.__init__c C sj | j �� \}}|dkrtj| j||d��| j �dd�t�� | j�� t j
tj| jd� t
�| j jd�ad S )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__c C s | j �d� d ad S )Nr )r
r! r )r � _exc_type�
_exc_value�
_tracebackr r r
�__exit__: s zSingleAttemptLock.__exit__N)
�__name__�
__module__�__qualname__�__doc__r �UAConfig�strr r$ r( r r r r
r s
r c sH e Zd ZdZddd�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
r r2 r3 c s t � j||d� || _|| _d S )Nr )�superr r2 r3 )r r
r r2 r3 r �� __class__r r
r Q s
zSpinLock.__init__c
s� t �d| j� d} zt� �� W d S tjy@ } z!t �d|d |j� |d7 }|| jkr0|�t �
| j� W Y d }~nd }~ww q
)Nzspin lock starting for %sr Tz$SpinLock Attempt %d. %s. Spinning...� )�LOG�debugr r4 r$ r r �msgr3 �time�sleepr2 )r �tries�er5 r r
r$ ] s"