我遇到意外和重大的问题,试图让在Unicorn下运行的Rails应用连接到受密码保护的Redis服务器。Resque没有采用Redis配置设置
在命令行上使用bundle exec rails c production
,我可以通过Resque.redis发出命令。但是,在Unicorn分支下,我的配置似乎正在丢失。
使用非密码保护的Redis服务器Just Works。但是,我打算在Redis服务器所在的其他服务器上运行工作,因此我需要密码保护。
我也成功地使用密码保护(使用相同的技术),但使用乘客而不是独角兽。
我有以下设置:
# config/resque.yml
development: localhost:6379
test: localhost:6379
production: redis://user:[email protected]:6379
。
# config/initializers/redis.rb
rails_root = ENV['RAILS_ROOT'] || File.dirname(__FILE__) + '/../..'
rails_env = ENV['RAILS_ENV'] || 'development'
$resque_config = YAML.load_file(rails_root + '/config/resque.yml')
uri = URI.parse($resque_config[rails_env])
Resque.redis = Redis.new(host: uri.host, port: uri.port, password: uri.password)
。
# unicorn.rb bootup file
preload_app true
before_fork do |server, worker|
Redis.current.quit
end
after_fork do |server, worker|
Redis.current.quit
end
。
我试过了(简化,将配置保存在全局中),甚至在after_fork钩子中将连接字符串硬编码为'Resque.redis =“redis:// user:[email protected]: 6379“'但唉,没成功。 Resque Worker工作,Rails web应用程序无法使用它。如果我更改了端口,工作人员就会跟踪这一点,而应用不会。 –
如果您在生产中启动导轨控制台,“Resque.redis”的输出是什么? –
从控制台,一切都按预期工作,Resque.redis.info(例如)从服务器返回信息。如果我使用无效的密码,我会得到一个“密码错误”的样式错误,所以我知道它在控制台上正确连接。即使运行'Resque.redis.quit'后跟相同的'Resque.redis =“url”'命令也可以在控制台上运行。 –