Module: Rabbit::Publishing

Extended by:
Publishing
Included in:
Publishing
Defined in:
lib/rabbit/publishing.rb,
lib/rabbit/publishing/job.rb,
lib/rabbit/publishing/message.rb,
lib/rabbit/publishing/channels_pool.rb

Defined Under Namespace

Classes: ChannelsPool, Job, Message

Constant Summary collapse

MUTEX =
Mutex.new

Instance Method Summary collapse

Instance Method Details

#poolObject



44
45
46
# File 'lib/rabbit/publishing.rb', line 44

def pool
  MUTEX.synchronize { @pool ||= ChannelsPool.new(create_client) }
end

#publish(msg) ⇒ Object

rubocop:disable Metrics/MethodLength



13
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
40
41
42
# File 'lib/rabbit/publishing.rb', line 13

def publish(msg) # rubocop:disable Metrics/MethodLength
  return if Rabbit.config.skip_publish?

  attempt = 0
  begin
    pool.with_channel msg.confirm_select? do |ch|
      ch.basic_publish *msg.basic_publish_args

      raise MessageNotDelivered, "RabbitMQ message not delivered: #{msg}" \
        if msg.confirm_select? && !ch.wait_for_confirms

      log msg
    end
  rescue *Rabbit.config.connection_reset_exceptions => error
    attempt += 1
    if attempt <= Rabbit.config.connection_reset_max_retries
      sleep(Rabbit.config.connection_reset_timeout)
      reinitialize_channels_pool
      retry
    else
      raise error
    end
  rescue Timeout::Error
    raise MessageNotDelivered, <<~MESSAGE
      Timeout while sending message #{msg}. Possible reasons:
        - #{msg.real_exchange_name} exchange is not found
        - RabbitMQ is extremely high loaded
    MESSAGE
  end
end