o Sb@s<dZddlZddlmZmZddlmZmZmZm Z ddl m Z ddl m Z mZddlmZddlmZmZerDdd lmZdd lmZed d ZegggggZGd ddeZGdddeZGdddeZGdddeZGdddeZGdddeZ ddZ!ddZ"deddfdd Z#e$gd!Z%d"d#Z&Gd$d%d%eZ'Gd&d'd'e'Z(Gd(d)d)e'Z)Gd*d+d+e'Z*Gd,d-d-eZ+Gd.d/d/e+Z,Gd0d1d1e+Z-Gd2d3d3e+Z.Gd4d5d5e+Z/Gd6d7d7eZ0Gd8d9d9eZ1Gd:d;d;eZ2Gdd?d?e2Z4Gd@dAdAe2Z5GdBdCdCeZ6GdDdEdEeZ7GdFdGdGe2Z8GdHdIdIe2Z9GdJdKdKeZ:GdLdMdMe:Z;GdNdOdOe:ZGdTdUdUe>Z?GdVdWdWe>Z@GdXdYdYe>ZAGdZd[d[eZBGd\d]d]eBZCGd^d_d_eBZDGd`dadaeBZEGdbdcdceZFdS)daTools to monitor driver events. .. versionadded:: 3.1 .. attention:: Starting in PyMongo 3.11, the monitoring classes outlined below are included in the PyMongo distribution under the :mod:`~pymongo.event_loggers` submodule. Use :func:`register` to register global listeners for specific events. Listeners must inherit from one of the abstract classes below and implement the correct functions for that class. For example, a simple command logger might be implemented like this:: import logging from pymongo import monitoring class CommandLogger(monitoring.CommandListener): def started(self, event): logging.info("Command {0.command_name} with request id " "{0.request_id} started on server " "{0.connection_id}".format(event)) def succeeded(self, event): logging.info("Command {0.command_name} with request id " "{0.request_id} on server {0.connection_id} " "succeeded in {0.duration_micros} " "microseconds".format(event)) def failed(self, event): logging.info("Command {0.command_name} with request id " "{0.request_id} on server {0.connection_id} " "failed in {0.duration_micros} " "microseconds".format(event)) monitoring.register(CommandLogger()) Server discovery and monitoring events are also available. For example:: class ServerLogger(monitoring.ServerListener): def opened(self, event): logging.info("Server {0.server_address} added to topology " "{0.topology_id}".format(event)) def description_changed(self, event): previous_server_type = event.previous_description.server_type new_server_type = event.new_description.server_type if new_server_type != previous_server_type: # server_type_name was added in PyMongo 3.4 logging.info( "Server {0.server_address} changed type from " "{0.previous_description.server_type_name} to " "{0.new_description.server_type_name}".format(event)) def closed(self, event): logging.warning("Server {0.server_address} removed from topology " "{0.topology_id}".format(event)) class HeartbeatLogger(monitoring.ServerHeartbeatListener): def started(self, event): logging.info("Heartbeat sent to server " "{0.connection_id}".format(event)) def succeeded(self, event): # The reply.document attribute was added in PyMongo 3.4. logging.info("Heartbeat to server {0.connection_id} " "succeeded with reply " "{0.reply.document}".format(event)) def failed(self, event): logging.warning("Heartbeat to server {0.connection_id} " "failed with error {0.reply}".format(event)) class TopologyLogger(monitoring.TopologyListener): def opened(self, event): logging.info("Topology with id {0.topology_id} " "opened".format(event)) def description_changed(self, event): logging.info("Topology description updated for " "topology id {0.topology_id}".format(event)) previous_topology_type = event.previous_description.topology_type new_topology_type = event.new_description.topology_type if new_topology_type != previous_topology_type: # topology_type_name was added in PyMongo 3.4 logging.info( "Topology {0.topology_id} changed type from " "{0.previous_description.topology_type_name} to " "{0.new_description.topology_type_name}".format(event)) # The has_writable_server and has_readable_server methods # were added in PyMongo 3.4. if not event.new_description.has_writable_server(): logging.warning("No writable servers available.") if not event.new_description.has_readable_server(): logging.warning("No readable servers available.") def closed(self, event): logging.info("Topology with id {0.topology_id} " "closed".format(event)) Connection monitoring and pooling events are also available. For example:: class ConnectionPoolLogger(ConnectionPoolListener): def pool_created(self, event): logging.info("[pool {0.address}] pool created".format(event)) def pool_cleared(self, event): logging.info("[pool {0.address}] pool cleared".format(event)) def pool_closed(self, event): logging.info("[pool {0.address}] pool closed".format(event)) def connection_created(self, event): logging.info("[pool {0.address}][conn #{0.connection_id}] " "connection created".format(event)) def connection_ready(self, event): logging.info("[pool {0.address}][conn #{0.connection_id}] " "connection setup succeeded".format(event)) def connection_closed(self, event): logging.info("[pool {0.address}][conn #{0.connection_id}] " "connection closed, reason: " "{0.reason}".format(event)) def connection_check_out_started(self, event): logging.info("[pool {0.address}] connection check out " "started".format(event)) def connection_check_out_failed(self, event): logging.info("[pool {0.address}] connection check out " "failed, reason: {0.reason}".format(event)) def connection_checked_out(self, event): logging.info("[pool {0.address}][conn #{0.connection_id}] " "connection checked out of pool".format(event)) def connection_checked_in(self, event): logging.info("[pool {0.address}][conn #{0.connection_id}] " "connection checked into pool".format(event)) Event listeners can also be registered per instance of :class:`~pymongo.mongo_client.MongoClient`:: client = MongoClient(event_listeners=[CommandLogger()]) Note that previously registered global listeners are automatically included when configuring per client event listeners. Registering a new global listener will not add that listener to existing client instances. .. note:: Events are delivered **synchronously**. Application threads block waiting for event handlers (e.g. :meth:`~CommandListener.started`) to return. Care must be taken to ensure that your event handlers are efficient enough to not adversely affect overall application performance. .. warning:: The command documents published through this API are *not* copies. If you intend to modify them in any way you must copy them in your event handler first. N)abc namedtuple) TYPE_CHECKINGAnyDictOptional)ObjectId)Hello HelloCompat)_handle_exception)_Address _DocumentOut)ServerDescription)TopologyDescription _Listeners)command_listenersserver_listenersserver_heartbeat_listenerstopology_listenerscmap_listenersc@seZdZdZdS)_EventListenerz,Abstract base class for all event listeners.N)__name__ __module__ __qualname____doc__rr9/tmp/pip-target-onvjaxws/lib/python/pymongo/monitoring.pyrsrc@.eZdZdZdddZdd d Zdd d ZdS)CommandListenerzAbstract base class for command listeners. Handles `CommandStartedEvent`, `CommandSucceededEvent`, and `CommandFailedEvent`. eventCommandStartedEventreturnNcCt)zAbstract method to handle a `CommandStartedEvent`. :Parameters: - `event`: An instance of :class:`CommandStartedEvent`. NotImplementedErrorselfrrrrstartedzCommandListener.startedCommandSucceededEventcCr")zAbstract method to handle a `CommandSucceededEvent`. :Parameters: - `event`: An instance of :class:`CommandSucceededEvent`. r#r%rrr succeededr(zCommandListener.succeededCommandFailedEventcCr")zAbstract method to handle a `CommandFailedEvent`. :Parameters: - `event`: An instance of :class:`CommandFailedEvent`. r#r%rrrfailedr(zCommandListener.failed)rr r!N)rr)r!N)rr+r!Nrrrrr'r*r,rrrrrs   rc@s~eZdZdZd&ddZd'd d Zd(d d Zd)ddZd*ddZd+ddZ d,ddZ d-ddZ d.ddZ d/d!d"Z d0d$d%ZdS)1ConnectionPoolListenera-Abstract base class for connection pool listeners. Handles all of the connection pool events defined in the Connection Monitoring and Pooling Specification: :class:`PoolCreatedEvent`, :class:`PoolClearedEvent`, :class:`PoolClosedEvent`, :class:`ConnectionCreatedEvent`, :class:`ConnectionReadyEvent`, :class:`ConnectionClosedEvent`, :class:`ConnectionCheckOutStartedEvent`, :class:`ConnectionCheckOutFailedEvent`, :class:`ConnectionCheckedOutEvent`, and :class:`ConnectionCheckedInEvent`. .. versionadded:: 3.9 rPoolCreatedEventr!NcCr")zAbstract method to handle a :class:`PoolCreatedEvent`. Emitted when a Connection Pool is created. :Parameters: - `event`: An instance of :class:`PoolCreatedEvent`. r#r%rrr pool_createdz#ConnectionPoolListener.pool_createdPoolReadyEventcCr")zAbstract method to handle a :class:`PoolReadyEvent`. Emitted when a Connection Pool is marked ready. :Parameters: - `event`: An instance of :class:`PoolReadyEvent`. .. versionadded:: 4.0 r#r%rrr pool_readys z!ConnectionPoolListener.pool_readyPoolClearedEventcCr")zAbstract method to handle a `PoolClearedEvent`. Emitted when a Connection Pool is cleared. :Parameters: - `event`: An instance of :class:`PoolClearedEvent`. r#r%rrr pool_clearedr1z#ConnectionPoolListener.pool_clearedPoolClosedEventcCr")zAbstract method to handle a `PoolClosedEvent`. Emitted when a Connection Pool is closed. :Parameters: - `event`: An instance of :class:`PoolClosedEvent`. r#r%rrr pool_closed'r1z"ConnectionPoolListener.pool_closedConnectionCreatedEventcCr")zAbstract method to handle a :class:`ConnectionCreatedEvent`. Emitted when a Connection Pool creates a Connection object. :Parameters: - `event`: An instance of :class:`ConnectionCreatedEvent`. r#r%rrrconnection_created1r1z)ConnectionPoolListener.connection_createdConnectionReadyEventcCr")zAbstract method to handle a :class:`ConnectionReadyEvent`. Emitted when a Connection has finished its setup, and is now ready to use. :Parameters: - `event`: An instance of :class:`ConnectionReadyEvent`. r#r%rrrconnection_ready; z'ConnectionPoolListener.connection_readyConnectionClosedEventcCr")zAbstract method to handle a :class:`ConnectionClosedEvent`. Emitted when a Connection Pool closes a Connection. :Parameters: - `event`: An instance of :class:`ConnectionClosedEvent`. r#r%rrrconnection_closedFr1z(ConnectionPoolListener.connection_closedConnectionCheckOutStartedEventcCr")zAbstract method to handle a :class:`ConnectionCheckOutStartedEvent`. Emitted when the driver starts attempting to check out a connection. :Parameters: - `event`: An instance of :class:`ConnectionCheckOutStartedEvent`. r#r%rrrconnection_check_out_startedPr1z3ConnectionPoolListener.connection_check_out_startedConnectionCheckOutFailedEventcCr")zAbstract method to handle a :class:`ConnectionCheckOutFailedEvent`. Emitted when the driver's attempt to check out a connection fails. :Parameters: - `event`: An instance of :class:`ConnectionCheckOutFailedEvent`. r#r%rrrconnection_check_out_failedZr1z2ConnectionPoolListener.connection_check_out_failedConnectionCheckedOutEventcCr")zAbstract method to handle a :class:`ConnectionCheckedOutEvent`. Emitted when the driver successfully checks out a Connection. :Parameters: - `event`: An instance of :class:`ConnectionCheckedOutEvent`. r#r%rrrconnection_checked_outdr1z-ConnectionPoolListener.connection_checked_outConnectionCheckedInEventcCr")aAbstract method to handle a :class:`ConnectionCheckedInEvent`. Emitted when the driver checks in a Connection back to the Connection Pool. :Parameters: - `event`: An instance of :class:`ConnectionCheckedInEvent`. r#r%rrrconnection_checked_innr<z,ConnectionPoolListener.connection_checked_in)rr/r!N)rr2r!N)rr4r!N)rr6r!N)rr8r!N)rr:r!N)rr=r!N)rr?r!N)rrAr!N)rrCr!N)rrEr!N)rrrrr0r3r5r7r9r;r>r@rBrDrFrrrrr.s   r.c@r)ServerHeartbeatListenerzAbstract base class for server heartbeat listeners. Handles `ServerHeartbeatStartedEvent`, `ServerHeartbeatSucceededEvent`, and `ServerHeartbeatFailedEvent`. .. versionadded:: 3.3 rServerHeartbeatStartedEventr!NcCr")zAbstract method to handle a `ServerHeartbeatStartedEvent`. :Parameters: - `event`: An instance of :class:`ServerHeartbeatStartedEvent`. r#r%rrrr'r(zServerHeartbeatListener.startedServerHeartbeatSucceededEventcCr")zAbstract method to handle a `ServerHeartbeatSucceededEvent`. :Parameters: - `event`: An instance of :class:`ServerHeartbeatSucceededEvent`. r#r%rrrr*r(z!ServerHeartbeatListener.succeededServerHeartbeatFailedEventcCr")zAbstract method to handle a `ServerHeartbeatFailedEvent`. :Parameters: - `event`: An instance of :class:`ServerHeartbeatFailedEvent`. r#r%rrrr,r(zServerHeartbeatListener.failed)rrHr!N)rrIr!N)rrJr!Nr-rrrrrGzs   rGc@r)TopologyListenerzAbstract base class for topology monitoring listeners. Handles `TopologyOpenedEvent`, `TopologyDescriptionChangedEvent`, and `TopologyClosedEvent`. .. versionadded:: 3.3 rTopologyOpenedEventr!NcCr")zAbstract method to handle a `TopologyOpenedEvent`. :Parameters: - `event`: An instance of :class:`TopologyOpenedEvent`. r#r%rrropenedr(zTopologyListener.openedTopologyDescriptionChangedEventcCr")zAbstract method to handle a `TopologyDescriptionChangedEvent`. :Parameters: - `event`: An instance of :class:`TopologyDescriptionChangedEvent`. r#r%rrrdescription_changedr(z$TopologyListener.description_changedTopologyClosedEventcCr")zAbstract method to handle a `TopologyClosedEvent`. :Parameters: - `event`: An instance of :class:`TopologyClosedEvent`. r#r%rrrclosedr(zTopologyListener.closed)rrLr!N)rrNr!N)rrPr!NrrrrrMrOrQrrrrrK   rKc@r)ServerListenerzAbstract base class for server listeners. Handles `ServerOpeningEvent`, `ServerDescriptionChangedEvent`, and `ServerClosedEvent`. .. versionadded:: 3.3 rServerOpeningEventr!NcCr")zAbstract method to handle a `ServerOpeningEvent`. :Parameters: - `event`: An instance of :class:`ServerOpeningEvent`. r#r%rrrrMr(zServerListener.openedServerDescriptionChangedEventcCr")zAbstract method to handle a `ServerDescriptionChangedEvent`. :Parameters: - `event`: An instance of :class:`ServerDescriptionChangedEvent`. r#r%rrrrOr(z"ServerListener.description_changedServerClosedEventcCr")zAbstract method to handle a `ServerClosedEvent`. :Parameters: - `event`: An instance of :class:`ServerClosedEvent`. r#r%rrrrQr(zServerListener.closed)rrUr!N)rrVr!N)rrWr!NrRrrrrrTrSrTcCst|dS)z'Convert duration 'dur' to microseconds.g.A)int total_seconds)Zdurrrr _to_microssrZcCs@t|tjs td|f|D]}t|tstd|fq|S)zValidate event listenersz%s must be a list or tupleListeners for %s must be either a CommandListener, ServerHeartbeatListener, ServerListener, TopologyListener, or ConnectionPoolListener.) isinstancerSequence TypeErrorr)option listenerslistenerrrr_validate_event_listenerss  rbrar!cCst|ts td|ft|trtj|t|tr"tj|t|t r-tj |t|t r8tj |t|t rEtj|dSdS)aRegister a global event listener. :Parameters: - `listener`: A subclasses of :class:`CommandListener`, :class:`ServerHeartbeatListener`, :class:`ServerListener`, :class:`TopologyListener`, or :class:`ConnectionPoolListener`. r[N)r\rr^r _LISTENERSrappendrGrrTrrKrr.r)rarrrregisters"          re) Z authenticateZ saslstartZ saslcontinueZgetnonceZ createuserZ updateuserZcopydbgetnonceZcopydbsaslstartZcopydbcCs"|dtjfvrd|vrdSdS)NZhelloZspeculativeAuthenticateTF)lowerr Z LEGACY_CMD) command_namedocrrr_is_speculative_authenticate!s ric @seZdZdZdZ ddedededeedee d df d d Z e d efd d Z e d efddZ e d efddZe d ee fddZe d eefddZdS) _CommandEventzBase class for command events.)Z __cmd_nameZ __rqst_idZ __conn_idZ__op_id __service_idNrg request_id connection_id operation_id service_idr!cCs"||_||_||_||_||_dSN)_CommandEvent__cmd_name_CommandEvent__rqst_id_CommandEvent__conn_id_CommandEvent__op_id_CommandEvent__service_id)r&rgrlrmrnrorrr__init__/s  z_CommandEvent.__init__cC|jS)zThe command name.)rqr&rrrrg=z_CommandEvent.command_namecCrw)z"The request id for this operation.)rrrxrrrrlBryz_CommandEvent.request_idcCrw)z@The address (host, port) of the server this command was sent to.)rsrxrrrrmGryz_CommandEvent.connection_idcCrw)z^The service_id this command was sent to, or ``None``. .. versionadded:: 3.12 )rurxrrrroLsz_CommandEvent.service_idcCrw)z(An id for this series of events or None.)rtrxrrrrnTryz_CommandEvent.operation_idrp)rrrr __slots__strrXr rrrvpropertyrgrlrmrornrrrrrj*s6 rjcszeZdZdZdZ ddedededede ed e e d dffd d Z e d efd dZ e d efddZddZZS)r aEvent published when a command starts. :Parameters: - `command`: The command document. - `database_name`: The name of the database this command was run against. - `request_id`: The request id for this operation. - `connection_id`: The address (host, port) of the server this command was sent to. - `operation_id`: An optional identifier for a series of related events. - `service_id`: The service_id this command was sent to, or ``None``. )Z__cmdZ__dbNcommand database_namerlrmrnror!c sj|s td|ftt|}tt|j|||||d|}|tvs)t||r-i|_ n||_ ||_ dS)Nz%r is not a valid commandro) ValueErrornextitersuperr rvrf_SENSITIVE_COMMANDSri_CommandStartedEvent__cmd_CommandStartedEvent__db) r&r}r~rlrmrnrorgcmd_name __class__rrrvis     zCommandStartedEvent.__init__cCrw)zThe command document.)rrxrrrr}ryzCommandStartedEvent.commandcCrw)z6The name of the database this command was run against.)rrxrrrr~ryz!CommandStartedEvent.database_namecC"d|jj|j|j|j|j|jfS)Nz=<%s %s db: %r, command: %r, operation_id: %s, service_id: %s>)rrrmr~rgrnrorxrrr__repr__zCommandStartedEvent.__repr__rp)rrrrrzr r{rXr rrrvr|r}r~r __classcell__rrrrr Zs0  r ceZdZdZdZ ddejdedede de d e e d e e d dffd d Z ed e fddZed efddZddZZS)r)a Event published when a command succeeds. :Parameters: - `duration`: The command duration as a datetime.timedelta. - `reply`: The server reply document. - `command_name`: The command name. - `request_id`: The request id for this operation. - `connection_id`: The address (host, port) of the server this command was sent to. - `operation_id`: An optional identifier for a series of related events. - `service_id`: The service_id this command was sent to, or ``None``. )__duration_micros__replyNdurationreplyrgrlrmrnror!c sRtt|j|||||dt||_|}|tvst||r$i|_dS||_dSNr) rr)rvrZ'_CommandSucceededEvent__duration_microsrfrri_CommandSucceededEvent__reply) r&rrrgrlrmrnrorrrrrvs    zCommandSucceededEvent.__init__cCrwz/The duration of this operation in microseconds.)rrxrrrduration_microsryz%CommandSucceededEvent.duration_microscCrwz/The server failure document for this operation.)rrxrrrrryzCommandSucceededEvent.replycCr)NzJ<%s %s command: %r, operation_id: %s, duration_micros: %s, service_id: %s>)rrrmrgrnrrorxrrrrrzCommandSucceededEvent.__repr__rp)rrrrrzdatetime timedeltar r{rXr rrrvr|rrrrrrrrr)s4   r)cr)r+a Event published when a command fails. :Parameters: - `duration`: The command duration as a datetime.timedelta. - `failure`: The server reply document. - `command_name`: The command name. - `request_id`: The request id for this operation. - `connection_id`: The address (host, port) of the server this command was sent to. - `operation_id`: An optional identifier for a series of related events. - `service_id`: The service_id this command was sent to, or ``None``. )rZ __failureNrfailurergrlrmrnror!cs.tt|j|||||dt||_||_dSr)rr+rvrZ$_CommandFailedEvent__duration_micros_CommandFailedEvent__failure)r&rrrgrlrmrnrorrrrvs   zCommandFailedEvent.__init__cCrwr)rrxrrrrryz"CommandFailedEvent.duration_microscCrwr)rrxrrrrryzCommandFailedEvent.failurecCs&d|jj|j|j|j|j|j|jfS)NzW<%s %s command: %r, operation_id: %s, duration_micros: %s, failure: %r, service_id: %s>)rrrmrgrnrrrorxrrrrszCommandFailedEvent.__repr__rp)rrrrrzrrr r{rXr rrrvr|rrrrrrrrr+s4   r+c@@eZdZdZdZdeddfddZedefdd Zd d Z dS) _PoolEventzBase class for pool events. __addressaddressr!NcC ||_dSrp_PoolEvent__addressr&rrrrrv  z_PoolEvent.__init__cCrw)zbThe address (host, port) pair of the server the pool is attempting to connect to. rrxrrrrz_PoolEvent.addresscCd|jj|jfSNz%s(%r))rrrrxrrrrz_PoolEvent.__repr__) rrrrrzr rvr|rrrrrrrs rcs\eZdZdZdZdedeeefddffdd Z e deeeffd d Z d d Z Z S) r/zPublished when a Connection Pool is created. :Parameters: - `address`: The address (host, port) pair of the server this Pool is attempting to connect to. .. versionadded:: 3.9 )Z __optionsroptionsr!Nctt||||_dSrp)rr/rv_PoolCreatedEvent__options)r&rrrrrrv& zPoolCreatedEvent.__init__cCrw)zCAny non-default pool options that were set on this Connection Pool.)rrxrrrr*ryzPoolCreatedEvent.optionscCd|jj|j|jfSNz %s(%r, %r))rrrrrxrrrr/zPoolCreatedEvent.__repr__)rrrrrzr rr{rrvr|rrrrrrrr/s "r/c@eZdZdZdZdS)r2zPublished when a Connection Pool is marked ready. :Parameters: - `address`: The address (host, port) pair of the server this Pool is attempting to connect to. .. versionadded:: 4.0 rNrrrrrzrrrrr23 r2csVeZdZdZdZd dedeeddffdd Ze deefd d Z d d Z Z S)r4aPublished when a Connection Pool is cleared. :Parameters: - `address`: The address (host, port) pair of the server this Pool is attempting to connect to. - `service_id`: The service_id this command was sent to, or ``None``. .. versionadded:: 3.9 )rkNrror!crrp)rr4rv_PoolClearedEvent__service_id)r&rrorrrrvMrzPoolClearedEvent.__init__cCrw)zConnections with this service_id are cleared. When service_id is ``None``, all connections in the pool are cleared. .. versionadded:: 3.12 )rrxrrrroQzPoolClearedEvent.service_idcCrr)rrrrrxrrrr[rzPoolClearedEvent.__repr__rp) rrrrrzr rrrvr|rorrrrrrr4@s  r4c@r)r6zPublished when a Connection Pool is closed. :Parameters: - `address`: The address (host, port) pair of the server this Pool is attempting to connect to. .. versionadded:: 3.9 rNrrrrrr6_rr6c@s&eZdZdZdZ dZ dZ dZdS)ConnectionClosedReasonzqAn enum that defines values for `reason` on a :class:`ConnectionClosedEvent`. .. versionadded:: 3.9 staleidleerror poolClosedN)rrrrZSTALEZIDLEERROR POOL_CLOSEDrrrrrlsrc@s eZdZdZdZ dZ dZdS)ConnectionCheckOutFailedReasonzyAn enum that defines values for `reason` on a :class:`ConnectionCheckOutFailedEvent`. .. versionadded:: 3.9 timeoutrZconnectionErrorN)rrrrTIMEOUTrZ CONN_ERRORrrrrrsrc@VeZdZdZdZdededdfddZedefd d Z edefd d Z d dZ dS)_ConnectionEventz.Private base class for some connection events.)r__connection_idrrmr!NcC||_||_dSrp)_ConnectionEvent__address_ConnectionEvent__connection_id)r&rrmrrrrv z_ConnectionEvent.__init__cCrwziThe address (host, port) pair of the server this connection is attempting to connect to. )rrxrrrrrz_ConnectionEvent.addresscCrw)zThe ID of the Connection.)rrxrrrrmryz_ConnectionEvent.connection_idcCrr)rrrrrxrrrrrz_ConnectionEvent.__repr__) rrrrrzr rXrvr|rrmrrrrrrs rc@r)r8aPublished when a Connection Pool creates a Connection object. NOTE: This connection is not ready for use until the :class:`ConnectionReadyEvent` is published. :Parameters: - `address`: The address (host, port) pair of the server this Connection is attempting to connect to. - `connection_id`: The integer ID of the Connection in this Pool. .. versionadded:: 3.9 rNrrrrrr8s r8c@r)r:a3Published when a Connection has finished its setup, and is ready to use. :Parameters: - `address`: The address (host, port) pair of the server this Connection is attempting to connect to. - `connection_id`: The integer ID of the Connection in this Pool. .. versionadded:: 3.9 rNrrrrrr: r:cs8eZdZdZdZfddZeddZddZZ S) r=aVPublished when a Connection is closed. :Parameters: - `address`: The address (host, port) pair of the server this Connection is attempting to connect to. - `connection_id`: The integer ID of the Connection in this Pool. - `reason`: A reason explaining why this connection was closed. .. versionadded:: 3.9 )__reasoncstt|||||_dSrp)rr=rv_ConnectionClosedEvent__reason)r&rrmreasonrrrrvs zConnectionClosedEvent.__init__cCrw)zA reason explaining why this connection was closed. The reason must be one of the strings from the :class:`ConnectionClosedReason` enum. )rrxrrrrzConnectionClosedEvent.reasoncCd|jj|j|j|jfS)Nz%s(%r, %r, %r))rrrrmrrxrrrr zConnectionClosedEvent.__repr__) rrrrrzrvr|rrrrrrrr=s  r=c@s0eZdZdZdZddZeddZddZd S) r?zPublished when the driver starts attempting to check out a connection. :Parameters: - `address`: The address (host, port) pair of the server this Connection is attempting to connect to. .. versionadded:: 3.9 rcCrrp(_ConnectionCheckOutStartedEvent__addressrrrrrvrz'ConnectionCheckOutStartedEvent.__init__cCrwrrrxrrrrrz&ConnectionCheckOutStartedEvent.addresscCrr)rrrrxrrrrrz'ConnectionCheckOutStartedEvent.__repr__N) rrrrrzrvr|rrrrrrr?s   r?c@r)rAa.Published when the driver's attempt to check out a connection fails. :Parameters: - `address`: The address (host, port) pair of the server this Connection is attempting to connect to. - `reason`: A reason explaining why connection check out failed. .. versionadded:: 3.9 )rrrrr!NcCrrp)'_ConnectionCheckOutFailedEvent__address&_ConnectionCheckOutFailedEvent__reason)r&rrrrrrvrz&ConnectionCheckOutFailedEvent.__init__cCrwr)rrxrrrrrz%ConnectionCheckOutFailedEvent.addresscCrw)zA reason explaining why connection check out failed. The reason must be one of the strings from the :class:`ConnectionCheckOutFailedReason` enum. )rrxrrrr"rz$ConnectionCheckOutFailedEvent.reasoncCrr)rrrrrxrrrr+rz&ConnectionCheckOutFailedEvent.__repr__) rrrrrzr r{rvr|rrrrrrrrA s  rAc@r)rCa*Published when the driver successfully checks out a Connection. :Parameters: - `address`: The address (host, port) pair of the server this Connection is attempting to connect to. - `connection_id`: The integer ID of the Connection in this Pool. .. versionadded:: 3.9 rNrrrrrrC/rrCc@r)rEa*Published when the driver checks in a Connection into the Pool. :Parameters: - `address`: The address (host, port) pair of the server this Connection is attempting to connect to. - `connection_id`: The integer ID of the Connection in this Pool. .. versionadded:: 3.9 rNrrrrrrE=rrEc@r) _ServerEventzBase class for server events.)Z__server_address __topology_idserver_address topology_idr!NcCrrp)_ServerEvent__server_address_ServerEvent__topology_id)r&rrrrrrvPrz_ServerEvent.__init__cCrw)z+The address (host, port) pair of the server)rrxrrrrTryz_ServerEvent.server_addresscCrwz>A unique identifier for the topology this server is a part of.)rrxrrrrYryz_ServerEvent.topology_idcCr)Nz<%s %s topology_id: %s>)rrrrrxrrrr^s z_ServerEvent.__repr__) rrrrrzr rrvr|rrrrrrrrKs rcZeZdZdZdZdddddeddffd d Zedd d Zedd dZ ddZ Z S)rVzJPublished when server description changes. .. versionadded:: 3.3 Z__previous_descriptionZ__new_descriptionprevious_descriptionrnew_descriptionargsr!Nc tt|j|||_||_dSrp)rrVrv4_ServerDescriptionChangedEvent__previous_description/_ServerDescriptionChangedEvent__new_descriptionr&rrrrrrrvn z&ServerDescriptionChangedEvent.__init__cCrw)zLThe previous :class:`~pymongo.server_description.ServerDescription`.)rrxrrrrxz2ServerDescriptionChangedEvent.previous_descriptioncCrw)zGThe new :class:`~pymongo.server_description.ServerDescription`.)rrxrrrr~rz-ServerDescriptionChangedEvent.new_descriptioncCr)Nz <%s %s changed from: %s, to: %s>)rrrrrrxrrrrrz&ServerDescriptionChangedEvent.__repr__)r!r rrrrrzrrvr|rrrrrrrrrVf"  rVc@r)rUzEPublished when server is initialized. .. versionadded:: 3.3 rNrrrrrrUrUc@r)rWz@Published when server is closed. .. versionadded:: 3.3 rNrrrrrrWrrWc@r) TopologyEventz+Base class for topology description events.rrr!NcCrrpZ_TopologyEvent__topology_id)r&rrrrrvrzTopologyEvent.__init__cCrwrrrxrrrrryzTopologyEvent.topology_idcCr)Nz<%s topology_id: %s>)rrrrxrrrrrzTopologyEvent.__repr__) rrrrrzrrvr|rrrrrrrs rcr)rNzPPublished when the topology description changes. .. versionadded:: 3.3 rrrrrr!Ncrrp)rrNrv6_TopologyDescriptionChangedEvent__previous_description1_TopologyDescriptionChangedEvent__new_descriptionrrrrrvrz(TopologyDescriptionChangedEvent.__init__cCrw)zPThe previous :class:`~pymongo.topology_description.TopologyDescription`.)rrxrrrrrz4TopologyDescriptionChangedEvent.previous_descriptioncCrw)zKThe new :class:`~pymongo.topology_description.TopologyDescription`.)rrxrrrrrz/TopologyDescriptionChangedEvent.new_descriptioncCr)Nz-<%s topology_id: %s changed from: %s, to: %s>)rrrrrrxrrrrrz(TopologyDescriptionChangedEvent.__repr__)r!rrrrrrrNrrNc@r)rLzKPublished when the topology is initialized. .. versionadded:: 3.3 rNrrrrrrLrrLc@r)rPzFPublished when the topology is closed. .. versionadded:: 3.3 rNrrrrrrPrrPc@r) _ServerHeartbeatEventz'Base class for server heartbeat events.rrmr!NcCrrpZ$_ServerHeartbeatEvent__connection_id)r&rmrrrrvrz_ServerHeartbeatEvent.__init__cCrw)zJThe address (host, port) of the server this heartbeat was sent to.rrxrrrrmrz#_ServerHeartbeatEvent.connection_idcCr)Nz<%s %s>)rrrmrxrrrrrz_ServerHeartbeatEvent.__repr__) rrrrrzr rvr|rmrrrrrrs rc@r)rHzFPublished when a heartbeat is started. .. versionadded:: 3.3 rNrrrrrrHrrHc |eZdZdZdZ ddededededd f fd d Z e defd d Z e defddZ e defddZ ddZZS)rIzIFired when the server heartbeat succeeds. .. versionadded:: 3.3 Z __durationrZ __awaitedFrrrmawaitedr!Nc&tt||||_||_||_dSrp)rrIrv(_ServerHeartbeatSucceededEvent__duration%_ServerHeartbeatSucceededEvent__reply'_ServerHeartbeatSucceededEvent__awaitedr&rrrmrrrrrv  z&ServerHeartbeatSucceededEvent.__init__cCrwz/The duration of this heartbeat in microseconds.)rrxrrrrryz&ServerHeartbeatSucceededEvent.durationcCrw)z-An instance of :class:`~pymongo.hello.Hello`.)rrxrrrrryz#ServerHeartbeatSucceededEvent.replycCrwzWhether the heartbeat was awaited. If true, then :meth:`duration` reflects the sum of the round trip time to the server and the time that the server waited before sending a response. )rrxrrrrrz%ServerHeartbeatSucceededEvent.awaitedcCd|jj|j|j|j|jfS)Nz,<%s %s duration: %s, awaited: %s, reply: %s>rrrmrrrrxrrrr(z&ServerHeartbeatSucceededEvent.__repr__F)rrrrrzfloatr r boolrvr|rrrrrrrrrrIs, rIc r)rJzxFired when the server heartbeat fails, either with an "ok: 0" or a socket exception. .. versionadded:: 3.3 rFrrrmrr!Ncrrp)rrJrv%_ServerHeartbeatFailedEvent__duration"_ServerHeartbeatFailedEvent__reply$_ServerHeartbeatFailedEvent__awaitedrrrrrv;rz#ServerHeartbeatFailedEvent.__init__cCrwr)rrxrrrrCryz#ServerHeartbeatFailedEvent.durationcCrw)zA subclass of :exc:`Exception`.)rrxrrrrHryz ServerHeartbeatFailedEvent.replycCrwr)rrxrrrrMrz"ServerHeartbeatFailedEvent.awaitedcCr)Nz,<%s %s duration: %s, awaited: %s, reply: %r>rrxrrrrWrz#ServerHeartbeatFailedEvent.__repr__r)rrrrrzr Exceptionr rrvr|rrrrrrrrrrJ2s, rJc@s&eZdZdZddZeddZeddZedd Zed d Z ed d Z ddZ d@ddZ   dAddZ  d@ddZddZddZddZddZd d!Zd"d#Zd$d%Zd&d'Zd(d)Zd*d+Zd,d-Zd.d/Zd0d1Zd2d3Zd4d5Zd6d7Zd8d9Zd:d;Z dd?Z"dS)B_EventListenerszConfigure event listeners for a client instance. Any event listeners registered globally are included by default. :Parameters: - `listeners`: A list of event listeners. cCstjdd|_tjdd|_tj}|dd|_tjdd|_tj dd|_ |durj|D]9}t |t r=|j |t |trH|j |t |trS|j |t |tr^|j |t |tri|j |q0t|j|_t|j|_t|j|_t|j|_t|j |_dSrp)rcr"_EventListeners__command_listenersr!_EventListeners__server_listenersr+_EventListeners__server_heartbeat_listenersr#_EventListeners__topology_listenersr_EventListeners__cmap_listenersr\rrdrTrGrKr.r%_EventListeners__enabled_for_commands#_EventListeners__enabled_for_server-_EventListeners__enabled_for_server_heartbeat%_EventListeners__enabled_for_topology!_EventListeners__enabled_for_cmap)r&r`lstrrrrvjs0              z_EventListeners.__init__cCrw)z-Are any CommandListener instances registered?)r rxrrrenabled_for_commandsryz$_EventListeners.enabled_for_commandscCrw)z,Are any ServerListener instances registered?)r rxrrrenabled_for_serverryz"_EventListeners.enabled_for_servercCrw)z5Are any ServerHeartbeatListener instances registered?)r rxrrrenabled_for_server_heartbeatryz,_EventListeners.enabled_for_server_heartbeatcCrw)z.Are any TopologyListener instances registered?)rrxrrrenabled_for_topologyryz$_EventListeners.enabled_for_topologycCrw)z4Are any ConnectionPoolListener instances registered?)rrxrrrenabled_for_cmapryz _EventListeners.enabled_for_cmapcCs|j|j|j|j|jS)z#List of registered event listeners.)rrrr r rxrrrevent_listenerssz_EventListeners.event_listenersNc CsV|dur|}t||||||d}|jD]}z||Wqty(tYqwdS)aPublish a CommandStartedEvent to all command listeners. :Parameters: - `command`: The command document. - `database_name`: The name of the database this command was run against. - `request_id`: The request id for this operation. - `connection_id`: The address (host, port) of the server this command was sent to. - `op_id`: The (optional) operation id for this operation. - `service_id`: The service_id this command was sent to, or ``None``. Nr)r rr'rr ) r&r}r~rlrmop_idror subscriberrrrpublish_command_starts    z%_EventListeners.publish_command_startFc Cs^|dur|}|r i}t|||||||} |jD]} z| | Wqty,tYqwdS)aPublish a CommandSucceededEvent to all command listeners. :Parameters: - `duration`: The command duration as a datetime.timedelta. - `reply`: The server reply document. - `command_name`: The command name. - `request_id`: The request id for this operation. - `connection_id`: The address (host, port) of the server this command was sent to. - `op_id`: The (optional) operation id for this operation. - `service_id`: The service_id this command was sent to, or ``None``. - `speculative_hello`: Was the command sent with speculative auth? N)r)rr*rr ) r&rrrgrlrmrroZspeculative_hellorrrrrpublish_command_successs   z'_EventListeners.publish_command_successc CsX|dur|}t|||||||d}|jD]} z| |Wqty)tYqwdS)acPublish a CommandFailedEvent to all command listeners. :Parameters: - `duration`: The command duration as a datetime.timedelta. - `failure`: The server reply document or failure description document. - `command_name`: The command name. - `request_id`: The request id for this operation. - `connection_id`: The address (host, port) of the server this command was sent to. - `op_id`: The (optional) operation id for this operation. - `service_id`: The service_id this command was sent to, or ``None``. Nr)r+rr,rr ) r&rrrgrlrmrrorrrrrpublish_command_failures   z'_EventListeners.publish_command_failurec C>t|}|jD]}z||WqtytYqwdS)zPublish a ServerHeartbeatStartedEvent to all server heartbeat listeners. :Parameters: - `connection_id`: The address (host, port) pair of the connection. N)rHrr'rr )r&rmrrrrr publish_server_heartbeat_started    z0_EventListeners.publish_server_heartbeat_startedc CDt||||}|jD]}z||Wq tytYq wdS)aPublish a ServerHeartbeatSucceededEvent to all server heartbeat listeners. :Parameters: - `connection_id`: The address (host, port) pair of the connection. - `duration`: The execution time of the event in the highest possible resolution for the platform. - `reply`: The command reply. - `awaited`: True if the response was awaited. N)rIrr*rr r&rmrrrrrrrr"publish_server_heartbeat_succeeded   z2_EventListeners.publish_server_heartbeat_succeededc Cr)aPublish a ServerHeartbeatFailedEvent to all server heartbeat listeners. :Parameters: - `connection_id`: The address (host, port) pair of the connection. - `duration`: The execution time of the event in the highest possible resolution for the platform. - `reply`: The command reply. - `awaited`: True if the response was awaited. N)rJrr,rr r rrrpublish_server_heartbeat_failed)r"z/_EventListeners.publish_server_heartbeat_failedc C@t||}|jD]}z||WqtytYqwdS)aPublish a ServerOpeningEvent to all server listeners. :Parameters: - `server_address`: The address (host, port) pair of the server. - `topology_id`: A unique identifier for the topology this server is a part of. N)rUrrMrr r&rrrrrrrpublish_server_opened;    z%_EventListeners.publish_server_openedc Cr$)aPublish a ServerClosedEvent to all server listeners. :Parameters: - `server_address`: The address (host, port) pair of the server. - `topology_id`: A unique identifier for the topology this server is a part of. N)rWrrQrr r%rrrpublish_server_closedJr'z%_EventListeners.publish_server_closedc CsDt||||}|jD]}z||Wq tytYq wdS)aPublish a ServerDescriptionChangedEvent to all server listeners. :Parameters: - `previous_description`: The previous server description. - `server_address`: The address (host, port) pair of the server. - `new_description`: The new server description. - `topology_id`: A unique identifier for the topology this server is a part of. N)rVrrOrr )r&rrrrrrrrr"publish_server_description_changedYs    z2_EventListeners.publish_server_description_changedc Cr)zPublish a TopologyOpenedEvent to all topology listeners. :Parameters: - `topology_id`: A unique identifier for the topology this server is a part of. N)rLr rMrr r&rrrrrrpublish_topology_openednrz'_EventListeners.publish_topology_openedc Cr)zPublish a TopologyClosedEvent to all topology listeners. :Parameters: - `topology_id`: A unique identifier for the topology this server is a part of. N)rPr rQrr r*rrrpublish_topology_closed|rz'_EventListeners.publish_topology_closedc CBt|||}|jD]}z||Wq tytYq wdS)aIPublish a TopologyDescriptionChangedEvent to all topology listeners. :Parameters: - `previous_description`: The previous topology description. - `new_description`: The new topology description. - `topology_id`: A unique identifier for the topology this server is a part of. N)rNr rOrr )r&rrrrrrrr$publish_topology_description_changeds   z4_EventListeners.publish_topology_description_changedc Cr$)z:Publish a :class:`PoolCreatedEvent` to all pool listeners.N)r/r r0rr )r&rrrrrrrpublish_pool_created    z$_EventListeners.publish_pool_createdc Cr)z8Publish a :class:`PoolReadyEvent` to all pool listeners.N)r2r r3rr r&rrrrrrpublish_pool_ready   z"_EventListeners.publish_pool_readyc Cr$)z:Publish a :class:`PoolClearedEvent` to all pool listeners.N)r4r r5rr )r&rrorrrrrpublish_pool_clearedr0z$_EventListeners.publish_pool_clearedc Cr)z9Publish a :class:`PoolClosedEvent` to all pool listeners.N)r6r r7rr r1rrrpublish_pool_closedr3z#_EventListeners.publish_pool_closedc Cr$)zWPublish a :class:`ConnectionCreatedEvent` to all connection listeners. N)r8r r9rr r&rrmrrrrrpublish_connection_created    z*_EventListeners.publish_connection_createdc Cr$)zDPublish a :class:`ConnectionReadyEvent` to all connection listeners.N)r:r r;rr r6rrrpublish_connection_readyr0z(_EventListeners.publish_connection_readyc Cr-)zVPublish a :class:`ConnectionClosedEvent` to all connection listeners. N)r=r r>rr )r&rrmrrrrrrpublish_connection_closeds    z)_EventListeners.publish_connection_closedc Cr)z_Publish a :class:`ConnectionCheckOutStartedEvent` to all connection listeners. N)r?r r@rr r1rrr$publish_connection_check_out_starteds   z4_EventListeners.publish_connection_check_out_startedc Cr$)z^Publish a :class:`ConnectionCheckOutFailedEvent` to all connection listeners. N)rAr rBrr )r&rrrrrrr#publish_connection_check_out_failedr8z3_EventListeners.publish_connection_check_out_failedc Cr$)zZPublish a :class:`ConnectionCheckedOutEvent` to all connection listeners. N)rCr rDrr r6rrrpublish_connection_checked_outr8z._EventListeners.publish_connection_checked_outc Cr$)zYPublish a :class:`ConnectionCheckedInEvent` to all connection listeners. N)rEr rFrr r6rrrpublish_connection_checked_inr8z-_EventListeners.publish_connection_checked_in)NN)NNF)#rrrrrvr|rrrrrrrrrrr!r#r&r(r)r+r,r.r/r2r4r5r7r9r:r;r<r=r>rrrrrasV      ! . "         r)Grr collectionsrrtypingrrrrZ bson.objectidrZ pymongo.hellor r Zpymongo.helpersr Zpymongo.typingsr r Zpymongo.server_descriptionrZpymongo.topology_descriptionrrrcobjectrrr.rGrKrTrZrbresetrrirjr r)r+rr/r2r4r6rrrr8r:r=r?rArCrErrVrUrWrrNrLrPrrHrIrJrrrrrs~)      "!! 0;99  $%'  '   ./