我在这个应用程序中使用过的 Gem
gem 'redis', '~> 3.0'
gem 'sidekiq'
gem 'bunny'
这是从队列中获取消息作为 rake 任务的消费者部分。
task :do_consumer => :environment do
connection = Bunny.new(ENV['CLOUDAMQP_URL'])
connection.start # start a communication session with the amqp server
channel = connection.channel()
queue = channel.queue('order-queue', durable: true)
puts ' [*] Waiting for messages. To exit press CTRL+C'
queue.subscribe(manual_ack: true, block: true) do |delivery_info,
properties, payload|
puts " [x] Received #{payload}"
puts " [x] Done"
channel.ack(delivery_info.delivery_tag, false)
# Call worker to do task
callSidekiqWorker.perform_async(payload)
end
end
档案
web: bundle exec puma -C config/puma.rb
worker: bundle exec sidekiq -e production
worker: bundle exec rake do_consumer
我在 heroku 中添加了redistogo插件,并将 REDIS_PROVIDER 配置为 ENV 变量。
这里的问题在本地运行良好,但是在我推送到 heroku 后,我在日志中收到此错误:
[x] Received {"pos_items_cache_id":816,"process_state_id":320,"location_id":604}
14 Sep 2018 16:57:57.439106 <190>1 2018-09-14T11:27:56.885838+00:00 app worker.1 - - [x] Done
14 Sep 2018 16:57:57.509635 <190>1 2018-09-14T11:27:56.885844+00:00 app worker.1 - - E, [2018-09-14T11:27:56.885527 4] ERROR --
<Bunny::Session:0x29beb80 vmeksylf@chimpanzee.rmq.cloudamqp.com:5672, vhost=vmeksylf, addresses=[chimpanzee.rmq.cloudamqp.com:5672]>: Uncaught exception from consumer
<Bunny::Consumer:21834760 @channel_id=1 @queue=order-queue> @consumer_tag=bunny-1536910387000-84532836151>:
<Redis::CannotConnectError: Error connecting to Redis on 127.0.0.1:6379 (Errno::ECONNREFUSED)> @ /app/vendor/bundle/ruby/2.4.0/gems/redis-3.3.5/lib/redis/client.rb:345:in `rescue in establish_connection'
我在heroku中做了什么错误的配置吗?我也认为这个 rake 任务并不总是在听。