module Isimud::EventObserver
Public Instance Methods
Activate the queues for an observer. This will create the observer queue and send an update message on the instance, which will trigger EventListener
instances to set up consumers. This is useful for situations when an observer is to be made active without an update.
# File lib/isimud/event_observer.rb, line 79 def activate_observer(client = isimud_client) create_queue(client) isimud_send_action_message(:update) end
Deactivate the queues for an observer. This will destroy the observer queue and send an update message on the instance, which will trigger EventListener
instances to cancel consumers. Note that enable_listener? should resolve to false in order for the EventListener
to cancel corresponding event consumers.
# File lib/isimud/event_observer.rb, line 87 def deactivate_observer(client = isimud_client) delete_queue(client) isimud_send_action_message(:update) end
Returns true if this instance is enabled for listening to events. Override in your subclass.
# File lib/isimud/event_observer.rb, line 44 def enable_listener? true end
# File lib/isimud/event_observer.rb, line 68 def event_queue_name self.class.event_queue_name(id) end
Event
handling hook. Override in your class.
# File lib/isimud/event_observer.rb, line 34 def handle_event(event) logger.warn("Isimud::EventObserver#handle_event not implemented for #{event_queue_name}") end
# File lib/isimud/event_observer.rb, line 72 def isimud_client Isimud.client end
Create or attach to a queue on the specified exchange. When an event message that matches the observer's routing keys is received, parse the event and call handle_event
on same. @param [Isimud::Client] client client instance @return queue or consumer object @see BunnyClient#subscribe
@see TestClient#subscribe
# File lib/isimud/event_observer.rb, line 59 def observe_events(client) return unless enable_listener? queue = create_queue(client) client.subscribe(queue) do |message| event = Event.parse(message) handle_event(event) end end
Exchange used for listening to events. Override in your subclass if you want to specify an alternative exchange.
# File lib/isimud/event_observer.rb, line 49 def observed_exchange nil end
Routing keys that are bound to the event queue. Override in your subclass
# File lib/isimud/event_observer.rb, line 39 def routing_keys [] end
Private Instance Methods
# File lib/isimud/event_observer.rb, line 94 def create_queue(client = isimud_client) exchange = observed_exchange || Isimud.events_exchange log "Isimud::EventObserver: creating queue #{event_queue_name} on exchange #{exchange} with bindings [#{exchange_routing_keys.join(',')}]" client.create_queue(event_queue_name, exchange, routing_keys: exchange_routing_keys) end
# File lib/isimud/event_observer.rb, line 105 def delete_queue(client = isimud_client) client.delete_queue(event_queue_name) end
# File lib/isimud/event_observer.rb, line 109 def set_routing_keys self.exchange_routing_keys = routing_keys end
# File lib/isimud/event_observer.rb, line 100 def update_queue delete_queue create_queue if enable_listener? && exchange_routing_keys.any? end