123456789_123456789_123456789_123456789_123456789_

Module: Mongo::Monitoring::Subscribable

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Extended In:
Included In:
Defined in: lib/mongo/monitoring.rb

Overview

Contains subscription methods common between monitoring and global event subscriptions.

Since:

  • 2.6.0

Instance Method Summary

Instance Method Details

#subscribe(topic, subscriber)

Note:

It is possible to subscribe the same listener to the same topic

Subscribe a listener to an event 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.

Examples:

Subscribe to the topic.

monitoring.subscribe(QUERY, subscriber)

Subscribe to the topic globally.

Monitoring::Global.subscribe(QUERY, subscriber)

Parameters:

  • topic (String)

    The event topic.

  • subscriber (Object)

    The subscriber to handle the event.

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/monitoring.rb', line 105

def subscribe(topic, subscriber)
  subscribers_for(topic).push(subscriber)
end

#subscribersHash<String, Object>

Get all the subscribers.

Examples:

Get all the subscribers.

monitoring.subscribers

Get all the global subscribers.

Mongo::Monitoring::Global.subscribers

Returns:

  • (Hash<String, Object>)

    The subscribers.

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/monitoring.rb', line 157

def subscribers
  @subscribers ||= {}
end

#subscribers?(topic) ⇒ true, false

Determine if there are any subscribers for a particular event.

Examples:

Are there subscribers?

monitoring.subscribers?(COMMAND)

Are there global subscribers?

Mongo::Monitoring::Global.subscribers?(COMMAND)

Parameters:

  • topic (String)

    The event topic.

Returns:

  • (true, false)

    If there are subscribers for the topic.

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/monitoring.rb', line 174

def subscribers?(topic)
  !subscribers_for(topic).empty?
end

#subscribers_for(topic) (private)

Since:

  • 2.6.0

[ GitHub ]

  
# File 'lib/mongo/monitoring.rb', line 180

def subscribers_for(topic)
  subscribers[topic] ||= []
end

#unsubscribe(topic, subscriber)

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.

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.

Examples:

Unsubscribe from the topic.

monitoring.unsubscribe(QUERY, subscriber)

Unsubscribe from the topic globally.

Mongo::Monitoring::Global.unsubscribe(QUERY, subscriber)

Parameters:

  • topic (String)

    The event topic.

  • subscriber (Object)

    The subscriber to be unsubscribed.

Since:

  • 2.6.0

[ GitHub ]

  
# File 'lib/mongo/monitoring.rb', line 138

def unsubscribe(topic, subscriber)
  subs = subscribers_for(topic)
  index = subs.index(subscriber)
  if index
    subs.delete_at(index)
  end
end