class Mmtrix::Agent::EventListener
Basic mechanism for the agent instance to provide agent-wide eventing. It is intended to keep different pieces of the app decoupled from each other.
While an EventListener
could be used elsewhere, it’s strongly expected your eventing needs should be met by the agent’s instance.
Attributes
runaway_threshold[RW]
Public Class Methods
new()
click to toggle source
# File lib/mmtrix/agent/event_listener.rb, line 15 def initialize @events = {} @runaway_threshold = 100 end
Public Instance Methods
check_for_runaway_subscriptions(event)
click to toggle source
# File lib/mmtrix/agent/event_listener.rb, line 26 def check_for_runaway_subscriptions(event) count = @events[event].size Mmtrix::Agent.logger.debug("Run-away event subscription on #{event}? Subscribed #{count}") if count > @runaway_threshold end
clear()
click to toggle source
# File lib/mmtrix/agent/event_listener.rb, line 31 def clear @events.clear end
notify(event, *args)
click to toggle source
# File lib/mmtrix/agent/event_listener.rb, line 35 def notify(event, *args) return unless @events.has_key?(event) @events[event].each do |handler| begin handler.call(*args) rescue => err Mmtrix::Agent.logger.debug("Failure during notify for #{event}", err) end end end
subscribe(event, &handler)
click to toggle source
# File lib/mmtrix/agent/event_listener.rb, line 20 def subscribe(event, &handler) @events[event] ||= [] @events[event] << handler check_for_runaway_subscriptions(event) end