Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up Redis To Go with Heroku and Rails 4.0.0

I am attempting to setup Redis To Go on my Rails 4 app. I want to be able to deploy it to Heroku as well.

So far, this is what I've done:

Through the dashboard.heroku site, I used the one click install for the Nano version of Redis To Go to install the addon to my app.

I added gem 'redis' to my gemfile.

In config/environments/development.rb I added this line:

ENV["REDISTOGO_URL"] = 'redis://redistogo:[email protected]:10280/'

Then, I created a config/initializers/redis.rb file which looks like this:

uri = URI.parse(ENV["redis://redistogo:[email protected]:10280/"] || "redis://localhost:6379/")
REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)

When running a Redis command in my console now, I get this error:

Redis::CannotConnectError: Error connecting to Redis on 127.0.0.1:6379 (ECONNREFUSED)

What am I doing wrong here, and what do I need to do to ensure that I can test in development and deploy to Heroku without any issues?

like image 677
Luigi Avatar asked Mar 17 '14 14:03

Luigi


3 Answers

ENV["REDISTOGO_URL"] should be in the environment on Heroku. I'd remove it from config/environments/development.rb altogether and change the redis.rb initializer to:

uri = URI.parse(ENV.fetch("REDISTOGO_URL", "redis://localhost:6379/"))
REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)

As long as that ENV var isn't set in development, it'll fall back to the local redis installation.

like image 136
brandonhilkert Avatar answered Oct 16 '22 08:10

brandonhilkert


To update brandonhilkert's answer for Rails 4:

uri = ENV["REDISTOGO_URL"] || "redis://localhost:6379/"
REDIS = Redis.new(:url => uri)

Also, you might want to use Redis.current instead of setting a REDIS variable (see here).

like image 32
Spone Avatar answered Oct 16 '22 10:10

Spone


For Rails 4 I did the following

In the console:

heroku addons:create redistogo
heroku config:set REDIS_PROVIDER=REDISTOGO_URL

In my Procfile I added:

worker: bundle exec sidekiq

In my 'gemfile.rb' I added:

gem 'redis'

I added the following file, config/initializers/redis.rb:

uri = ENV["REDISTOGO_URL"] || "redis://localhost:6379/"
REDIS = Redis.new(:url => uri)
like image 1
Steve Avatar answered Oct 16 '22 10:10

Steve