Can't connect to redis on heroku - ruby-on-rails

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.

Related

Connecting to remote redis on heroku

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.

Heroku RedisCloud Redis::CannotConnectError on localhost instead of REDISCLOUD_URL

There are quite a few questions like this, but none seem to resolve my issue. Here's the grub:
Rails 4, Ruby 2.0.0 app using Resque and Redis.
My redis initializer:
#config/initializers/redis.rb
if ENV["REDISCLOUD_URL"]
$redis = Redis.new(:url => ENV["REDISCLOUD_URL"])
end
Error is this, as reported by heroku logs:
Redis::CannotConnectError: Error connecting to Redis on 127.0.0.1:6379 (ECONNREFUSED)
My procfile:
web: bundle exec rackup config.ru -p $PORT
resque: env TERM_CHILD=1 QUEUES=* bundle exec rake resque:work
If it helps, my REDISCLOUD_URL is:
redis://rediscloud:somestuff#someotherstuff:17695
It isn't in http://. Is that an issue?
I've tried all the other stack overflow posts on this issues, and nothing works. Any tips? I've been follwing the RedisCloud Heroku page here: https://devcenter.heroku.com/articles/rediscloud
Nevermind, I found the answer. I did $redis = Resque.redis = Redis.new ... in my redis.rb initializer, and then I had to migrate my db. Silly me.

Redis to Go - Heroku - Rails

I'm trying to get Sidekiq playing in Heroku. Without Luck.. My configs look like that:
Procfile
web: bundle exec passenger start -p $PORT --max-pool-size 5
worker: bundle exec sidekiq
Initializers/redis.rb
uri = URI.parse(ENV["REDISTOGO_URL"] || "redis://localhost:6379/")
REDIS = Redis.new(:url => ENV['REDISTOGO_URL'])
but when i do heroku ps only the web instance is shown. Not Sidekiq.
However i can manually run heroku run sidekiq and run my workers. What am i missing so that Heroku doesn't start that on it's own ?
The problem is that you didn't configure Sidekiq in the initializer so that Sidekiq knows exactly how to connect to redis. Create Initializers/sidekiq.rb and add the following code
Sidekiq.configure_server do |config|
config.redis = { :url => ENV["REDISTOGO_URL"] }
end
Also you can remove
uri = URI.parse(ENV["REDISTOGO_URL"] || "redis://localhost:6379/")
You don't need it as you are using the redis url from the environment.
Don't forget to restart your server.

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.

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

Resources