Connecting to remote redis on heroku - ruby-on-rails

I'm using rails on heroku. I have a VPS with redis on it. I don't want to use Heroku redis as it is quiet costly than what i have right now.So I tried the following method
1. Added gem 'redis' to Gemfile.
2. Added the following code to /config/initializers/redis.rb
$redis = Redis.new(:host => "MYSERVER", :port => 6739, :password => "MYPASS")
and pushed it to github.
Still it gives me error
Error connecting to Redis on localhost:6379 (Errno::ECONNREFUSED)
Please help.

Related

Can't connect to redis on heroku

I'm using sidekiq with active_job in my rails application. The job currently works but I am unable to get connect to the Redis when deploying to heroku. In heroku I have the RedisToGo addon. When pushing to heroku I get the following error.
error
/app/vendor/bundle/ruby/2.2.0/gems/redis-3.2.1/lib/redis/client.rb:331:in `rescue in establish_connection': Error connecting to Redis on 127.0.0.1:6379 (Errno::ECONNREFUSED) (Redis::CannotConnectError)
config/application.rb
config.after_initialize do
PasswordAgingJob.perform_later
SetOffCallJob.perform_later
end
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/initializers/active_job.rb
ActiveJob::Base.queue_adapter = :sidekiq
Set REDIS_PROVIDER to the name of the env var with your Redis URL.
Type this: heroku config:set REDIS_PROVIDER=REDISTOGO_URL. Restart.
Explained here: https://github.com/mperham/sidekiq/wiki/Using-Redis#using-an-env-variable
Setting REDIS_URL in heroku config help me to resolve:
Error connecting to Redis on 127.0.0.1:6379 (Errno::ECONNREFUSED) (Redis::CannotConnectError)
You must set the REDISTOGO_URL on Heroku.
Click on Settings, and then Reveal Config Vars.
Setting REDIS_URL to redis://#pike.redistogo.com:10731/ seem to work for me.

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

I want to use Redis on Heroku but I got this error Redis::CannotConnectError (Error connecting to Redis on 127.0.0.1:6379 (ECONNREFUSED)) .
I checked these , but didn't get helped.
Redis connection to 127.0.0.1:6379 failed - connect ECONNREFUSED, deploying redis to heroku unable to connect, How to get Redis to start on Heroku? .
I use Ruby 2.0.0p247 and Rails4. I'm using puma.
I use RedisToGo(nano) and, in /config/initializers/redis.rb I write this.
uri = URI.parse(ENV["REDISTOGO_URL"] || "redis://localhost:6379/" )
$redis = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
I checked "REDISTOGO_URL" is the same URL that I can see when $ heroku config .
I did Redis restart on Heroku GUI, but it doesn't work.
Please tell me anything I need to do.
Thank you for your help.
Check which ENV variable should contain redis configuration
For example if you are going to use Redis for Sidekiq you need to map ENV["REDISTOGO_URL"] to ENV["REDIS_PROVIDER"]
because Sidekiq is expecting to get redis configuration from ENV["REDIS_PROVIDER"]
You can do it this way
heroku config:set REDIS_PROVIDER=REDISTOGO_URL
Also you don't need to URI.parse, Redis will do it instead of you in case it is needed.
$redis = Redis.new(url: ENV['REDISTOGO_URL' || "redis://localhost:6379/"])
I suggest to use the gem figaro to set ENV variables for your local development. Thus you remove OR statement
$redis = Redis.new(url: ENV['REDISTOGO_URL'])
and set alternative value in your config/application.yml
REDISTOGO_URL: redis://127.0.0.1:6379
You're trying to connect to your own computer (note 127.0.0.1 == localhost). I'm guessing that's not the Redis server you're looking for :)
Looks like ENV["REDISTOGO_URL"] isn't properly set.
Also, just a side note, look up Redis To Go if you haven't already, which is what most people use as a Redis server in conjunction with Heroku.

ArgumentError: odd number of arguments for Hash when trying to connect to redis

I'm trying to get rails connect to redis by following this tutorial.
But I'm getting the following error when I try $redis = Redis.new(:host => 'localhost', :port => 6379) or even just Redis.new. I've tried the new notation as well (host: 'localhost',port: 6379). Redis works (ping-PONG test via redis-cli passes).
ArgumentError: odd number of arguments for Hash
from /var/lib/gems/1.9.1/gems/redis-2.1.1/lib/redis.rb:65:in `[]'
from /var/lib/gems/1.9.1/gems/redis-2.1.1/lib/redis.rb:65:in `info'
from /var/lib/gems/1.9.1/gems/redis-2.1.1/lib/redis.rb:606:in `inspect'
from /var/lib/gems/1.9.1/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start'
from /var/lib/gems/1.9.1/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start'
from /var/lib/gems/1.9.1/gems/railties-4.0.0/lib/rails/commands.rb:64:in `<top (required)>'
from bin/rails:4:in `require'
from bin/rails:4:in `<main>'
What am I doing wrong?
Config Details:
$ ruby -v
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]
$ rails -v
Rails 4.0.0
I receive the same error using the redis gem version 2.1.1. Try to update the redis gem to latest, which is version 3.0.4.
To update the redis gem, you can either run
gem update redis
to update the locally installed version, or run
bundle update redis
to update the redis gem that is installed as part of your Gemfile.
$ irb
1.9.3p392 :001 > require 'redis'
=> true
1.9.3p392 :002 > Redis.new(:host => 'localhost', :port => 6379)
ArgumentError: odd number of arguments for Hash
from /Users/zachallett/.rvm/gems/ruby-1.9.3-p392/gems/redis-2.1.1/lib/redis.rb:65:in `[]'
from /Users/zachallett/.rvm/gems/ruby-1.9.3-p392/gems/redis-2.1.1/lib/redis.rb:65:in `info'
from /Users/zachallett/.rvm/gems/ruby-1.9.3-p392/gems/redis-2.1.1/lib/redis.rb:606:in `inspect'
from /Users/zachallett/.rvm/rubies/ruby-1.9.3-p392/bin/irb:16:in `<main>'
.
$ irb
1.9.3p392 :001 > require 'redis'
=> true
1.9.3p392 :002 > Redis.new(:host => 'localhost', :port => 6379)
=> #<Redis client v3.0.4 for redis://localhost:6379/0>
Had the following issue but we couldn't upgrade the redis gem to 3.x. It turned out that redis-rb was looking for a key/value pair for the redis config which didn't exist when running redis-server.
Running redis-server with the config file
redis-server /usr/local/etc/redis.conf
fixed the issue.
I haven't used redis yet, but based on a cursory look, seems like you're using the redis-ruby library. You probably want to experiment with redis-rails when it's available!(http://rubygems.org/gems/redis-rails)
The easiest way out is, look for Gemfile, and remove information about the redis gem. Replace that line with:
gem 'redis-rails'
Save the file, then run:
$ bundle install
I had the very same issue, turns out homebrew had updated my redis-server from 2.6.x to 2.8.6
I reverted back to an older version of redis and it worked!
I've ran into the same error. The first thing I notices is that this error is related to the visual output that gets displayed in the console as a representation of the object. The inspect method is what gets called for this purpose. See the following from the stack trace:
/var/lib/gems/1.9.1/gems/redis-2.1.1/lib/redis.rb:606:in 'inspect'
So if you do redis = Redis.new(:host => 'localhost', :port => 6379)
or anytime you type just redis therafter, you'll get this error. However if you ignore it and just do
redis.get 'foo' everything works just fine.

Redis connection refused through ruby daemon worker on Heroku

A Rails 3.2.6 application running as a 'web' Heroku process is connecting to Redis using the ENV["REDISTOGO_URL"] environment variable:
irb(main):002:0> Redis.current
=> #<Redis client v2.2.2 connected to redis://xxx.redistogo.com:1234/0 (Redis v2.4.11)>
----- /initializers/redis.rb
if Rails.env.development?
Redis.current = Redis.new
elsif Rails.env.test?
Redis.current = Redis.new
elsif Rails.env.production?
uri = URI.parse(ENV["REDISTOGO_URL"])
Redis.current = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
end
A ruby daemon 'streaming' process runs as a secondary worker:
----- Procfile
web: bundle exec rails server thin -p $PORT -e $RACK_ENV
worker: bundle exec rake jobs:work
streaming: RAILS_ENV=production ruby bin/streaming.rb start
However, the streaming process is crashing when it calls methods in the main Rails application that connect with Redis - even though the streaming process supposed to be loading the same redis.rb initializer as the 'web' Rails application process.
----- /bin/streaming_ctl.rb
# encoding: UTF-8
require "rubygems"
require "bundler/setup"
require "daemons"
Daemons.run(File.expand_path("../streaming.rb", __FILE__))
----- /bin/streaming.rb
# encoding: UTF-8
# TODO: set rails env in init script
ENV["RAILS_ENV"] ||= "production"
# load rails environment
require File.expand_path('../../config/environment', __FILE__)
logger = ActiveSupport::BufferedLogger.new(File.expand_path("./../../log/streaming.log", __FILE__))
Streaming.start
logger.info("\nStarting streaming in #{Rails.env.to_s} mode.")
Why is the streaming process/worker using the default Redis host and port?
[streaming.1]: /app/vendor/bundle/ruby/1.9.1/gems/redis-2.2.2/lib/redis/client.rb:236:in `rescue in establish_connection': Connection refused - Unable to connect to Redis on 127.0.0.1:6379 (Errno::ECONNREFUSED)
Switching from "Redis.current" in /initializer/redis.rb and using a "$redis" variable convention instead (as illustrated here http://jimneath.org/2011/03/24/using-redis-with-ruby-on-rails.html) fixed the connection problem.
The app was using redis gem version 2.2.2 at the time. It appears as though the Redis.current in this gem version won't be consistent across 'web' and 'worker' processes because Heroku runs these processes on separate threads. The documentation on the gem repo suggests that updating to gem to >= version 3 will enable Redis.current to run in multi-threaded environments:
https://github.com/redis/redis-rb/blob/master/CHANGELOG.md#300

Why doesn't Delayed Jobs work with my ActionMailer production setup?

I am working on a Ruby (1.8.6) on Rails (2.3.5) application in which I am currently using Gmail to deliver email messages. I want to switch to sending the messages with Delayed Jobs.
I have delayed jobs sending messages on my development environment but when I deploy to my production server and try the messages get rejected and an error shows up in my delayed_jobs table: 530 5.7.0 Must issue a STARTTLS command first. i25sm12946175anh.17. I've seen this error before (when I don't have TLS enabled in my config). But it was working before I started using delayed_jobs. Here is my mail config:
ActionMailer::Base.smtp_settings = {
:enable_starttls_auto => true,
:address => 'smtp.gmail.com',
:port => 587,
:domain => 'example.com',
:authentication => :plain,
:user_name => 'user#example.com',
:password => 'password'
}
Any help with this issue is greatly appreciated.
Update: The application is actually running on REE ruby 1.8.7. So maybe delayed jobs is somehow using the regular ruby interpreter installed on the server (1.8.6). But, how do I get delayed jobs to use my REE install? And how do I tell what things are using what version of Ruby.
But, how do I get delayed jobs to use
my REE install? And how do I tell what
things are using what version of Ruby.
The script/delayed_job command uses the system Ruby. So "which ruby" should point to the REE installation. If not, you can force it by using the Ruby executable. Lets say, your REE is installed at /opt/ruby-enterprise-1.8.7-2010.01. Then:
$ RAILS_ENV=production /opt/ruby-enterprise-1.8.7-2010.01/bin/ruby script/delayed_job -n 2 start
Or if you use the rake command to start, like:
$ rake jobs:work -t RAILS_ENV=production
Then which rake should point to your REE installation. If its not, then you can make it use REE by:
RAILS_ENV=production /opt/ruby-enterprise-1.8.7-2010.01/bin/rake jobs:work -t
Normally you would use :enable_starttls_auto as you wrote, but that only works for ruby >= 1.8.7 and ruby >= 1.9.
So you need to use the solution as stated here: define a file smtp_tls.rb which you place in your initializers folder.

Resources