Redis works on console, but fails in application - ruby-on-rails

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!

Related

How to setup redis connection

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.

Redis with rails. You need to reconnect to Redis after forking error

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.

Redis + Soulmate on heroku - Implementing autocomplete on multiple models

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"]

Errno::ECONNREFUSED: Connection refused - connect(2) for action mailer

I have been working with rails since a long. Now I am facing a small issue in the ActionMailer. I want to send an email when user gets registered to confirm his registration.
I am able to send email in the development mode but where as not in the production mode.
the exception Errno::ECONNREFUSED: Connection refused - connect(2) is coming everytime when deliver method is called.
I have written the following code.
My SMTP config looks:
config.action_mailer.default_url_options = { :host => "localhost:3000" }
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.raise_delivery_errors = true
config.action_mailer.smtp_settings = {
:openssl_verify_mode => OpenSSL::SSL::VERIFY_NONE,
:ssl => true,
:enable_starttls_auto => true, #this is the important stuff!
:address => 'smtp.xxxx.xxx',
:port => xxx,
:domain => 'xxxxxx',
:authentication => :plain,
:user_name => 'xxxxxxx#xxx.xxx',
:password => 'xxxxxxxxx'
}
In the controller, I have written the following:
def confirm_registration_in_c
#user = User.find_by_email(asdf123#gmail.com)
if #user
UserMailer.confirm_registration(#user).deliver
end
end
In my user_mailer.rb :
class UserMailer < ActionMailer::Base
default from: "from#example.com"
def confirm_registration(user)
#user = user
#user_name = #user.name
email = #user.email
mail(:to => email, :subject => "Reset your password")
end
end
I am able to send email in the development mode in my local host, but I am not able to send the email in the dedicated server.
Can anybody help me please?
In my situation, I encountered similar problems when I was trying to making through a sending-email Rails app tutorial, the Heroku logs kept telling my that
......
Errno::ECONNREFUSED (Connection refused - connect(2) for "localhost" port 25):
......
After I compared my code with author's code, I got to find out that I had not setup my ActionMailer configurations in the config/environments/production.rb file.
Then I came to realized that I just had my config/environments/development.rb configured for sending-email, but I had not done it for my config/environments/production.rb.
So you may check it when your app's behavior difers between development and production.
Be sure you have configured your port correctly. I switched from gmail in development (port 587) to sending from my local server in production and was getting this error till I corrected the port to the one my server uses (port 25).
My problem is not identical to this question, but I feel many would found this thread via google.
If you use external SMTP service like sendgrid and has set up ActionMailer accordingly, yet it still gives this error:
Errno::ECONNREFUSED: Connection refused - connect(2) for "localhost" port 25
You may be passing config hash with String key, which are ignored. Keys must be symbols!
This may happen if it is de-serialized, what I did is to ensure keys are symbols:
config.action_mailer.smtp_settings = get_smtp_setting.symbolize_keys
for production you cant write
config.action_mailer.default_url_options = { :host => "localhost:3000" }
add production url for host, like,
config.action_mailer.default_url_options = { :host => "http://www.yourdomain.com" }
There is another reason for this error:
Errno::ECONNREFUSED: Connection refused - connect(2) for "localhost" port 25
It should be looked at SENDMAIL service on your server:
Is SENDMAIL installed?
Is SENDMAIL running?
I had this error due to the stopped SENDMAIL.
Good luck!
I had a similar issue with SendGrid and repeated Errno::ECONNREFUSED: Connection refused - connect(2) ) Finally I modified the way smtp settings were set by changing the definition within /config/environments/production.rb
from:
ActionMailer::Base.smtp_settings = {
to:
config.action_mailer.smtp_settings = {
this did the trick.
I just tracked down a similar problem while trying to deploy wordpress with Capistrano.
cap aborted!
Errno::ECONNREFUSED: Connection refused - connect(2) for "{my-ip-address}" port {my-ssh-port}
I would also get this similar error:
Tasks: TOP => git:create_release
(See full trace by running task with --trace)
The deploy has failed with an error: #<Errno::ECONNREFUSED: Connection refused - connect(2) for "my-ip-address" port {my-port}>
It turns out it was an issue with concurrent SSH sessions as my server runs Fail2Ban. To solve that I simply did the following:
Edit the jail that contains SSH configurations
$ sudo nano /etc/fail2ban/jail.local
look for [SSH] and set enabled = false
then find [ssh-ddos] and set enabled = false
Remember to restart Fail2Ban after your changes and open-ssh (if thats what your using)
$ sudo service fail2ban reload
$ sudo /etc/init.d/ssh reload
Its worth noting that the connection would be refused at different steps (tasks) in the deployment. For example after a reboot and a quick bundle exec cap production deploy:check everything appeared fine. Then I tried to deploy and received the same error, but during the execution of a different task. I also use UFW which I disabled and reenabled without issues. UFW was not the cause of the above problem.
I had a similar problem after I solved this. It was an issue with the current directory permissions. That's over here.
You may see a similar error message when using turbo_stream in rails 7 (or while doing the rails 7 demo).
If you do, run this and everything should work:
redis-server
More info here.

How to get Redis to start on Heroku?

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.

Resources