Ruby 在 Rails - Puma on_worker_boot 连接到 redis

Ruby on Rails - Puma on_worker_boot connect with redis

我正在尝试使用 Puma 连接 heroku 上的 redis,但我一直 运行 进入错误

Read error: #<Redis::CannotConnectError: Error connecting to Redis on 127.0.0.1:6379 (Errno::ECONNREFUSED)>

我搜索了 Whosebug,但 none 的答案似乎解决了我的问题。

我的puma.rb配置

workers Integer(ENV['WEB_CONCURRENCY'] || 2)
threads_count = Integer(ENV['MAX_THREADS'] || 5)
threads threads_count, threads_count

preload_app!

rackup      DefaultRackup
port        ENV['PORT']     || 3000
environment ENV['RACK_ENV'] || 'development'

on_worker_boot do
  ActiveRecord::Base.establish_connection

  Redis.current.client.reconnect
  $redis = Redis.current
end

我的 redis.rb 初始化器

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

我检查了 ENV["REDISCLOUD_URL"]redis://rediscloud:blank@blank.eu-west-1-1.1.ec2.garantiadata.com:port

我不知道我做错了什么,这对我来说似乎都是正确的

我必须设置配置变量 heroku config:set REDIS_PROVIDER=REDISCLOUD_URL

Puma.rb

workers Integer(ENV['WEB_CONCURRENCY'] || 2)
threads_count = Integer(ENV['MAX_THREADS'] || 5)
threads threads_count, threads_count

preload_app!

rackup      DefaultRackup
port        ENV['PORT']     || 3000
environment ENV['RACK_ENV'] || 'development'

on_worker_boot do
  ActiveRecord::Base.establish_connection

  if ENV["REDISCLOUD_URL"]
    uri = URI.parse(ENV["REDISCLOUD_URL"])
    Redis.current = Redis.new(host: uri.host, port: uri.port, password: uri.password)
    $redis = Redis.current
  else
    Redis.current.quit
  end
end

redis.rb

if ENV["REDISCLOUD_URL"]
  uri = URI.parse(ENV["REDISCLOUD_URL"])
  $redis = Redis.new(host: uri.host, port: uri.port, password: uri.password)
end