
    nhhN                       d Z ddlmZ ddlZddlZddlZddlZddlZddlm	Z	m
Z
mZ ddlmZmZ ddlmZ ddlmZ ddlmZmZ dd	lmZ dd
lmZ ddlmZmZmZ ddlmZ ddl m!Z! ddl"m#Z# ddl$m%Z% e	rddl&m'Z'm(Z(m)Z) ddl*m+Z+ ddl,m-Z- dZ.d"dZ/d#dZ0 G d d      Z1 G d de1      Z2 G d de1      Z3 G d de1      Z4 e5       Z6d$dZ7d%dZ8d&d Z9d&d!Z:e.r ejv                  e:       yy)'z9Class to monitor a MongoDB server on a background thread.    )annotationsN)TYPE_CHECKINGAnyOptional)commonperiodic_executor)MovingMinimum)_SrvResolver)NetworkTimeout_OperationCancelled)Hello)_async_create_lock)_SDAM_LOGGER
_debug_log_SDAMStatusMessage)_shutdown_executors)_is_faas)MovingAverage)ServerDescription)AsyncConnectionPool_CancellationContext)TopologySettings)TopologyFc                .    d| _         d| _        d| _        y)z'PYTHON-2433 Clear error traceback info.N)__traceback____context__	__cause__errors    /var/www/html/SchoolMeal/SchoolMeal/pds_admin_SchoolMeal/Backend/venv/lib/python3.12/site-packages/pymongo/asynchronous/monitor.py	_sanitizer"   2   s    EEEO    c                D    t        dt        j                         | z
        S )zReturn the duration since the given start time.

    Accounts for buggy platforms where time.monotonic() is not monotonic.
    See PYTHON-4600.
    g        )maxtime	monotonic)starts    r!   _monotonic_durationr)   9   s     sDNN$u,--r#   c                  <    e Zd ZddZd	dZd	dZd	dZd	dZd	dZy)
MonitorBasec                    dfd}t        j                  ||||      }|| _        ddfd}t        j                  | |j
                        t        j                  ||      | _        t        |        y)zBase class to do periodic work on a background thread.

        The background thread is signaled to stop when the Topology or
        this instance is freed.
        c                 V   K           } | y| j                          d {    y7 w)NFT)_run)monitorself_refs    r!   targetz$MonitorBase.__init__.<locals>.targetL   s-     jG,,.   !s   )'))intervalmin_intervalr1   nameNc                :            }|r|j                          y y Ngc_safe_close)dummyr/   r0   s     r!   _on_topology_gcz-MonitorBase.__init__.<locals>._on_topology_gcY   s      jG%%' r#   )returnboolr6   )r9   zOptional[Topology]r;   None)	r   AsyncPeriodicExecutor	_executorweakrefrefcloseproxy	_topology	_register)	selftopologyr4   r2   r3   r1   executorr:   r0   s	           @r!   __init__zMonitorBase.__init__C   s`    	 %::Ld
 "	( ;;tX^^4 xA$r#   c                8    | j                   j                          y)z[Start monitoring, or restart after a fork.

        Multiple calls have no effect.
        N)r?   openrF   s    r!   rK   zMonitorBase.opene   s    
 	r#   c                8    | j                   j                          y)zGC safe close.N)r?   rB   rL   s    r!   r8   zMonitorBase.gc_safe_closel   s    r#   c                ,   K   | j                          yw)zWClose and stop monitoring.

        open() restarts the monitor after closing.
        Nr7   rL   s    r!   rB   zMonitorBase.closep   s     
 	s   c                T   K   | j                   j                          d{    y7 w)zWait for the monitor to stop.N)r?   joinrL   s    r!   rP   zMonitorBase.joinw   s     nn!!###   (&(c                8    | j                   j                          y)z)If the monitor is sleeping, wake it soon.N)r?   wakerL   s    r!   request_checkzMonitorBase.request_check{   s    r#   N)rG   r   r4   strr2   intr3   floatr;   r=   )	__name__
