U
    lHJe@                     @   s   d dl Z d dlZd dlZd dlZd dlmZmZmZ d dlm	Z	 d dl
mZ eeeZdadd ZG dd dZG d	d
 d
eZdS )    N)config
exceptionsutil)notices)Noticec                   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 Zd ZdZejedddZdd Zdd 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    zSingleAttemptLock.__init__c                 C   sj   | j  \}}|dkr(tj| j||d| j ddt | j t	j
tj| jd t| j jdad S )Nr   )Zlock_requestr   pidlockz{}:{})Z	operation)r   Zcheck_lock_infor   LockHeldErrorr   Zwrite_cacheformatosgetpidr   addr   ZOPERATION_IN_PROGRESS	functoolspartialdelete_cache_keyr   )r   Zlock_pidZcur_lock_holderr	   r	   r
   	__enter__(   s      zSingleAttemptLock.__enter__c                 C   s   | j d d ad S )Nr   )r   r   r   )r   Z	_exc_typeZ
_exc_valueZ
_tracebackr	   r	   r
   __exit__:   s    zSingleAttemptLock.__exit__N)
__name__
__module____qualname____doc__r   UAConfigstrr   r   r   r	   r	   r	   r
   r      s   r   c                       sB   e Zd ZdZdddejeeed fddZ fdd	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(   r)   c                   s    t  j||d || _|| _d S )Nr   )superr   r(   r)   )r   r   r   r(   r)   r   	__class__r	   r
   r   Q   s    zSpinLock.__init__c              
      s   t d| j d}zt   W qW q tjk
r } z<t d|d |j |d7 }|| jkrd|nt	
| j W 5 d }~X Y qX qd S )Nzspin lock starting for %sr   z$SpinLock Attempt %d. %s. Spinning...   )LOGdebugr   r*   r   r   r   msgr)   timesleepr(   )r   Ztrieser+   r	   r
   r   ]   s    
  
zSpinLock.__enter__)r   r    r!   r"   r   r#   r$   intr   r   __classcell__r	   r	   r+   r
   r%   @   s   r%   )r   Zloggingr   r1   Zuaclientr   r   r   Zuaclient.filesr   Zuaclient.files.noticesr   Z	getLoggerZreplace_top_level_logger_namer   r.   r   r   r   r%   r	   r	   r	   r
   <module>   s   (