
    1i'                     f    d dl Z d dlZd dlmZmZmZ  G d d      Zde j                  defdZd Z	y)	    N)AnyCallable	Coroutinec            	           e Zd ZdZd Zd Zd ZdedefdZ	dedefd	Z
ded
edeeeef   f   fdZdej                  dedefdZdej                  dedefdZdej                  dedefdZy)BackgroundSchedulerzf
    Schedules background tasks execution either in separate thread or in the running event loop.
    c                 `    d | _         g | _        t        j                         | _        d| _        y )NF)_next_timer_event_loops	threadingLock_lock_stoppedselfs    Z/mnt/e/genesis-system/.venvs/voice-bridge/lib/python3.12/site-packages/redis/background.py__init__zBackgroundScheduler.__init__   s'    ^^%
    c                 $    | j                          y N)stopr   s    r   __del__zBackgroundScheduler.__del__   s    		r   c                    | j                   5  | j                  r
	 ddd       yd| _        | j                  r!| j                  j                          d| _        | j                  D ].  }|j                         s|j                  |j                         0 | j                  j                          ddd       y# 1 sw Y   yxY w)zB
        Stop all scheduled tasks and clean up resources.
        NT)	r   r   r	   cancelr
   
is_runningcall_soon_threadsafer   clear)r   loops     r   r   zBackgroundScheduler.stop   s     ZZ 	&}}	& 	& !DM  '')#'  )) 9??$--dii89 ##%	& 	& 	&s   B8AB887B88Cdelaycallbackc                    | j                   5  | j                  r
	 ddd       y	 ddd       t        j                         }| j                   5  | j                  j                  |       ddd       t        j                  t        || j                  ||g|d      }|j                          y# 1 sw Y   xY w# 1 sw Y   SxY w)zI
        Runs callable task once after certain delay in seconds.
        NTtargetargsdaemon)r   r   asyncionew_event_loopr
   appendr   Thread_start_event_loop_in_thread_call_laterstart)r   r   r   r#   r   threads         r   run_oncezBackgroundScheduler.run_once(   s     ZZ 	}}	 		
 %%'ZZ 	+$$T*	+ !!.((%ADA

 		 		+ 	+   B/B;/B8;Cintervalc                    | j                   5  | j                  r
	 ddd       y	 ddd       t        j                         }| j                   5  | j                  j                  |       ddd       t        j                  t        || j                  ||g|d      }|j                          y# 1 sw Y   xY w# 1 sw Y   SxY w)zN
        Runs recurring callable task with given interval in seconds.
        NTr!   )r   r   r%   r&   r
   r'   r   r(   r)   _call_later_recurringr+   )r   r/   r   r#   r   r,   s         r   run_recurringz!BackgroundScheduler.run_recurring=   s     ZZ 	}}	 		
 %%'ZZ 	+$$T*	+ !!.22HhNN

 		 		+ 	+r.   coro.c                    K    j                   5   j                  r
	 ddd       y	 ddd       t        j                         t	        |g|  fdj                         _        y# 1 sw Y   KxY ww)z
        Runs recurring coroutine with given interval in seconds in the current event loop.
        To be used only from an async context. No additional threads are created.
        Nc                      j                   5  j                  r
	 d d d        y 	 d d d                 j                         _        y # 1 sw Y   (xY wr   r   r   
call_laterr	   )r/   r   r   tickwrappeds   r   r8   z5BackgroundScheduler.run_recurring_async.<locals>.tick`   sP     ==   I#x>D s   AA)r   r   r%   get_running_loop_async_to_sync_wrapperr7   r	   )r   r/   r3   r#   r   r8   r9   s   ``  @@@r   run_recurring_asyncz'BackgroundScheduler.run_recurring_asyncR   s{      ZZ 	}}	 		 '')(t;d;	? 	?  ??8T:#	 	s!   BA6	BA
B6A?;Br   c                     | j                   5  | j                  r
	 d d d        y 	 d d d         |j                  ||g| | _        y # 1 sw Y   #xY wr   r6   )r   r   r   r   r#   s        r   r*   zBackgroundScheduler._call_laterl   sS     ZZ 	}}	 		 +4??5(BTB	 	s   AAc                     | j                   5  | j                  r
	 d d d        y 	 d d d         | j                  ||| j                  |||g|  y # 1 sw Y   ,xY wr   )r   r   r*   _execute_recurringr   r   r/   r   r#   s        r   r1   z)BackgroundScheduler._call_later_recurringt   sd     ZZ 	}}	 		 	(D33T8X	
PT	
	 	s   AAc                 R   | j                   5  | j                  r
	 ddd       y	 ddd       	  ||  | j                   5  | j                  r
	 ddd       y	 ddd        | j                  ||| j                  |||g|  y# 1 sw Y   ^xY w# t        $ r Y fw xY w# 1 sw Y   GxY w)zR
        Executes recurring callable task with given interval in seconds.
        N)r   r   	Exceptionr*   r?   r@   s        r   r?   z&BackgroundScheduler._execute_recurring   s     ZZ 	}}	 			dO
 ZZ 	}}	 		 	(D33T8X	
PT	
	 	  			 	s'   BB BB	BBB&N)__name__
__module____qualname____doc__r   r   r   floatr   r-   r2   r   r   r<   r%   AbstractEventLoopr*   r1   r?    r   r   r   r      s    &(e x *e x *;;%-c9S#s]3K.K%L;4C--C6;CGOC
''
 
 	

''
 
 	
r   r   
event_loopcall_soon_cbc           	         t        j                  |         | j                  || g|  	 | j                          	 t        j                  |       }|D ]  }|j                           | j                  t        j                  |ddi       | j                          y# t        $ r Y w xY w# | j                          w xY w# 	 t        j                  |       }|D ]  }|j                           | j                  t        j                  |ddi       n# t        $ r Y nw xY w| j                          w # | j                          w xY wxY w)z
    Starts event loop in a thread and schedule callback as soon as event loop is ready.
    Used to be able to schedule tasks using loop.call_later.

    :param event_loop:
    :return:
    return_exceptionsTN)
r%   set_event_loop	call_soonrun_forever	all_tasksr   run_until_completegatherrB   close)rJ   rK   r#   pendingtasks        r   r)   r)      s6    :&Jz9D9 	''
3G  ))@4@   		 	''
3G  ))@4@  		 Jsf   C AB 	B)&B, (B))B, ,B>E	ADD4	D D4D  D4#E	4EE	c                       fd}|S )a  
    Wraps an asynchronous function so it can be used with loop.call_later.

    :param loop: The event loop in which the coroutine will be executed.
    :param coro_func: The coroutine function to wrap.
    :param args: Positional arguments to pass to the coroutine function.
    :param kwargs: Keyword arguments to pass to the coroutine function.
    :return: A regular function suitable for loop.call_later.
    c                  @    t        j                    i        y )N)r   )r%   ensure_future)r#   	coro_funckwargsr   s   r   r9   z'_async_to_sync_wrapper.<locals>.wrapped   s    i88tDr   rI   )r   rZ   r#   r[   r9   s   ```` r   r;   r;      s    E Nr   )
r%   r   typingr   r   r   r   rH   r)   r;   rI   r   r   <module>r]      s>      + +V
 V
r))9A<r   