我有一个 Rails 应用程序,它使用 Resque 进行后台作业。这在本地工作正常,但在部署到 Heroku 后我收到连接错误:
Redis::CannotConnectError (Error connecting to Redis on localhost:6379 (Errno::ECONNREFUSED)):
我看到它试图连接到本地主机,这是不正确的。我正在使用 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
。这里出了什么问题?
最佳答案
似乎在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
关于ruby-on-rails - Heroku 上的 Redis 连接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37276690/