1

I have a RabbitMQ with bunny working using consumer.rb and publisher.rb. If I run

    ruby consumer.rb

and then

    publisher.rb 

I get data being published (data coming from another class). Data prints like this on ruby consumer.rb when I run publisher.rb:

Test New Data
1142.5186392493372

I need these two scripts run constantly so publisher keep pushing the data (@devise.value) through and i see the above output continuously. I have tried making a new server file using Daemon and run ruby server.rb start. But this does keep running publisher.rb but not constantly so it publishes the data. Also how can I make this RabbitMQ ruby app publish data to another web based rails app?

publisher.rb

require 'bunny' require 'daemons'

class BunnyPublisher

def connection
    conn = Bunny.new
    conn.start
end 

def channel
    connection.create_channel
end

def q
    channel.queue("que")
end

def exchange
    channel.default_exchange
end

def publish(data, q)
    exchange.publish(data, routing_key: "que") 

    sleep(5)

    connection.close
end

server.rb

require 'daemons'

Daemons.run('publisher.rb')

Thanks

Pav
  • 19
  • 1
  • 4
  • Right, I added a while loop to make publisher go on until stopped. i = 0 while i < 1 data = { json format } message = data.to_json; exchange.publish(message, routing_key: "q", persistent: true) sleep(0.5) end – Pav Feb 14 '20 at 08:58
  • Is the problem solved then? Maybe update your question to let us know if there are other things you would like help with :) – cesartalves Feb 14 '20 at 20:56

1 Answers1

0

No need for Deamons. I changed Publisher class code in the original question to the current one. And I added a while loop as mentioned.

ruby file running publisher:

i = 0
while i < 1
data = { 
            weight: @devise.value, 
            pressure: @devise.value
        }
    m = data.to_json;
    @pub.publish(m, routing_key: @q) // @pub: Publisher class
end

in Publisher class:

def publish(data, q)
    exchange.publish(data, routing_key: "queue_name") //exchange another method

    sleep(5) //Publish every 5 seconds

    connection.close
end
Pav
  • 19
  • 1
  • 4