Class: Alondra::EventRouter

Inherits:
Object
  • Object
show all
Defined in:
lib/alondra/event_router.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.listenersObject



4
5
6
# File 'lib/alondra/event_router.rb', line 4

def self.listeners
  @listeners ||= []
end

Instance Method Details

#process(event) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/alondra/event_router.rb', line 8

def process(event)
  event.channel.receive(event)

  # Event listeners callback can manipulate AR objects and so can potentially
  # block the EM reactor thread. To avoid that, we defer them to another thread.
  EM.defer do

    # Ensure the connection associated with the thread is checked in
    # after the callbacks are processed
    ActiveRecord::Base.connection_pool.with_connection do
      listening_classes = EventRouter.listeners.select do |ob|
        ob.listen_to?(event.channel_name)
      end

      listening_classes.each { |listening_class| listening_class.process(event) }
    end
  end
end