| Current Path : /usr/lib/python3/dist-packages/uaclient/__pycache__/ |
| Current File : //usr/lib/python3/dist-packages/uaclient/__pycache__/lock.cpython-38.pyc |
U
8�-dT � @ s� d dl Z d dlZd dlZd dlZd dlmZmZ d dlmZ d dl m
Z
e�d�Zda
dd� ZG dd � d �ZG d
d� de�ZdS )� N)�config�
exceptions)�notices)�Noticezpro.lockc C s t r
t � d S �N)�clear_lock_file� r r �//usr/lib/python3/dist-packages/uaclient/lock.py�clear_lock_file_if_present s r
c @ s2 e Zd ZdZejed�dd�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 )�selfr
r �_argsr r r �__init__$ s zSingleAttemptLock.__init__c C sj | j �� \}}|dkr(tj| j||d��| j �dd�t�� | j�� t j
tj| jd� t
�| j jd�ad S )Nr )Zlock_requestr �pid�lockz{}:{})Z operation)r
Zcheck_lock_infor �
LockHeldErrorr Zwrite_cache�format�os�getpidr �addr ZOPERATION_IN_PROGRESS� functools�partial�delete_cache_keyr )r Zlock_pidZcur_lock_holderr r r � __enter__( s � ��zSingleAttemptLock.__enter__c C s | j �d� d ad S )Nr )r
r r )r Z _exc_typeZ
_exc_valueZ
_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 sB e Zd ZdZddd�ejeeed�� fdd�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_retries)r
r r'