
    [jC                       d Z ddlmZ ddlZddlZddlZddlmZmZm	Z	m
Z
mZ ddlmZ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 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&dZ* G d d          Z+ G d de+          Z, G d de+          Z- G d de+          Z. e/            Z0d'd Z1d(d#Z2d)d$Z3d)d%Z4 ej5        e4           dS )*z9Class to monitor a MongoDB server on a background thread.    )annotationsN)TYPE_CHECKINGAnyMappingOptionalcast)commonperiodic_executor)MovingMinimum)NotPrimaryErrorOperationFailure_OperationCancelled)Hello)_create_lock)_shutdown_executors)_is_faas)MovingAverage)ServerDescription)_SrvResolver)
ConnectionPool_CancellationContext)TopologySettings)Topologyerror	ExceptionreturnNonec                0    d| _         d| _        d| _        dS )z'PYTHON-2433 Clear error traceback info.N)__traceback____context__	__cause__r   s    S/var/www/app.qaans.com/backend/venv/lib/python3.11/site-packages/pymongo/monitor.py	_sanitizer%   )   s    EEEOOO    c                  @    e Zd Zdd	ZddZddZddZdddZddZdS )MonitorBasetopologyr   namestrintervalintmin_intervalfloatc                    d
fd}t          j        ||||          }|| _        ddfd	}t          j        | |j                  t          j        ||          | _        t          |            dS )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.
        r   boolc                 L                 } | dS |                                   dS )NFT)_run)monitorself_refs    r$   targetz$MonitorBase.__init__.<locals>.target9   s*    hjjGuLLNNN4r&   )r,   r.   r6   r*   NdummyOptional[Topology]r   c                L                 }|r|                                  d S d S Ngc_safe_close)r7   r4   r5   s     r$   _on_topology_gcz-MonitorBase.__init__.<locals>._on_topology_gcF   s:     hjjG (%%'''''( (r&   )r   r1   r:   )r7   r8   r   r   )	r
   PeriodicExecutor	_executorweakrefrefcloseproxy	_topology	_register)	selfr)   r*   r,   r.   r6   executorr=   r5   s	           @r$   __init__zMonitorBase.__init__1   s    	 	 	 	 	 	 %5Ld
 
 
 "	( 	( 	( 	( 	( 	( 	( ;tX^44 xAA$r&   r   r   c                8    | j                                          dS )z[Start monitoring, or restart after a fork.

        Multiple calls have no effect.
        N)r?   openrF   s    r$   rJ   zMonitorBase.openR   s    
 	r&   c                8    | j                                          dS )zGC safe close.N)r?   rB   rK   s    r$   r<   zMonitorBase.gc_safe_closeY   s    r&   c                .    |                                   dS )zWClose and stop monitoring.

        open() restarts the monitor after closing.
        Nr;   rK   s    r$   rB   zMonitorBase.close]   s    
 	r&   NtimeoutOptional[int]c                :    | j                             |           dS )zWait for the monitor to stop.N)r?   join)rF   rN   s     r$   rQ   zMonitorBase.joind   s    G$$$$$r&   c                8    | j                                          dS )z)If the monitor is sleeping, wake it soon.N)r?   wakerK   s    r$   request_checkzMonitorBase.request_checkh   s    r&   )r)   r   r*   r+   r,   r-   r.   r/   r   r   r:   )rN   rO   r   r   )	__name__
__module____qualname__rH   rJ   r<   rB   rQ   rT    r&   r$   r(   r(   0   s           B         % % % % %     r&   r(   c                  h     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 xZS )Monitorserver_descriptionr   r)   r   poolr   topology_settingsr   c                   t                                          |d|j        t          j                   || _        || _        || _        | j        j        j	        | _
        | j
        duo| j
        j        | _        d| _        t          |||                    |j                            | _        |j        dk    r	d| _        dS |j        dk    r	d| _        dS t)                       | _        dS )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_threadNstreamTpollF)superrH   heartbeat_frequencyr	   MIN_HEARTBEAT_INTERVAL_server_description_pool	_settings_pool_options_event_listeners
_listenersenabled_for_server_heartbeat_publish_cancel_context_RttMonitor_create_pool_for_monitoraddress_rtt_monitorserver_monitoring_mode_streamr   )rF   r\   r)   r]   r^   	__class__s        r$   rH   zMonitor.__init__n   s     	+1)		
 	
 	
 $6 
*.6Gt3d8d?C'--.@.HII
 

 3x??DLLL5?? DLLL'zz>DLLLr&   r   r   c                D    | j         }|r|                                 dS dS )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   s6     & 	 NN		 	r&   c                    | j                                          | j        j        r| j                                          dS dS )z1Start an _RttMonitor that periodically runs ping.N)rr   rJ   r?   _stoppedrB   rK   s    r$   _start_rtt_monitorzMonitor._start_rtt_monitor   sK    
 	   >" 	&##%%%%%	& 	&r&   c                    | j                                          | j                                         |                                  d S r:   )r?   rB   rr   r<   ry   rK   s    r$   r<   zMonitor.gc_safe_close   sC    '')))r&   c                    |                                   | j                                         |                                  d S r:   )r<   rr   rB   _reset_connectionrK   s    r$   rB   zMonitor.close   sC    !!! 	     r&   c                8    | j                                          d S r:   )rg   resetrK   s    r$   r   zMonitor._reset_connection   s    
r&   c                   	 | j         }	 |                                 | _         ng# t          $ rZ}t          |           t	          | j         j        |          | _         |j        r| j                                         Y d }~d S d }~ww xY w| j	        
                    | j         | j         j                   | j        rE| j         j        r9| j         j        r-|                                  | j                                         | j         j        r"|j        r| j                                         d S d S d S # t          $ r |                                  Y d S w xY w)Nr#   )
reset_pool)rf   _check_serverr   r%   r   rq   is_server_type_knownr?   
skip_sleeprD   	on_changer   rt   topology_versionr|   ReferenceErrorrB   )rF   prev_sdexcs      r$   r3   zMonitor._run   s   "	.G+/+=+=+?+?((& 
 
 
#+<,4C, , ,( / 0 N--///
 N$$(T5M5S %    | ,(=,,=, '')))))+++'- ,'2N ,))+++++, , , ,  	 	 	JJLLLLLL	s8   D4 $ D4 
BAB=D4 BB&D4 4EEc                (   t          j                    }	 	 |                                 S # t          t          f$ r_}t          t          t          t          f         |j	                  }| j
                            |                    d                      d}~ww xY w# t          $ r  t          $ r}t          |           | j        }|j        }t          j                    |z
  }| j        rHt'          | j        o|j        o|j                  }| j        J | j                            ||||           |                                  t5          |t6                    r | j                                         t=          ||          cY d}~S d}~ww xY w)z^Call hello or read the next streaming response.

        Returns a ServerDescription.
        z$clusterTimeNr#   )time	monotonic_check_oncer   r   r   r   r+   r   detailsrD   receive_cluster_timegetr   r   r%   rf   rq   rm   r1   rt   r   r   rk   publish_server_heartbeat_failedr   
isinstancer   rr   r   r   )	rF   startr   r   r   sdrq   durationawaiteds	            r$   r   zMonitor._check_server   s   
   	;'')))$o6   wsCx0#+>>33GKK4O4OPPP	
  	 	 	 	; 	; 	;e)BjG~''%/H} ct|_0G_BL_``222??SXZabbb""$$$%!455 ##%%%$WE:::::::::	;s4   * BABBB F1CFFFc                   | j         j        }| j        rY| j        J | j         }t	          | j        j        o| j        o|j        o|j	                  }| j        
                    ||           | j        r | j        j        r|                                  | j                                        5 }|j        | _        |                     |          \  }}|j        s| j                            |           | j                                        \  }}t+          ||||          }| j        r+| j        J | j                            ||||j                   |cddd           S # 1 swxY w Y   dS )zfA single attempt to call hello.

        Returns a ServerDescription, or raises an exception.
        N)min_round_trip_time)rf   rq   rm   rk   r1   rg   connsrt   r   r    publish_server_heartbeat_startedrn   	cancelledr   checkoutcancel_context_check_with_socket	awaitablerr   
add_sampler   r   "publish_server_heartbeat_succeeded)	rF   rq   r   r   connresponseround_trip_timeavg_rttmin_rtts	            r$   r   zMonitor._check_once   s   
 *2= 	O?...)B 
  (L(+( '	 G O<<WgNNN 	%D$8$B 	%""$$$Z  "" 	d#'#6D (,(?(?(E(E%Ho% >!,,_===#04466GW"7HgSZ[[[B} 222BB_h8J   	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   -B(E""E&)E&r   r   tuple[Hello, float]c                   | j                                         }t          j                    }|j        r$t          |                                d          }n]| j        r?|j        r8| j	        j
        r,|                    || j	        j
        | j        j                  }n|                    |dd          }|t          j                    |z
  fS )zcReturn (Hello, round_trip_time).

        Can raise ConnectionFailure or OperationFailure.
        T)r   N)rD   max_cluster_timer   r   more_to_comer   _next_replyrt   performed_handshakerf   r   _hellorh   rd   )rF   r   cluster_timer   r   s        r$   r   zMonitor._check_with_socket$  s    
 ~6688   	=T--//4@@@HHL	=!5	=:>:R:c	= {{(92 HH {{<t<<H))E111r&   )r\   r   r)   r   r]   r   r^   r   rU   )r   r   )r   r   r   r   )rV   rW   rX   rH   ry   r|   r<   rB   r   r3   r   r   r   __classcell__ru   s   @r$   r[   r[   m   s        %* %* %* %* %* %*N   & & & &   
! ! ! !   # # # #J; ; ; ;@" " " "H2 2 2 2 2 2 2 2r&   r[   c                  0     e Zd Zd fdZddZdd
Z xZS )
SrvMonitorr)   r   r^   r   c                   t                                          |dt          j        |j                   || _        | j        j        | _        t          | j        j	        t                    sJ | j        j	        | _        dS )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   rH   r	   MIN_SRV_RESCAN_INTERVALrd   rh   _seeds	_seedlistr   fqdnr+   _fqdn)rF   r)   r^   ru   s      r$   rH   zSrvMonitor.__init__>  ss     	(*1		
 	
 	
 +.$.-s33333.-


r&   r   r   c                    |                                  }|rN|| _        	 | j                            | j                   d S # t          $ r |                                  Y d S w xY wd S r:   )_get_seedlistr   rD   on_srv_updater   rB   )rF   seedlists     r$   r3   zSrvMonitor._runP  s}    %%'' 	%DN,,T^<<<<<!   

		 	s   A   A"!A"Optional[list[tuple[str, Any]]]c                x   	 t          | j        | j        j        j        | j        j                  }|                                \  }}t          |          dk    rt          	 | j	        
                    t          |t          j                             |S # t          $ r |                                  Y dS w xY w)zXPoll SRV records for a seedlist.

        Returns a list of ServerDescriptions.
        r   N)r   r   rh   pool_optionsconnect_timeoutsrv_service_nameget_hosts_and_min_ttllenr   r?   update_intervalmaxr	   r   rT   )rF   resolverr   ttls       r$   r   zSrvMonitor._get_seedlistZ  s    
	#
+;/ H
 %::<<MHc8}}!! " N**3sF4R+S+STTTO  	 	 	
    44	s   A B B98B9)r)   r   r^   r   rU   )r   r   )rV   rW   rX   rH   r3   r   r   r   s   @r$   r   r   =  se        . . . . . .$          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 )ro   r)   r   r^   r   r]   r   c                    t                                          |d|j        t          j                   || _        t                      | _        t                      | _	        t                      | _        dS )z\Maintain round trip times for a server.

        The Topology is weakly referenced.
        pymongo_server_rtt_threadN)rc   rH   rd   r	   re   rg   r   _moving_averager   _moving_minr   _lock)rF   r)   r^   r]   ru   s       r$   rH   z_RttMonitor.__init__v  sc    
 	'1)		
 	
 	
 
,(??!^^


r&   r   r   c                `    |                                   | j                                         d S r:   )r<   rg   r   rK   s    r$   rB   z_RttMonitor.close  s0     	
r&   sampler/   c                    | j         5  | j                            |           | j                            |           ddd           dS # 1 swxY w Y   dS )zAdd a RTT sample.N)r   r   r   r   )rF   r   s     r$   r   z_RttMonitor.add_sample  s    Z 	0 	0 ++F333''///	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0 	0s   5A

AAtuple[Optional[float], float]c                    | j         5  | j                                        | j                                        fcddd           S # 1 swxY w Y   dS )zBGet the calculated average, or None if no samples yet and the min.N)r   r   r   r   rK   s    r$   r   z_RttMonitor.get  s    Z 	F 	F'++--t/?/C/C/E/EE	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	F 	Fs   2AAAc                    | j         5  | j                                         | j                                         ddd           dS # 1 swxY w Y   dS )zReset the average RTT.N)r   r   r   r   rK   s    r$   r   z_RttMonitor.reset  s    Z 	% 	% &&(((""$$$	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	% 	%s   3AAAc                    	 |                                  }|                     |           d S # t          $ r |                                  Y d S t          $ r | j                                         Y d S w xY wr:   )_pingr   r   rB   r   rg   r   )rF   rtts     r$   r3   z_RttMonitor._run  s    
	 **,,COOC      	 	 	JJLLLLLL 	 	 	J	s   )- A4"A43A4c                   | j                                         5 }| j        j        rt	          d          t          j                    }|                                 t          j                    |z
  cddd           S # 1 swxY w Y   dS )z)Run a "hello" command and return the RTT.z_RttMonitor closedN)rg   r   r?   r{   r   r   r   hello)rF   r   r   s      r$   r   z_RttMonitor._ping  s    Z  "" 	,d~& 6 4555N$$EJJLLL>##e+	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	, 	,s   AA??BB)r)   r   r^   r   r]   r   rU   )r   r/   r   r   )r   r   )r   r/   )rV   rW   rX   rH   rB   r   r   r   r3   r   r   r   s   @r$   ro   ro   u  s        $ $ $ $ $ $"   0 0 0 0F F F F
% % % %   , , , , , , , ,r&   ro   r4   c                n    t          j        | t                    }t                              |           d S r:   )r@   rA   _unregister	_MONITORSadd)r4   rA   s     r$   rE   rE     s+    
+g{
+
+CMM#r&   monitor_ref"weakref.ReferenceType[MonitorBase]c                :    t                               |            d S r:   )r   remove)r   s    r$   r   r     s    [!!!!!r&   c                     t           d S t          t                     } | D ]"} |            }|r|                                 #d }d S r:   )r   listr<   )monitorsrA   r4   s      r$   _shutdown_monitorsr     sZ     IH  $ $#%% 	$!!###GGGr&   c                 V    t           } | r
 |              t          } | r |              d S d S r:   )r   r   )shutdowns    r$   _shutdown_resourcesr     s@    !H 


"H 




 r&   )r   r   r   r   )r4   r(   r   r   )r   r   r   r   rU   )6__doc__
__future__r   atexitr   r@   typingr   r   r   r   r   pymongor	   r
   pymongo._csotr   pymongo.errorsr   r   r   pymongo.hellor   pymongo.lockr   pymongo.periodic_executorr   pymongo.poolr   pymongo.read_preferencesr   pymongo.server_descriptionr   pymongo.srv_resolverr   r   r   r   pymongo.settingsr   pymongo.topologyr   r%   r(   r[   r   ro   setr   rE   r   r   r   registerrY   r&   r$   <module>r      s   @ ? " " " " " "    > > > > > > > > > > > > > > - - - - - - - - ' ' ' ' ' ' Q Q Q Q Q Q Q Q Q Q       % % % % % % 9 9 9 9 9 9 ! ! ! ! ! ! 2 2 2 2 2 2 8 8 8 8 8 8 - - - - - - *CCCCCCCCCC111111))))))   : : : : : : : :zM2 M2 M2 M2 M2k M2 M2 M2`5 5 5 5 5 5 5 5p=, =, =, =, =,+ =, =, =,F CEE	   
" " " "        # $ $ $ $ $r&   