o >f@sddlZddlZddlZddlmZddlmZmZmZddl m Z m Z m Z ddl mZddlmZmZddlmZddlmZeeeZGd d d e Zeeed d d ejZdeeeffddZddZ GdddZ!dS)N)Tuple) exceptionssystemutil) DataObjectFieldStringDataValue)notices)DataObjectFileDataObjectFileFormat)UAFile)Noticec@s2eZdZedeedegZdedefddZdS)LockDatalock_pid lock_holdercCs||_||_dSNrr)selfrrr//usr/lib/python3/dist-packages/uaclient/lock.py__init__s zLockData.__init__N)__name__ __module__ __qualname__rrfieldsstrrrrrrrs rlockF)privatereturncCszt}Wntjytjtjdwd}|s|S|j}|j}zt d|gt ||fWStj y\t sKtd||t ||fYStd||ttj|YSw)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_lockrrrrrcheck_lock_info"s:  r2cCs tdSr)r#deleterrrrclear_lock_file_if_presentLs r4c@sFeZdZdZddddededefdd Zd d Zd d ZddZ dS) 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_retriesrr8r9cGs||_||_||_dSr)rr8r9)rrr8r9_argsrrrres zRetryLock.__init__cCsVt\}}|dkrtj|j||dtttt |jdt j t j |jddS)Nr) lock_requestrpidr) operation)r2r LockHeldErrorrr#writerrosgetpidr addr OPERATION_IN_PROGRESS)rrcur_lock_holderrrr grab_lockps  zRetryLock.grab_lockc Cstd|jd} z|WdStjy?}z!td|d|j|d7}||jkr/|t |j WYd}~nd}~wwq )Nzspin lock starting for %srTz%RetryLock Attempt %d. %s. Spinning...) r,r-rrErr>msgr9timesleepr8)rtrieserrr __enter__s"  zRetryLock.__enter__cCstttjdSr)r#r3r remover rC)r _exc_type _exc_value _tracebackrrr__exit__szRetryLock.__exit__N) rrr__doc__rr)rrErLrQrrrrr5Ps   r5)"loggingr@rHtypingruaclientrrruaclient.data_typesrrruaclient.filesr uaclient.files.data_typesr r uaclient.files.filesr uaclient.files.noticesr getLoggerreplace_top_level_logger_namerr,rJSONr#r)rr2r4r5rrrrs(      *