
    ^h                         S SK r S SKrS SKJr  S SKJr  S SKJr  S SKJ	r	J
r
   " S S\ R                  5      r " S S	\ R                  5      r " S
 S\5      r " S S5      rg)    N)Optional)	BaseModel)print_verbose)	DualCache
RedisCachec                       \ rS rSrSrSrSrg)SchedulerCacheKeys   zscheduler:queue    N)__name__
__module____qualname____firstlineno__queuedefault_in_memory_ttl__static_attributes__r       K/home/james-whalen/.local/lib/python3.13/site-packages/litellm/scheduler.pyr	   r	      s    Er   r	   c                        \ rS rSrSrSrSrSrg)DefaultPriorities   r         r   N)r   r   r   r   HighMediumLowr   r   r   r   r   r      s    DF
Cr   r   c                   4    \ rS rSr% \\S'   \\S'   \\S'   Srg)FlowItem   priority
request_id
model_namer   N)r   r   r   r   int__annotations__strr   r   r   r   r   r      s    MOOr   r   c                       \ rS rSr% \\S'     SS\\   S\\   4S jjr	S\
4S jrS	\S
\S\S\4S jrS	\S
\S\S\4S jrS rS
\S\4S jrS\S
\SS4S jrSrg)	Scheduler   cacheNpolling_intervalredis_cachec                     / U l         SnUb  [        R                  R                  n[	        X#S9U l        U=(       d    SU l        g)zO
polling_interval: float or null - frequency of polling queue. Default is 3ms.
N)r,   r   gQ?)r   r	   r   valuer   r*   r+   )selfr+   r,   r   s       r   __init__Scheduler.__init__   sG     
15"$6$L$L$R$R!#

 !1 8Dr   requestc                    #    U R                  UR                  S9I S h  vN n[        R                  " X!R                  UR
                  45        U R                  X!R                  S9I S h  vN   g  NR N7f)Nr#   )r   r#   )	get_queuer#   heapqheappushr!   r"   
save_queue)r/   r2   r   s      r   add_requestScheduler.add_request1   se      nn0B0BnCCu//1C1CDE ooE6H6HoIII D
 	Js"   A6A2AA6,A4-A64A6idr#   health_deploymentsreturnc                 R  #    U R                  US9I Sh  vN nU(       d  [        SR                  U5      5      e[        S[	        U5       35        [	        U5      S:X  aC  [        SU SU 35        US   S   U:X  a%  [
        R                  " U5        [        S	U 35        g
gg
 N7f)z
Return if request can be processed.

Returns:
- True:
    * If healthy deployments are available
    * OR If request at the top of queue
- False:
    * If no healthy deployments available
    * AND request not at the top of queue
r4   N-Incorrectly setup. Queue is invalid. Queue={}zlen(health_deployments): r   zqueue: z, seeking id=   zPopped id: TF)r5   	Exceptionformatr   lenr6   heappopr/   r;   r#   r<   r   s        r   pollScheduler.poll;   s      nn
n;;?FFuM  	1#6H2I1JKL!"a'GE7-t<=Qx{b e$B401- <s   B'B%BB'c                    #    U R                  US9I Sh  vN nU(       d  [        SR                  U5      5      eUS   S   U:X  a  gg N37f)zKReturn if the id is at the top of the queue. Don't pop the value from heap.r4   Nr?   r   r@   TF)r5   rA   rB   rE   s        r   peekScheduler.peek_   sS     nn
n;;?FFuM  8A;" <s   AA	4Ac                     U R                   $ )z$Get the status of items in the queue)r   )r/   s    r   get_queue_statusScheduler.get_queue_statusq   s    zzr   c                 8  #    U R                   b|  SR                  [        R                  R                  U5      nU R                   R                  US9I Sh  vN nUb  [        U[        5      (       d  / $ [        U[        5      (       a  U$ U R                  $  NA7f)z.
Return a queue for that specific model group
N{}:{})key)r*   rB   r	   r   r.   async_get_cache
isinstancelist)r/   r#   
_cache_keyresponses       r   r5   Scheduler.get_queueu   s      ::! (:(@(@(F(F
SJ!ZZ77J7GGHz(D'A'A	Hd++zz Hs   ABBABr   c                    #    U R                   bK  SR                  [        R                  R                  U5      nU R                   R                  X1S9I Sh  vN   g N7f)z+
Save the updated queue of the model group
NrO   )rP   r.   )r*   rB   r	   r   r.   async_set_cache)r/   r   r#   rT   s       r   r8   Scheduler.save_queue   sR      ::! (:(@(@(F(F
SJ**,,,III Js   AAAA)r*   r+   r   )NN)r   r   r   r   r   r%   r   floatr   r0   r   r9   r&   rS   boolrF   rI   rL   r5   r8   r   r   r   r   r(   r(      s     -1,09"5/9 j)9$J J"S "c "t "PT "HS c t PT $# $ d   r   r(   )enumr6   typingr   pydanticr   litellmr   litellm.caching.cachingr   r   Enumr	   r   r   r(   r   r   r   <module>rb      sO        ! 9 
		 y m mr   