我遇到意外和重大的問題,試圖讓在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」'命令也可以在控制檯上運行。 –