Resque does not collect Redis configuration settings

I am having unexpected and significant problems trying to get a Rails application running under Unicorn to connect to a password protected Redis server.

Using bundle exec rails c production on the command line, I can issue commands through Resque.redis. However, it seems that my configuration is lost when it forks under Unicorn.

Using a Redis server without password protection Just Works. However, I intend to run workers on other servers than where the Redis server lives, so I need this to be password protected.

I also had success in using password protected (using the same method), but using Passenger rather than Unicorn.

I have the following setup:

 # config/resque.yml development: localhost:6379 test: localhost:6379 production: redis://user: PASSWORD@oak.isc.org :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 

.

+6
source share
5 answers

UPDATED A completely different idea based on the @lmarlow comment on the resque issue .

I bet it breaks down wherever you have Redis ~> 3 (I mean the ruby ​​client version, not the server version).

Starting with this entry, Resque requires Redis ~> 2, but does not indicate what is in its gemspec. So you need help by adding this to your Gemfile:

 gem 'redis', '~>2' # until a new version of resque comes out gem 'resque' 

Also, make sure that the communication device is used universally. Otherwise, if your system has a new version of the Redis pearl, it will be used, and Resque will fail as before.

Finally, a cosmetic note ... you can simplify the configuration:

 # config/initializers/redis.rb $resque_redis_url = uris_per_environment[rails_env] # note no URI.parse Resque.redis = $resque_redis_url 

and then

 # unicorn.rb bootup file after_fork do |server, worker| Resque.redis = $resque_redis_url end 
+4
source

Well, for the sake of other people who can solve this problem, I solved it for myself at least

The main problem is calling Redis.new other places in the code, for example. in the geocoding setting or unicorn configuration file.

just make sure that every time you call Redis initialization, you pass the appropriate values ​​for example something like

 REDIS = Redis.connect(:url => ENV['REDISTOGO_URL']) 

and you should never

 Redis.new 

since by default it will be set to localhost and the default port

+6
source

This was useful to me:

source: https://github.com/redis/redis-rb/blob/master/examples/unicorn/unicorn.rb

 require "redis" worker_processes 3 # If you set the connection to Redis *before* forking, # you will cause forks to share a file descriptor. # # This causes a concurrency problem by which one fork # can read or write to the socket while others are # performing other operations. # # Most likely you'll be getting ProtocolError exceptions # mentioning a wrong initial byte in the reply. # # Thus we need to connect to Redis after forking the # worker processes. after_fork do |server, worker| Redis.current.quit end 
+2
source

The unicorn configuration worked for me: fooobar.com/questions/781830 / ...

 before_fork do |server, worker| if defined?(Resque) Resque.redis.quit Rails.logger.info("Disconnected from Redis") end end after_fork do |server, worker| if defined?(Resque) Resque.redis = REDIS_WORKER Rails.logger.info("Connected to Redis") end end 
+2
source

I think the problem was in Resque-web. Its configuration file, they fixed it now. In version 0.0.11, they also mention this in a comment: https://github.com/resque/resque-web/blob/master/config/initializers/resque_config.rb#L3

Previously, their file looked like this: https://github.com/resque/resque-web/blob/v0.0.9/config/initializers/resque_config.rb

And if for some reason you cannot update, instead try setting the env variable RAILS_RESQUE_REDIS=<host>:<port> instead, since the Initializers load after it tries to connect redis (and does not work).

0
source

Source: https://habr.com/ru/post/917484/


All Articles