module Mongo::Monitoring::Subscribable
Contains subscription methods common between monitoring and global event subscriptions.
@since 2.6.0
Public Instance Methods
Subscribe a listener to an event topic.
@note It is possible to subscribe the same listener to the same topic multiple times, in which case the listener will be invoked as many times as it is subscribed and to unsubscribe it the same number of unsubscribe calls will be needed.
@example Subscribe to the topic.
monitoring.subscribe(QUERY, subscriber)
@example Subscribe to the topic globally.
Monitoring::Global.subscribe(QUERY, subscriber)
@param [ String ] topic The event topic. @param [ Object ] subscriber The subscriber to handle the event.
@since 2.1.0
# File lib/mongo/monitoring.rb, line 102 def subscribe(topic, subscriber) subscribers_for(topic).push(subscriber) end
Get all the subscribers.
@example Get all the subscribers.
monitoring.subscribers
@example Get all the global subscribers.
Mongo::Monitoring::Global.subscribers
@return [ Hash<String, Object> ] The subscribers.
@since 2.1.0
# File lib/mongo/monitoring.rb, line 154 def subscribers @subscribers ||= {} end
Determine if there are any subscribers for a particular event.
@example Are there subscribers?
monitoring.subscribers?(COMMAND)
@example Are there global subscribers?
Mongo::Monitoring::Global.subscribers?(COMMAND)
@param [ String ] topic The event topic.
@return [ true, false ] If there are subscribers for the topic.
@since 2.1.0
# File lib/mongo/monitoring.rb, line 171 def subscribers?(topic) !subscribers_for(topic).empty? end
Unsubscribe a listener from an event topic.
If the listener was subscribed to the event topic multiple times, this call removes a single subscription.
If the listener was not subscribed to the topic, this operation is a no-op and no exceptions are raised.
@note Global
subscriber registry is separate from per-client
subscriber registry. The same subscriber can be subscribed to events from a particular client as well as globally; unsubscribing globally will not unsubscribe that subscriber from the client it was explicitly subscribed to.
@note Currently the list of global subscribers is copied into
a client whenever the client is created. Thus unsubscribing a subscriber globally has no effect for existing clients - they will continue sending events to the unsubscribed subscriber.
@example Unsubscribe from the topic.
monitoring.unsubscribe(QUERY, subscriber)
@example Unsubscribe from the topic globally.
Mongo::Monitoring::Global.unsubscribe(QUERY, subscriber)
@param [ String ] topic The event topic. @param [ Object ] subscriber The subscriber to be unsubscribed.
@since 2.6.0
# File lib/mongo/monitoring.rb, line 135 def unsubscribe(topic, subscriber) subs = subscribers_for(topic) index = subs.index(subscriber) if index subs.delete_at(index) end end
Private Instance Methods
# File lib/mongo/monitoring.rb, line 177 def subscribers_for(topic) subscribers[topic] ||= [] end