
    g	                    l    S r SSKJr  SSKrSSKJr  SSKrSSKJr  SSK	J
r
  SS	KJr  \r " S
 S5      rg)The QueueManager class.    )annotationsN)	Coroutine)HomeAssistant   )HacsExecutionStillInProgress   )LOGGERc                  l    \ rS rSrSrSS jr\SS j5       r\SS j5       rSS jr	SS jr
SSS	 jjrS
rg)QueueManager   r   c                ,    Xl         / U l        SU l        g )NFhassqueuerunning)selfr   s     5/config/custom_components/hacs/utils/queue_manager.py__init__QueueManager.__init__   s    	&(
    c                ,    [        U R                  5      $ )-Return a count of pending tasks in the queue.)lenr   r   s    r   pending_tasksQueueManager.pending_tasks   s     4::r   c                     U R                   S:g  $ )r   r   )r   r   s    r   has_pending_tasksQueueManager.has_pending_tasks   s     !!Q&&r   c                    / U l         g)zClear the queue.N)r   r   s    r   clearQueueManager.clear#   s	    
r   c                :    U R                   R                  U5        g)zAdd a task to the queue.N)r   append)r   tasks     r   addQueueManager.add'   s    

$r   Nc                  #    U R                   (       a  [        R                  S5        [        e[	        U R
                  5      S:X  a  [        R                  S5        gSU l         [        R                  S5        / nU(       a(  U R
                  SU  H  nUR                  U5        M     O$U R
                   H  nUR                  U5        M     [        R                  S[	        U5      5        [        R                  " 5       n[        R                  " USS06I Sh  vN nU H0  n[        U[        5      (       d  M  [        R                  S	U5        M2     [        R                  " 5       U-
  nU H  nU R
                  R                  U5        M      [        R                  S
[	        U5      U5        U R                  (       a)  [        R                  S[	        U R
                  5      5        SU l         g N7f)zExecute the tasks in the queue.z+<QueueManager> Execution is already runningr   z!<QueueManager> The queue is emptyNTz,<QueueManager> Checking out tasks to executez4<QueueManager> Starting queue execution for %s tasksreturn_exceptionsz<QueueManager> %szM<QueueManager> Queue execution finished for %s tasks finished in %.2f secondsz.<QueueManager> %s tasks remaining in the queueF)r   _LOGGERdebugr   r   r   r%   timeasynciogather
isinstance	Exceptionerrorremover   )r   number_of_taskslocal_queuer&   startresultentryends           r   executeQueueManager.execute+   sw    <<MMGH..tzz?aMM=>DE

#3O4""4( 5 

""4( # 	LcR]N^_		~~{KdKKE%++159  iikE!DJJd#   	[	

 !!MMJCPTPZPZO\! Ls   DG7G5G7=B9G7r   )r   r   returnNone)r<   int)r<   bool)r<   r=   )r&   r   r<   r=   )N)r4   z
int | Noner<   r=   )__name__
__module____qualname____firstlineno____doc__r   propertyr   r   r"   r'   r:   __static_attributes__ r   r   r   r      sF    !
   ' ' ' 'r   r   )rD   
__future__r   r.   collections.abcr   r-   homeassistant.corer   
exceptionsr   loggerr
   r+   r   rG   r   r   <module>rM      s.     "  %  , 5 
A Ar   