Class: EventHub::ActorPublisher

Inherits:
Object
  • Object
show all
Includes:
Celluloid, Helper
Defined in:
lib/eventhub/actor_publisher.rb

Overview

Heartbeat class

Instance Method Summary collapse

Methods included from Helper

#create_bunny_connection, #get_name_from_class, #now_stamp, #stringify_keys

Constructor Details

#initializeActorPublisher

Returns a new instance of ActorPublisher.



9
10
11
12
# File 'lib/eventhub/actor_publisher.rb', line 9

def initialize
  EventHub.logger.info("Publisher is starting...")
  @connection = nil
end

Instance Method Details

#cleanupObject



41
42
43
44
# File 'lib/eventhub/actor_publisher.rb', line 41

def cleanup
  EventHub.logger.info("Publisher is cleaning up...")
  @connection&.close
end

#publish(args = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/eventhub/actor_publisher.rb', line 14

def publish(args = {})
  # keep connection once established
  unless @connection
    @connection = create_bunny_connection
    @connection.start
  end

  message = args[:message]
  return if message.nil?

  exchange_name = args[:exchange_name] || EH_X_INBOUND

  channel = @connection.create_channel
  channel.confirm_select
  exchange = channel.direct(exchange_name, durable: true)

  exchange.publish(message, persistent: true)
  success = channel.wait_for_confirms

  unless success
    raise "Published message from Listener actor " \
          "has not been confirmed by the server"
  end
ensure
  channel&.close
end