__module____qualname__rI   rK   r8   rB   rP   rT    r#   r!   r+   r+   B   s!     D$r#   r+   c                       e Zd Z	 	 	 	 	 	 	 	 d fdZddZddZddZddZddZddZ	ddZ
dd	Zdd
ZddZ xZS )Monitorc                   t         |   |d|j                  t        j                         || _        || _        || _        | j                  j                  j                  | _
        | j                  duxr | j                  j                  | _        d| _        d| _        t        |||j!                  |j"                              | _        |j&                  dk(  rd| _        y|j&                  dk(  rd| _        yt+                | _        y)a   Class to monitor a MongoDB server on a background thread.

        Pass an initial ServerDescription, a Topology, a Pool, and
        TopologySettings.

        The Topology is weakly referenced. The Pool must be exclusive to this
        Monitor.
        pymongo_server_monitor_taskNstreamTpollF)superrI   heartbeat_frequencyr   MIN_HEARTBEAT_INTERVAL_server_description_pool	_settings_pool_options_event_listeners
_listenersenabled_for_server_heartbeat_publish_cancel_context_conn_id_RttMonitor_create_pool_for_monitoraddress_rtt_monitorserver_monitoring_mode_streamr   )rF   server_descriptionrG   pooltopology_settings	__class__s        r!   rI   zMonitor.__init__   s     	)11))		
 $6 
*..66GGt3d8d8d?C'+'--.@.H.HI

 33x?DL55? DL'z>DLr#   c                B    | j                   }|r|j                          yy)zCancel any concurrent hello check.

        Note: this is called from a weakref.proxy callback and MUST NOT take
        any locks.
        N)rn   cancel)rF   contexts     r!   cancel_checkzMonitor.cancel_check   s#     && NN	 r#   c                   K   | j                   j                          | j                  j                  r#| j                   j	                          d{    yy7 w)z1Start an _RttMonitor that periodically runs ping.N)rs   rK   r?   _stoppedrB   rL   s    r!   _start_rtt_monitorzMonitor._start_rtt_monitor   sG     
 	 >>""##))+++ #+s   AAAAc                    | j                   j                          | j                  j                          | j	                          y r6   )r?   rB   rs   r8   r}   rL   s    r!   r8   zMonitor.gc_safe_close   s0    '')r#   c                   K   t        j                  | j                  j                         | j                  j                         d       d {    y 7 w)NT)return_exceptions)asynciogatherr?   rP   rs   rL   s    r!   rP   zMonitor.join   s?     nnNN!4#4#4#9#9#;t
 	
 	
