Search code examples
rubyherokusinatraredisredistogo

Can't connect to Redis from Sinatra


I'm trying to hook up redis to a Sinatra app I'm building:

require 'rubygems'
require 'sinatra'
#require 'sinatra/synchrony'
require 'redis'
require 'mongo_mapper'

require './startup'

def stats_connect
 uri = URI.parse('redis://redistogo:xxxxxxxxxxxxxxxxxx@barb.redistogo.com:1337/')
 puts 'connecting to... ' + uri.to_s
 redis = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
 puts "Statistics connected >> OK" if redis
 return redis
end

stats = stats_connect

post('/') do
 #...
end

If I run the following app with foreman, I get this error:

  • 18:09:02 web.1 | started with pid 825

  • 18:09:08 web.1 | /Users/vladdypwnz/.rvm/gems/ruby-1.9.2-p180/gems/redis-3.0.1/lib/redis/connection/ruby.rb:113:in `connect_nonblock': Can't assign requested address - connect(2) (Errno::EADDRNOTAVAIL)

  • 18:09:08 web.1 | from /Users/vladdypwnz/.rvm/gems/ruby-1.9.2-p180/gems/redis-3.0.1/lib/redis/connection/ruby.rb:113:in `connect'

When I push to heroku, the error changes to this:

/app/vendor/bundle/ruby/1.9.1/gems/redis-3.0.1/lib/redis/client.rb:260:in `rescue in establish_connection': Timed out connecting to Redis on barb.redistogo.com:0 (Redis::CannotConnectError)

If I pop into IRB, require redis and use the same exact stats_connect() method that I created, redis works just fine, I can access everything and create keys.

What's going on? I'm completely stumped.


Solution

  • Are you sure your redis is running on barb.redistogo.com:1337? A couple weeks ago I deployed an app using redis to heroku and used this configuration:

    uri = URI.parse(ENV["REDISTOGO_URL"])
    redis = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
    

    to run it locally I did:

    redis = Redis.new(:host => "localhost", :port => 6379)