I've added the Redistogo nano add-on on Heroku and I've tested it out in the console successfully. However when my app tries to connect with Redis I get the following error:
Heroku Log file:
2011-10-12T08:19:50+00:00 app[web.1]: Errno::ECONNREFUSED (Connection refused - Unable to connect to Redis on 127.0.0.1:6379):
2011-10-12T08:19:50+00:00 app[web.1]: app/controllers/sessions_controller.rb:14:in `create'
Why is it trying to access Redis on localhost?
My Redis.rb in the config/initializers folder has this, which is almost certainly the problem.
#What's pasted below is pasted ad verbatim. I don't know what to change the values to.
uri = URI.parse(ENV["REDISTOGO_URL"])
REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
Are you using Resque? If so, you'll need to tell Resque which Redis to use.
Resque.redis = REDIS
If not, then the code you've posted about is NOT setting your REDIS connection up.
Try this:
heroku config --long | grep REDIS
to see what your REDISTOGO_URL is. You might have set it accidentally.
Related
in order to use RedisToGo on development, I need to set the environment variable as follows:
ENV["REDISTOGO_URL"] = 'redis://username:password#my.host:6389'
How can I create a redis connection with the username, password and my.host setting, assuming I already have redis-server installed? I think there's a commandline command I have used to achieve this before but I can't seem to remember it now and I can't find any information on this online. Suggestions?
My setup is similar and uses the following:
# config/initializers/redis.rb
uri = URI.parse(ENV["REDISTOGO_URL"])
REDIS = Redis.new(host: uri.host, port: uri.port, password: uri.password)
development & test environments:
ENV['REDISTOGO_URL'] = 'redis:://#localhost:6379'
And then you don't need to set anything for production as long as you have the gem and RedisToGo installed on your Heroku instance.
I am using Redis with my Rails app. I have sidekiq gem installed too. My redis server runs in the same machine in default port.
I created a initializer which initalizes a redis lient.
config/initializers/redis.rb
$redis = Redis.new(:host => 'localhost', :port => 6379)
I have another initalizer that sets the number of accounts currently active in the system.
config/initializers/z_account_list.rb
$redis.set('accounts',Account.count);
In one of my views i am using this piece of code.
<div class="more-text">and <%= "#{$redis.get('accounts')}" %> more...</div>
When i set the value for accounts manually in redis without using the initializer, everything works fine. But when i add the initializer, i get
ActionView::Template::Error (Tried to use a connection from a child process without reconnecting. You need to reconnect to Redis after forking.):
I searched for the error. But most solutions are for resque and has something to do with after_fork. Since i am new to Rails and Redis and since i am not using Resque i am getting a little confused. Please help me out on this.
In forked environments like pushion passenger, we have to reconnect to redis whenever a worker is forked. My biggest confusion was where to put the reconnection statements. In many blogs it was suggested to put it in config/environments.rb. But it didn't work for me.
I added
if defined?(PhusionPassenger)
PhusionPassenger.on_event(:starting_worker_process) do |forked|
if forked
$redis.client.disconnect
$redis = Redis.new(:host => 'localhost', :port => 6379)
Rails.logger.info "Reconnecting to redis"
else
# We're in conservative spawning mode. We don't need to do anything.
end
end
end
to config/initializers/redis.rb
and everything started working fine.
I have to use websockets in my rake task and for that I changed my event.rb to
config.synchronize = true
# Uncomment and edit to point to a different redis instance.
# Will not be used unless standalone or synchronization mode
# is enabled.
config.redis_options = {:host => 'localhost', :port => '3000'}
and when I start my rails server I get this error:
! Invalid request
Exiting
/usr/local/rvm/gems/ruby-1.9.3-p194#socialmail/gems/redis-3.0.4/lib/redis/connection/synchrony.rb:115:in `read': Got 'Protocol error, got "H" as reply type byte' as initial reply byte. If you're in a forking environment, such as Unicorn, you need to connect to Redis after forking. (Redis::ProtocolError)
What am I doing wrong?
Thanks
Hey thanks for the question, I personally couldn't get a rake task of mine to post to a websocket channel I had open on my rails server. Your synchronize command helped (along with starting a Redis server locally).
Your problem through - seems like you're pointing to 3000. Is that your rails server or the Redis instance? If you're running it locally, I'd omit that line.
This is my first experience with redis, I am implementing autocomplete on the search form with soulmate and redis.
I have installed redis on my local machine and I have to do redis-server to make sure redis is working.
To make it work on heroku I have used redis_to_go and followed the instruction given on the link.
However it seems redis server is not getting started as I keep getting the error Error connecting to Redis on 127.0.0.1:6379 (ECONNREFUSED).
I have created a redis.rb file in initializer which has the following code :-
ENV["REDISTOGO_URL"] ||= "redis://redistogo:972612d8048aad8#tarpon.redistogo.com:9436/"
uri = URI.parse(ENV["REDISTOGO_URL"])
REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
I am expecting this piece of code to start redis server for me.
What else do I need to do to make redis work on heroku ?
I solved this after going back and forth with Redis Cloud support.
I needed to make a file - config/initializers/soulmate.rb:
Soulmate.redis = ENV["REDISCLOUD_URL"]
Hope that helps someone else!
After banging my head for 2 days I think implementing autocomplete with rails is a poor option in production.
I was able to implement it on my development machine but redis + heroku was a nightmare for me and very poor support by the redis_to_go team.
I have implemented autocomplete search using typeahead.js instead.
Although you've moved on, to answer the original question: I found a discussion about this in the Soulmate issues list: https://github.com/seatgeek/soulmate/pull/20 (extra info: the fix was merged into the gem well before this question was posed).
So, to solve the problem: add 'ENV["REDIS_URL"] = ENV["REDISTOGO_URL"]' to redis.rb.
My own redis.rb now looks like this:
uri = URI.parse(ENV["REDISTOGO_URL"])
REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
ENV["REDIS_URL"] = ENV["REDISTOGO_URL"]
I'm using Redis with split gem in a RoR application hosted on Heroku.
I've configured it with RedisToGo using the following codes:
/config/initializers/redis.rb
uri = URI.parse(ENV["REDISTOGO_URL"] || "redis://localhost:6379/" )
REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
/config/application.rb
config.gem 'redis'
When I try to
REDIS.set("foo","bar")
on Heroku console, it works fine. It shows Redis ToGo address.
However, when I try to load the application I get the following error:
Errno::ECONNREFUSED: Connection refused - Unable to connect to Redis on localhost:6379
Howcome REDIS is responding correctly, with correct address in Heroku console, but it shows localhost address when the application calls it?
I was able to fix it, and I'll let the solution registered:
I wasn't initializing Split.redis, and therefore it was trying to create a default Redis, which has localhost as host.
So I created the following initializer
/config/initializers/split.rb
Split.redis = REDIS
and then Split could find it!