Class: SongkickQueue::Producer
- Inherits:
-
Object
- Object
- SongkickQueue::Producer
- Defined in:
- lib/songkick_queue/producer.rb
Instance Attribute Summary collapse
-
#reconnect_attempts ⇒ Object
Returns the value of attribute reconnect_attempts.
Instance Method Summary collapse
-
#initialize ⇒ Producer
constructor
A new instance of Producer.
-
#publish(queue_name, payload, options = {}) ⇒ Bunny::Exchange
Serializes the given message and publishes it to the default RabbitMQ exchange.
Constructor Details
Instance Attribute Details
#reconnect_attempts ⇒ Object
Returns the value of attribute reconnect_attempts.
3 4 5 |
# File 'lib/songkick_queue/producer.rb', line 3 def reconnect_attempts @reconnect_attempts end |
Instance Method Details
#publish(queue_name, payload, options = {}) ⇒ Bunny::Exchange
Serializes the given message and publishes it to the default RabbitMQ exchange
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/songkick_queue/producer.rb', line 21 def publish(queue_name, payload, = {}) = .fetch(:message_id) { SecureRandom.hex(6) } produced_at = .fetch(:produced_at) { Time.now.utc.iso8601 } = { message_id: , produced_at: produced_at, payload: payload } = JSON.generate() exchange = client.default_exchange = { queue_name: String(queue_name), message_id: , produced_at: produced_at, } ActiveSupport::Notifications.instrument('produce_message.songkick_queue', ) do exchange.publish(, routing_key: String(queue_name)) end self.reconnect_attempts = 0 logger.info "Published message #{} to '#{queue_name}' at #{produced_at}" exchange rescue Bunny::ConnectionClosedError self.reconnect_attempts += 1 if (reconnect_attempts > config.max_reconnect_attempts) fail TooManyReconnectAttemptsError, "Attempted to reconnect more than " + "#{config.max_reconnect_attempts} times" end logger.info "Attempting to reconnect to RabbitMQ, attempt #{reconnect_attempts} " + "of #{config.max_reconnect_attempts}" wait_for_bunny_session_to_reconnect retry end |