Class: Flu::EventPublisher

Inherits:
Object
  • Object
show all
Defined in:
lib/flu-rails/event_publisher.rb

Direct Known Subclasses

Dummy::InMemoryEventPublisher

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ EventPublisher

Returns a new instance of EventPublisher.



7
8
9
10
# File 'lib/flu-rails/event_publisher.rb', line 7

def initialize(configuration)
  @logger        = configuration.logger
  @configuration = configuration
end

Instance Method Details

#connectObject



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/flu-rails/event_publisher.rb', line 19

def connect
  connected = false
  while !connected
    begin
      connect_to_exchange
      connected = true
    rescue Bunny::TCPConnectionFailedForAllHosts
      @logger.warn("RabbitMQ connection failed, try again in 1 second.")
      sleep 1
    end
  end
end

#publish(event, persistent = true) ⇒ Object



12
13
14
15
16
17
# File 'lib/flu-rails/event_publisher.rb', line 12

def publish(event, persistent=true)
  routing_key = event.to_routing_key
  @logger.debug("Publishing event with id '#{event.id}' with routing key: #{routing_key}")
  @exchange.publish(event.to_json, routing_key: routing_key, persistent: persistent)
  @logger.debug("Event published.")
end