Class: EventHub::ActorPublisher

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

Overview

Publisher class

Instance Method Summary collapse

Methods included from Helper

#create_bunny_connection, #get_name_from_class, #now_stamp, #stringify_keys

Constructor Details

#initializeActorPublisher



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

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

Instance Method Details

#cleanupObject



46
47
48
49
# File 'lib/eventhub/actor_publisher.rb', line 46

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

#publish(args = {}) ⇒ Object



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
40
41
42
43
44
# File 'lib/eventhub/actor_publisher.rb', line 15

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)

  publish_options = {persistent: true}
  correlation_id = args[:correlation_id] || CorrelationId.current
  publish_options[:correlation_id] = correlation_id if correlation_id

  exchange.publish(message, publish_options)
  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