Class: OneApm::Support::EventListener

Inherits:
Object
  • Object
show all
Defined in:
lib/one_apm/support/event/event_listener.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEventListener

Returns a new instance of EventListener.



8
9
10
11
# File 'lib/one_apm/support/event/event_listener.rb', line 8

def initialize
  @events = {}
  @runaway_threshold = 100
end

Instance Attribute Details

#runaway_thresholdObject

Returns the value of attribute runaway_threshold.



6
7
8
# File 'lib/one_apm/support/event/event_listener.rb', line 6

def runaway_threshold
  @runaway_threshold
end

Instance Method Details

#check_for_runaway_subscriptions(event) ⇒ Object



19
20
21
22
# File 'lib/one_apm/support/event/event_listener.rb', line 19

def check_for_runaway_subscriptions(event)
  count = @events[event].size
  OneApm::Manager.logger.debug("Run-away event subscription on #{event}? Subscribed #{count}") if count > @runaway_threshold
end

#clearObject



24
25
26
# File 'lib/one_apm/support/event/event_listener.rb', line 24

def clear
  @events.clear
end

#notify(event, *args) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/one_apm/support/event/event_listener.rb', line 28

def notify(event, *args)
  return unless @events.has_key?(event)

  @events[event].each do |handler|
    begin
      handler.call(*args)
    rescue => err
      OneApm::Manager.logger.debug("Failure during notify for #{event}", err)
    end
  end
end

#subscribe(event, &handler) ⇒ Object



13
14
15
16
17
# File 'lib/one_apm/support/event/event_listener.rb', line 13

def subscribe(event, &handler)
  @events[event] ||= []
  @events[event] << handler
  check_for_runaway_subscriptions(event)
end