Class: Mercury::Sync

Inherits:
Object
  • Object
show all
Defined in:
lib/mercury/sync.rb

Class Method Summary collapse

Class Method Details

.publish(source_name, msg, tag: '', headers: {}, amqp_opts: {}, wait_for_publisher_confirms: true) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/mercury/sync.rb', line 7

def publish(source_name, msg, tag: '', headers: {}, amqp_opts: {}, wait_for_publisher_confirms: true)
  conn = Bunny.new(amqp_opts)
  conn.start
  ch = conn.create_channel

  ch.confirm_select if wait_for_publisher_confirms # see http://rubybunny.info/articles/extensions.html and Mercury#enable_publisher_confirms
  ex = ch.topic(source_name, Mercury.source_opts)
  ex.publish(WireSerializer.new.write(msg), **Mercury.publish_opts(tag, headers))
  if wait_for_publisher_confirms
    ch.wait_for_confirms or raise 'failed to confirm publication'
  end
ensure
  conn.close
end