Skip to content

Commit

Permalink
[frontend] Fix crash when configuring exchange options for RabbitMQ
Browse files Browse the repository at this point in the history
Bunny, the library we use for using rabbitmq, only accepts hash keys
that are symbols. Any key that is not a symbol doesn't get applied
by bunny.

OBS was using with_indifferent_access for any hash passed to bunny
to ensure that the bunny recognizes the hash as 'symbolized'.
However with_indifferent_access creates and returns an
ActiveSupport::HashWithIndifferentAccess instance, which isn't something
bunny knows how to deal with. Therefore it still failed to
apply the options.

The solution is to use the symbolize_keys method to ensure that all
hash keys are symbols, before passing them to bunny.

Kudos goes to @coolo for debugging this.
  • Loading branch information
bgeuken committed Oct 17, 2017
1 parent 8c9e0a0 commit 475816c
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/api/lib/rabbitmq_bus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ def self.publish(event_queue_name, event_payload)
return unless CONFIG['amqp_options']
start_connection

queue = $rabbitmq_channel.queue(event_queue_name, CONFIG['amqp_queue_options'].try(:with_indifferent_access) || {})
queue = $rabbitmq_channel.queue(event_queue_name, CONFIG['amqp_queue_options'].try(:symbolize_keys) || {})
$rabbitmq_exchange.publish(event_payload, routing_key: queue.name)
end

# Start one connection, channel and exchange per rails process
# and reuse them
def self.start_connection
$rabbitmq_conn ||= Bunny.new(CONFIG['amqp_options'].with_indifferent_access)
$rabbitmq_conn ||= Bunny.new(CONFIG['amqp_options'].try(:symbolize_keys))
$rabbitmq_conn.start
$rabbitmq_channel ||= $rabbitmq_conn.create_channel
$rabbitmq_exchange = if CONFIG['amqp_exchange_name']
$rabbitmq_channel.exchange(CONFIG['amqp_exchange_name'], CONFIG['amqp_exchange_options'].try(:with_indifferent_access) || {})
$rabbitmq_channel.exchange(CONFIG['amqp_exchange_name'], CONFIG['amqp_exchange_options'].try(:symbolize_keys) || {})
else
$rabbitmq_channel.default_exchange
end
Expand Down

0 comments on commit 475816c

Please sign in to comment.