Module: Isimud::EventObserver

Extended by:
ActiveSupport::Concern
Includes:
Logging
Defined in:
lib/isimud/event_observer.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Methods included from Logging

#log, #logger

Instance Method Details

#activate_observer(client = isimud_client) ⇒ Object

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.



79
80
81
82
# 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) ⇒ Object

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.



87
88
89
90
# 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?Boolean

Returns true if this instance is enabled for listening to events. Override in your subclass.

Returns:

  • (Boolean)


44
45
46
# File 'lib/isimud/event_observer.rb', line 44

def enable_listener?
  true
end

#event_queue_nameObject



68
69
70
# File 'lib/isimud/event_observer.rb', line 68

def event_queue_name
  self.class.event_queue_name(id)
end

#handle_event(event) ⇒ Object

Event handling hook. Override in your class.



34
35
36
# 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_clientObject



72
73
74
# File 'lib/isimud/event_observer.rb', line 72

def isimud_client
  Isimud.client
end

#observe_events(client) ⇒ Object

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.

Parameters:

Returns:

  • queue or consumer object

See Also:



59
60
61
62
63
64
65
66
# 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_exchangeObject

Exchange used for listening to events. Override in your subclass if you want to specify an alternative exchange.



49
50
51
# File 'lib/isimud/event_observer.rb', line 49

def observed_exchange
  nil
end

#routing_keysObject

Routing keys that are bound to the event queue. Override in your subclass



39
40
41
# File 'lib/isimud/event_observer.rb', line 39

def routing_keys
  []
end