s   AAAAc                   K   | j                          | j                  j                          d {    | j                          d {    y 7 7 wr6   )r8   rs   rB   _reset_connectionrL   s    r!   rB   zMonitor.close   sI     %%''' $$&&& 	( 	's!   .AAAA	AAc                T   K   | j                   j                          d {    y 7 wr6   )rg   resetrL   s    r!   r   zMonitor._reset_connection   s     jj   rQ   c                   K   	 | j                   }	 | j                          d {   | _         | j                  j                  | j                   | j                   j                  t        | j                   j                  t                      d {    | j"                  r^| j                   j                  rH| j                   j$                  r2| j'                          d {    | j                  j                          | j                   j                  r&|j                  r| j                  j                          | j                  j                  r#| j                  j                          d {    y y 7 P# t        $ r}t        |       t	        | j                   j
                  |      | _         |j                  r| j                  j                          Y d }~| j                  j                  r$| j                  j                          d {  7   y y d }~ww xY w7 7 B# t(        $ r | j                          d {  7   Y w xY w7 # | j                  j                  r$| j                  j                          d {  7   w w xY ww)Nr   )
reset_poolinterrupt_connections)rf   _check_serverr   r"   r   rr   is_server_type_knownr?   
skip_sleepr   rs   rB   rD   	on_changer    
isinstancer   ru   topology_versionr   ReferenceError)rF   prev_sdexcs      r!   r.   zMonitor._run   s    '	0..G151C1C1E+E( ..**((3399&01I1I1O1OQ_&` +    ||((==,,==--///))+''--'2N2N))+
 ~~&&''--/// 'G ,F& 
#+<,,44C,( // NN--/0 ~~&&''--/// 'E
 0  	**,	
 0 ~~&&''--/// 's   JH$ E7 E4	E7 A!H$ HAH$ H! AH$ :3J-I.J4E7 7	H AHH$ 3JHJHH$ !H$ $I
 II
I 	I

I J4JJJJc                x  K   d| _         t        j                         }	 | j                          d{   S 7 # t        $ r  t
        $ rk}t        |       | j                  }|j                  }t        |      }t        | j                  xr |j                  xr |j                        }| j                  r,| j                  J | j                  j!                  ||||       t#        j$                  t&        j(                        rMt+        t"        t,        j.                  | j0                  j2                  |d   |d   ||dz  || j                   	       | j5                          d{  7   t7        |t8              r | j:                  j=                          d{  7   t?        ||      cY d}~S d}~ww xY ww)z^Call hello or read the next streaming response.

        Returns a ServerDescription.
        Nr        )message
topologyId
serverHost
serverPortawaited
durationMSfailuredriverConnectionIdr   ) ro   r&   r'   _check_oncer   	Exceptionr"   rf   rr   r)   r<   ru   r   r   rm   rk   publish_server_heartbeat_failedr   isEnabledForloggingDEBUGr   r   HEARTBEAT_FAILrD   _topology_idr   r   r   rs   r   r   )rF   r(   r    sdrr   durationr   s          r!   r   zMonitor._check_server   sf    
  	;))++++ 	 	;e))BjjG*51H4<<[B,C,C[H[H[\G}}222??SXZab((7 .==#~~::&qz&qz#'$!'+}}
 ((***%!45##))+++$WE::5	;sV   F:9 79 F:9 F7DF2&E)'3F2FF2,F7-F:2F77F:c                2  K   | j                   j                  }| j                   }t        | j                  j                  xr( | j
                  xr |j                  xr |j                        }| j                  r*| j                  J | j                  j                  ||       | j                  r.| j                  j                  r| j                          d{    | j                  j                         4 d{   }t        j                   t"        j$                        rSt'        t        t(        j*                  | j,                  j.                  |j0                  |j2                  |d   |d   |       |j4                  | _        |j0                  | _        | j9                  |       d{   \  }}|j:                  s#| j<                  j?                  |       d{    | j<                  jA                          d{   \  }}tC        ||||      }| j                  r6| j                  J | j                  jE                  ||||j:                         t        j                   t"        j$                        rbt'        t        t(        jF                  | j,                  j.                  |j0                  |j2                  |d   |d   ||dz  |jH                  
       |cddd      d{    S 7 7 7 K7 7 7 # 1 d{  7  sw Y   yxY ww)zfA single attempt to call hello.

        Returns a ServerDescription, or raises an exception.
        Nr   r   )r   r   r   serverConnectionIdr   r   r   )min_round_trip_timer   )	r   r   r   r   r   r   r   r   reply)%rf   rr   r<   rg   connsru   r   r   rm   rk    publish_server_heartbeat_startedrn   	cancelledr   checkoutr   r   r   r   r   r   HEARTBEAT_STARTrD   r   idserver_connection_idcancel_contextro   _check_with_socket	awaitablers   
add_samplegetr   "publish_server_heartbeat_succeededHEARTBEAT_SUCCESSdocument)	rF   rr   r   r   connresponseround_trip_timeavg_rttmin_rtts	            r!   r   zMonitor._check_once%  s    
 **22%% JJaa"2I2IabNaNa
 ==??...OO<<WgND$8$8$B$B((***::&&( (	 (	D((7 .>>#~~::'+ww'+'@'@&qz&qz#	 $(#6#6D  GGDM.2.E.Ed.K(K%Ho%%''22?CCC%)%6%6%:%:%<<GW"7HgSZ[B}}222BB_h8J8J ((7 .@@#~~::'+ww'+'@'@&qz&qz#.5"++ Q(	 (	 (	 +(	  )LC<)(	 (	 (	 (	s   CLK2"L<K5=L B-L-K8.1LK; !LK>CL L,L -L5L8L;L>L LLL	LLc                  K   t        j                         }|j                  r$t        |j	                          d{   d      }n| j
                  re|j                  rY| j                  j                  rC|j                  | j                  j                  | j                  j                         d{   }n|j                  dd       d{   }t        |      }||fS 7 7 07 w)zcReturn (Hello, round_trip_time).

        Can raise ConnectionFailure or OperationFailure.
        NT)r   )r&   r'   more_to_comer   _next_replyru   performed_handshakerf   r   _hellorh   rd   r)   )rF   r   r(   r   r   s        r!   r   zMonitor._check_with_socketb  s     
  4#3#3#55FHLLT55$:R:R:c:c "[[((9922 H "[[t44H&u-!! 6
 5s4   9C'C!A6C'2C#3C'C%C'#C'%C')rv   r   rG   r   rw   r   rx   r   rX   )r;   r   )r   r   r;   ztuple[Hello, float])rY   rZ   r[   rI   r}   r   r8   rP   rB   r   r.   r   r   r   __classcell__ry   s   @r!   r^   r^      sd    &*-&* &* 	&*
 ,&*P,


'!(0T%;N;z"r#   r^   c                  0     e Zd Zd fdZddZddZ xZS )
SrvMonitorc                R   t         |   |dt        j                  |j                         || _        | j
                  j                  | _        t        | j
                  j                  t              sJ | j
                  j                  | _        t        j                         | _        y)zClass to poll SRV records on a background thread.

        Pass a Topology and a TopologySettings.

        The Topology is weakly referenced.
        pymongo_srv_polling_threadN)rc   rI   r   MIN_SRV_RESCAN_INTERVALrd   rh   _seeds	_seedlistr   fqdnrU   _fqdnr&   r'   _startup_time)rF   rG   rx   ry   s      r!   rI   zSrvMonitor.__init__{  s~     	(**11		
 +..$..--s333..--
!^^-r#   c                j  K   t        j                         | j                  t        j                  z   k  ry | j                          d {   }|r6|| _        	 | j                  j                  | j                         d {    y y 7 =7 # t        $ r | j                          d {  7   Y y w xY wwr6   )r&   r'   r   r   r   _get_seedlistr   rD   on_srv_updater   rB   )rF   seedlists     r!   r.   zSrvMonitor._run  s     >>d0063Q3QQQ++--%DN#nn224>>BBB  . C! #jjl""#sT   AB3	B
B3(B  B	B B3	B B0'B*(B0-B3/B00B3c                  K   	 t        | j                  | j                  j                  j                  | j                  j
                        }|j                          d{   \  }}t        |      dk(  rt        	 | j                  j                  t        |t        j                               |S 7 Q# t        $ r | j                          Y yw xY ww)zXPoll SRV records for a seedlist.

        Returns a list of ServerDescriptions.
        Nr   )r
   r   rh   pool_optionsconnect_timeoutsrv_service_nameget_hosts_and_min_ttllenr   r?   update_intervalr%   r   r   rT   )rF   resolverr   ttls       r!   r   zSrvMonitor._get_seedlist  s     
	#

++;;//H
 #+"@"@"BBMHc8}! " NN**3sF4R4R+STO C  	
  	s;   CAB4  B2!B4 =5C2B4 4CCCC)rG   r   rx   r   rX   )r;   zOptional[list[tuple[str, Any]]])rY   rZ   r[   rI   r.   r   r   r   s   @r!   r   r   z  s    .&#r#   r   c                  P     e Zd Zd fdZd	dZd
dZddZd	dZd	dZddZ	 xZ
S )rp   c                    t         |   |d|j                  t        j                         || _        t               | _        t               | _	        t               | _        y)z\Maintain round trip times for a server.

        The Topology is weakly referenced.
        pymongo_server_rtt_taskN)rc   rI   rd   r   re   rg   r   _moving_averager	   _moving_minr   _lock)rF   rG   rx   rw   ry   s       r!   rI   z_RttMonitor.__init__  sQ    
 	%11))		
 
,(?')
r#   c                t   K   | j                          | j                  j                          d {    y 7 wr6   )r8   rg   r   rL   s    r!   rB   z_RttMonitor.close  s*      jj   s   .868c                   K   | j                   4 d{    | j                  j                  |       | j                  j                  |       ddd      d{    y7 K7 # 1 d{  7  sw Y   yxY ww)zAdd a RTT sample.N)r   r   r   r   )rF   samples     r!   r   z_RttMonitor.add_sample  sd     :: 	0 	0  ++F3''/	0 	0 	0 	0 	0 	0 	0sD   A8AA87A#A8A!A8!A8#A5)A,*A51A8c                   K   | j                   4 d{    | j                  j                         | j                  j                         fcddd      d{    S 7 I7 # 1 d{  7  sw Y   yxY ww)zBGet the calculated average, or None if no samples yet and the min.N)r   r   r   r   rL   s    r!   r   z_RttMonitor.get  sk     :: 	F 	F''++-t/?/?/C/C/EE	F 	F 	F 	F 	F 	F 	FsD   A6AA64A!A6AA6A6!A3'A*(A3/A6c                   K   | j                   4 d{    | j                  j                          | j                  j                          ddd      d{    y7 I7 # 1 d{  7  sw Y   yxY ww)zReset the average RTT.N)r   r   r   r   rL   s    r!   r   z_RttMonitor.reset  s`     :: 	% 	%  &&(""$	% 	% 	% 	% 	% 	% 	%sD   A6AA65A!A6AA6A6!A3'A*(A3/A6c                "  K   	 | j                          d {   }| j                  |       d {    y 7 7 # t        $ r | j                          d {  7   Y y t        $ r& | j
                  j                          d {  7   Y y w xY wwr6   )_pingr   r   rB   r   rg   r   )rF   rtts     r!   r.   z_RttMonitor._run  sq     
	% 

$C//#&&& %& 	**, 	%**""$$$	%sa   B: 6: 8: B: : BABB%BBB	BBBc                X  K   | j                   j                         4 d{   }| j                  j                  rt	        d      t        j                         }|j                          d{    t        |      cddd      d{    S 7 m7 "7 	# 1 d{  7  sw Y   yxY ww)z)Run a "hello" command and return the RTT.Nz_RttMonitor closed)	rg   r   r?   r   r   r&   r'   hellor)   )rF   r   r(   s      r!   r   z_RttMonitor._ping  s     ::&&( 	. 	.D~~&& 455NN$E**,&u-	. 	. 	. 		. 	. 	. 	.sW   B*BB*A	B.B/B=B*	B
B*BB*B'BB'#B*)rG   r   rx   r   rw   r   rX   )r   rW   r;   r=   )r;   ztuple[Optional[float], float])r;   rW   )rY   rZ   r[   rI   rB   r   r   r   r.   r   r   r   s   @r!   rp   rp     s'    *"!0F
%%.r#   rp   c                b    t        j                  | t              }t        j	                  |       y r6   )r@   rA   _unregister	_MONITORSadd)r/   rA   s     r!   rE   rE     s    
++g{
+CMM#r#   c                .    t         j                  |        y r6   )r   remove)monitor_refs    r!   r   r     s    [!r#   c                 v    t         y t        t               } | D ]  } |       }|s|j                           d }y r6   )r   listr8   )monitorsrA   r/   s      r!   _shutdown_monitorsr     sD     IH  $%!!#$
 Gr#   c                 B    t         } | r |         t        } | r |         y y r6   )r   r   )shutdowns    r!   _shutdown_resourcesr     s!    !H
"H
 r#   )r    r   r;   r=   )r(   rW   r;   rW   )r/   r+   r;   r=   )r   z"weakref.ReferenceType[MonitorBase]r;   r=   rX   )<__doc__
__future__r   r   atexitr   r&   r@   typingr   r   r   pymongor   r   pymongo._csotr	   !pymongo.asynchronous.srv_resolverr
   pymongo.errorsr   r   pymongo.hellor   pymongo.lockr   pymongo.loggerr   r   r   pymongo.periodic_executorr   pymongo.pool_optionsr   pymongo.read_preferencesr   pymongo.server_descriptionr   pymongo.asynchronous.poolr   r   r   pymongo.asynchronous.settingsr   pymongo.asynchronous.topologyr   _IS_SYNCr"   r)   r+   r^   r   rp   setr   rE   r   r   r   registerr\   r#   r!   <module>r     s    @ "      / / - ' : >  + G G 9 ) 2 8 
 ?6.; ;|w"k w"t9 9x=.+ =.F E	
"  FOO'( r#   