0

I cannot figure out how to subscribe to my RabbitMQ locally(it worked when I used CloudAMPQ.) I suspect the problem is that my SendPriceJob isn't connected to the right connection/channel/exchange but I'm not certain.

I have a worker that fetches data from an API every one minute.

class FetchPriceJob
  @queue = :update_price

  def self.perform
    # Do some stuff

    FetchPriceJob.new.publish(response.to_json)
  end

  def publish(data)
    channel.default_exchange.publish(data, routing_key: queue.name)
    connection.close
  end

  def connection
    @conn ||= begin
      conn = Bunny.new(host: "localhost", vhost: "/", user: "guest", password: "guest")
      conn.start
    end
  end

  def channel
    @channel ||= connection.create_channel
  end

  def queue
    @queue ||= channel.queue('current_prices')
  end
end

I have another worker that opens a connection and listens.

module SendPriceJob
  @queue = :price_serve

  def self.perform
    conn = Bunny.new(host: "localhost", vhost: "/", user: "guest", password: "guest")
    conn.start
    ch  = conn.create_channel
    x   = ch.default_exchange
    q   = ch.queue('current_prices')
    begin
      q.subscribe(block: true) do |_, _, body|
        ActionCable.server.broadcast 'prices', body
      end
    rescue Interrupt => _
      ch.close
      conn.close
    end
  end
end

These two workers are kicked off by a process manager.

# Procfile
elastic: elasticsearch
redis: redis-server
web: rails server -p 3000
send_worker: QUEUE=price_serve rake resque:work
fetch_worker: QUEUE=update_price rake resque:work
scheduler: rake resque:scheduler

I'm running my RabbitMQ server: http://prntscr.com/i6rcwv. I'm successfully queueing messages on the connection/channel/exchange: http://prntscr.com/i6rd7q. From my logs it seems that the scheduler I'm running works, as does the producer: http://prntscr.com/i6rdxf.

This is my first time working with message queues so I may have done something completely wrong. I feel like I should be close though because it was working with CloudAMQP. The only difference was that the Bunny.new was configured to connect to an external API.

halfer
  • 19,824
  • 17
  • 99
  • 186
PrimeTimeTran
  • 1,807
  • 2
  • 17
  • 29
  • (Please don't use any image host other than the official Stack Overflow image CDN. There is a commercial relationship between SO and Imgur, which will guarantee that those links won't break in the future. External entities are not protected in the same way, and we often have to edit, close or delete questions where these breaks happen). – halfer Jun 11 '18 at 21:40

2 Answers2

2

You can use the sneakers or pwwka gem for background processing of RabbitMQ messages. Saves you a lot of the bottle neck involved in subscribing to the message queues.

Adim
  • 1,716
  • 12
  • 13
1

In this method:

def publish(data)
  channel.default_exchange.publish(data, routing_key: queue.name)
  connection.close
end

Can you try this instead:

queue.publish(data, routing_key: queue.name)

Documentation: http://rubybunny.info/articles/exchanges.html#default_exchange

Alex A
  • 156
  • 1
  • 10
  • Also a suggestion, create a small trial script, and try to establish a connection and publish a message with your local rabbitMQ, it might be misconfigured. Try the link in my answer. – Alex A Jan 28 '18 at 18:59