
    d                     B    d dl Z d dlZd dlmZ d dlmZ  G d de      Zy)    N)logger)ustrc                   ^    e Zd ZdZ ej
                  d      Zd Zd Zd Z	d Z
ed        Zy	)
PeriodicOperationz
    Instances of PeriodicOperation are tasks that are executed only after the given
    time period has elapsed.

    NOTE: the run() method catches any exceptions raised by the operation and logs them as warnings.
    <   )minutesc                 
   | j                   j                  | _        t        |t        j
                        r|nt	        j
                  |      | _        t        j                  j                         | _        d | _	        d | _
        y )N)seconds)	__class____name___name
isinstancedatetime	timedelta_periodutcnow_next_run_time_last_warning_last_warning_time)selfperiods     G/usr/lib/python3/dist-packages/azurelinuxagent/ga/periodic_operation.py__init__zPeriodicOperation.__init__$   s^    ^^,,
!+FH4F4F!GvXM_M_hnMo&//668!"&    c                 .   	 | j                   t        j                  j                         k  rb	 t        j                  d| j
                         | j                          t        j                  j                         | j                  z   | _         y y # t        j                  j                         | j                  z   | _         w xY w# t        $ r}dj                  | j
                  t        |            }|| j                  k7  sD| j                  8t        j                  j                         | j                  | j                  z   k\  rDt        j                  |       t        j                  j                         | _        || _        Y d }~y Y d }~y d }~ww xY w)NzExecuting {0}...zKError in {0}: {1} --- [NOTE: Will not log the same error for the next hour])r   r   r   r   verboser   
_operationr   	Exceptionformatr   r   r   _LOG_WARNING_PERIODwarn)r   ewarnings      r   runzPeriodicOperation.run+   sT   	-""h&7&7&>&>&@@TNN#5tzzBOO%*2*;*;*B*B*Dt||*SD' A
 +3*;*;*B*B*Dt||*SD' 	-cjjkokukuw{|}w~G$,,,0G0G0OS[SdSdSkSkSmqu  rI  rI  LP  Ld  Ld  rd  TdG$*2*;*;*B*B*D'%,"" Td	-s/   +C 0B 0C 2CC 	FB7FFc                     | j                   S )N)r   r   s    r   next_run_timezPeriodicOperation.next_run_time:   s    """r   c                     t               )zn
        Derived classes must override this with the definition of the operation they need to perform
        )NotImplementedErrorr&   s    r   r   zPeriodicOperation._operation=   s     "##r   c                 8   t        | D cg c]  }|j                          c}      }|t        j                  j                         z
  }|j                  dz  dz  |j
                  z   dz  |j                  z   dz  }|dkD  rt        j                  |       yyc c}w )z
        Takes a list of operations, finds the operation that should be executed next (that with the closest next_run_time)
        and sleeps until it is time to execute that operation.
           i  g    .Ar   N)	minr'   r   r   daysr
   microsecondstimesleep)
operationsopnext_operation_timesleep_timedeltasleep_secondss        r   sleep_until_next_operationz,PeriodicOperation.sleep_until_next_operationC   s     "
"K"2#3#3#5"KL-0A0A0H0H0JJ)..3d:_=T=TTXaads  eA  eA  A  EN  N1JJ}%  #Ls   BN)r   
__module____qualname____doc__r   r   r    r   r$   r'   r   staticmethodr6    r   r   r   r      sD     -(,,R8'-#$ & &r   r   )r   r/   azurelinuxagent.commonr   azurelinuxagent.common.futurer   objectr   r;   r   r   <module>r?      s   $   ) .7& 7&r   