Module: Isimud::EventObserver
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#activate_observer(client = isimud_client) ⇒ Object
Activate the queues for an observer.
-
#deactivate_observer(client = isimud_client) ⇒ Object
Deactivate the queues for an observer.
-
#enable_listener? ⇒ Boolean
Returns true if this instance is enabled for listening to events.
- #event_queue_name ⇒ Object
-
#handle_event(event) ⇒ Object
Event handling hook.
- #isimud_client ⇒ Object
-
#observe_events(client) ⇒ Object
Create or attach to a queue on the specified exchange.
-
#observed_exchange ⇒ Object
Exchange used for listening to events.
-
#routing_keys ⇒ Object
Routing keys that are bound to the event queue.
Methods included from Logging
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) (: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) (:update) end |
#enable_listener? ⇒ Boolean
Returns true if this instance is enabled for listening to events. Override in your subclass.
44 45 46 |
# File 'lib/isimud/event_observer.rb', line 44 def enable_listener? true end |
#event_queue_name ⇒ Object
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_client ⇒ Object
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.
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 || event = Event.parse() handle_event(event) end end |
#observed_exchange ⇒ Object
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_keys ⇒ Object
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 |