>fFddlZddlZddlZddlmZddlmZmZmZddl m Z m Z m Z ddl mZddlmZmZddlmZddlmZej,ej.eZGd d e Zeeed d ej6ZdeeeffdZdZ GddZ!y)N)Tuple) exceptionssystemutil) DataObjectFieldStringDataValue)notices)DataObjectFileDataObjectFileFormat)UAFile)Noticec@eZdZedeedegZdedefdZy)LockDatalock_pid lock_holderc ||_||_yNrr)selfrrs //usr/lib/python3/dist-packages/uaclient/lock.py__init__zLockData.__init__s  &N)__name__ __module__ __qualname__rr fieldsstrrrrrrs0 j/* m_-F ''3'rrlockF)privatereturncF tj}d}|s|S|j }|j} tjd|gt||fS#tj$r%tjtj wxYw#tj$rytjs&tjd||t||fcYStj!d||tj"tj |cYSwxYw)a Return lock info if lock file is present the lock is active. If process claiming the lock is no longer present, remove the lock file and log a warning. :return: A tuple (pid, string describing lock holder) If no active lock, pid will be -1. )lock_file_path)psz.Found stale lock file previously held by %s:%sz1Removing stale lock file previously held by %s:%s)lock_data_filereadrInvalidFileFormatErrorInvalidLockFilepathrrrsubpintProcessExecutionErrorrwe_are_currently_rootLOGdebugwarningensure_file_absent) lock_data_objno_lockrrs rcheck_lock_infor7"sM&++- G %%H++K T8$%H {++  , ,M((8K8KLLM  + +))+ II@  M;/ / ?   !!."5"56s#A#B8BA D !eZdZdZddddededefdZd Zd Zd Z y ) RetryLockah 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. The RetryLock 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_retriesrr?r@c.||_||_||_yr)rr?r@)rrr?r@_argss rrzRetryLock.__init__es'$&rcTt\}}|dkDr"tj|j||tj t ttj|jtjtj|jy)Nr) lock_requestrpidr) operation)r7r LockHeldErrorrr(writerrosgetpidr addrOPERATION_IN_PROGRESS)rrcur_lock_holders r grab_lockzRetryLock.grab_lockps&5&7#? a<**!--+   c"))+.Dris --BB"J')g:::8DE'z'   65!'sCx'TC5C5r