Heroku上的Redis连接错误

时间:2016-05-17 12:47:54

标签: ruby-on-rails heroku redis resque

我有一个Rails应用程序,它使用Resque作为后台作业。这在本地工作正常,但在部署到Heroku后,我收到连接错误:

Redis::CannotConnectError (Error connecting to Redis on localhost:6379 (Errno::ECONNREFUSED)):

我看到它试图连接到localhost,这是不正确的。我使用了Heroku Redis :: Redis加载项,并添加了redis gem。这就是initializers.redis.rb的样子:

$redis = Redis.new(url: ENV["REDIS_URL"])

这是我的Procfile

web: bundle exec puma -C config/puma.rb
resque: env TERM_CHILD=1 bundle exec rake resque:work QUEUE=* COUNT=1

在配置变量REDIS_URL中添加。这里出了什么问题?

1 个答案:

答案 0 :(得分:0)

似乎在Heroku上使用Puma或Unicorn时,您还需要将它添加到启动过程中。我使用的是Puma,所以我将其添加到config/puma.rb

on_worker_boot do
  # ...
  if defined?(Resque)
     Resque.redis = ENV["REDIS_URL"] || "redis://127.0.0.1:6379"
  end
end

以下是Puma和Unicorn的更详细解释: https://devcenter.heroku.com/articles/deploying-rails-applications-with-the-puma-web-server https://devcenter.heroku.com/articles/rails-unicorn#caveats