Hello.
Help me please.
I have a Publisher and Consumer via Bunny.
I run this script and everything works fine.
But there are those days when there are no messages in the queue (usually 5-6 days). Then new messages are added to the queue, but the executable script does not read them. There is no error in the logs.
What could be wrong?
Launch task:
#!/bin/bash
if ! [ -z $1 ]; then
env=$1;
else
env='development';
fi
echo == Start with RAILS_ENV = $env
bundle exec rake rabbit:subscribe RAILS_ENV=$env --trace 2>&1 >> log/consumer.log &
echo == The consumer is running!
My Task:
namespace :rabbit do
desc 'Subscribe msg'
task :subscribe => :environment do
begin
load 'app/services/external_receipt_service/atol.rb'
load 'app/services/external_receipt_service/base.rb'
Rabbitmq::Publisher.subscribe(ENV["RABBITMQ_RECEIPT_QUEUE"])
# How would you write a subscription loop when writing a long-running
# consumer, ie a ruby program with a a single thread that subscribe
# to a unique queue and processes the messages?
# prevent main thread from naturally terminating if q.subscribe(block: false)
loop{ sleep 2 }
rescue Interrupt => _
puts "Ctrl + C pressed"
rescue => e
puts e
end
end
end
My Consumer:
module RabbitMQ
class Consumer
def self.subscribe(queue)
exchange = channel.queue(queue, durable: true)
exchange.subscribe(ack: true) do |delivery_info, properties, payload|
begin
Rails.logger.debug(payload)
@channel.ack(delivery_info.delivery_tag)
rescue => e
@channel.nack(delivery_info.delivery_tag, false, true)
end
end
end
def self.channel
@channel ||= connection.create_channel
end
def self.connection
@conn = Bunny.new(ENV["RABBITMQ_SERVER"], {vhost: ENV["RABBITMQ_VHOST"], recover_from_connection_close: true})
@conn.start
end
def self.close
@conn.close
end
end
end