Redis tries to connect to localhost on Heroku instead of REDIS_URL Redis tries to connect to localhost on Heroku instead of REDIS_URL ruby-on-rails ruby-on-rails

Redis tries to connect to localhost on Heroku instead of REDIS_URL


I can think of two possibilities:

  • Is something setting ENV['REDIS_URL'] before the initializer runs? For instance maybe you have a .env file checked into git that is overriding the Heroku variable?

  • You say this code is from redis.rb. Do you have a config/initializers/resque.rb also? What about config/resque.yml? Either of those may be also trying to open a Redis connection. (If you could post the entire stack trace of your error, you could confirm this or rule it out.) Or are you using Redis for anything other than Resque?

You could also do some printf debugging and change your initializer to say:

if Rails.env.production?  puts "production: #{ENV['REDIS_URL']}"  uri = URI.parse(ENV["REDIS_URL"])else  puts "not production"  uri = URI.parse("redis://localhost:6379")end

That should help you clarify what's going on. (You may need to use Rails.logger.info instead of puts.)

EDIT: That stack trace is very helpful! Sure enough, your own initializer isn't in there at all, but there is other code trying to load its own redis connection, here:

remote:        /tmp/build_329306a238b046dda86a54d29db48f4c/vendor/bundle/ruby/2.4.0/gems/resque-web-0.0.9/config/initializers/resque_config.rb:4:in `<top (required)>'

If you look up that gem, you can see it is doing this:

require 'resque'config = ENV.fetch("RAILS_RESQUE_REDIS", "127.0.0.1:6379")Resque.redis = config

So I think the answer is to set RAILS_RESQUE_REDIS in addition to REDIS_URL. You can use the Heroku config:set command to do that.


You forgot to pass the USERNAME to the connector, and your REDIS_URL can have been changed by the service provider...

I recommend use

$redis = if Rails.env.production? ? Redis.new(ENV["REDIS_URL"]) : Redis.new("redis://localhost:6379")

It would be even better use the env variable your service provider set, like ENV['REDISCLOUD_URL'], because if they change the url for any motive, like when you update a plan or they need to change their infra... they could just set their variable and it would be transparent for you, without any interruption of service.

If you maintain yourself the REDIS_URL, and they change their url, your app will lost access to until you figure out the URL should change and set the update the REDIS_URL again

OBS

If the problem is when setting up the worker... the problem can be even that it is not starting with the correct environment, at least if you run it on Rails 5, you should config your resque Rakefile to

#If you're using Rails 5.x, include the following in lib/tasks/resque.rb:require 'resque/tasks'task 'resque:setup' => :environment


Inside of your config/initializes/set_redis_url.rb

ENV['REDIS_URL'] = ENV["REDISCLOUD_URL"] if ENV["REDISCLOUD_URL"]

In my past i have used action cable to work with redis and my app wouldn't work without these lines in config/environments/production.rb

config.action_cable.allowed_request_origins = ['https://your-app.herokuapp.com',                                           'http://your-app.herokuapp.com']config.action_cable.url = "wss://sitepoint-custom-messaging.herokuapp.com/cable"