module Isimud::EventObserver

Public Instance Methods

activate_observer(client = isimud_client) click to toggle source

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_observer(client = isimud_client) click to toggle source

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
enable_listener?() click to toggle source

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
event_queue_name() click to toggle source
# File lib/isimud/event_observer.rb, line 68
def event_queue_name
  self.class.event_queue_name(id)
end
handle_event(event) click to toggle source

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
isimud_client() click to toggle source
# File lib/isimud/event_observer.rb, line 72
def isimud_client
  Isimud.client
end
observe_events(client) click to toggle source

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
observed_exchange() click to toggle source

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() click to toggle source

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

create_queue(client = isimud_client) click to toggle source
# 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
delete_queue(client = isimud_client) click to toggle source
# File lib/isimud/event_observer.rb, line 105
def delete_queue(client = isimud_client)
  client.delete_queue(event_queue_name)
end
set_routing_keys() click to toggle source
# File lib/isimud/event_observer.rb, line 109
def set_routing_keys
  self.exchange_routing_keys = routing_keys
end
update_queue() click to toggle source
# File lib/isimud/event_observer.rb, line 100
def update_queue
  delete_queue
  create_queue if enable_listener? && exchange_routing_keys.any?
end