I am attempting to setup Redis To Go on my Rails 4 app. I want to be able to deploy it to Heroku as well.
So far, this is what I've done:
Through the dashboard.heroku site, I used the one click install for the Nano version of Redis To Go to install the addon to my app.
I added gem 'redis' to my gemfile.
In config/environments/development.rb I added this line:
ENV["REDISTOGO_URL"] = 'redis://redistogo:b9fc604b1c86a1f6c232ce1dd16cd989#albacore.redistogo.com:10280/'
Then, I created a config/initializers/redis.rb file which looks like this:
uri = URI.parse(ENV["redis://redistogo:b9fc604b1c86a1f6d872ce1dd16cd989#albacore.redistogo.com:10280/"] || "redis://localhost:6379/")
REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
When running a Redis command in my console now, I get this error:
Redis::CannotConnectError: Error connecting to Redis on 127.0.0.1:6379 (ECONNREFUSED)
What am I doing wrong here, and what do I need to do to ensure that I can test in development and deploy to Heroku without any issues?
ENV["REDISTOGO_URL"] should be in the environment on Heroku. I'd remove it from config/environments/development.rb altogether and change the redis.rb initializer to:
uri = URI.parse(ENV.fetch("REDISTOGO_URL", "redis://localhost:6379/"))
REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
As long as that ENV var isn't set in development, it'll fall back to the local redis installation.
To update brandonhilkert's answer for Rails 4:
uri = ENV["REDISTOGO_URL"] || "redis://localhost:6379/"
REDIS = Redis.new(:url => uri)
Also, you might want to use Redis.current instead of setting a REDIS variable (see here).
For Rails 4 I did the following
In the console:
heroku addons:create redistogo
heroku config:set REDIS_PROVIDER=REDISTOGO_URL
In my Procfile I added:
worker: bundle exec sidekiq
In my 'gemfile.rb' I added:
gem 'redis'
I added the following file, config/initializers/redis.rb:
uri = ENV["REDISTOGO_URL"] || "redis://localhost:6379/"
REDIS = Redis.new(:url => uri)
Related
Hi I'm finding this hard to figure out as.
I'm deploying using Capistrano, and I have quite a few recurring scheduled tasks that I'm using resque-scheduler to run.
On my local environment I would run:
QUEUE=* rake environment resque:work
to start resque and to start resque-scheduler:
rake environment resque:scheduler
this is my resque.rake file:
require 'resque/tasks'
require 'resque/scheduler/tasks'
namespace :resque do
task :setup do
require 'resque'
Resque.redis = ENV['REDIS_SERVER']
end
task :setup_schedule => :setup do
require 'resque-scheduler'
Resque.schedule = YAML.load_file("#{Rails.root}/config/resque_schedule.yml")
end
task :scheduler_setup => :setup_schedule
end
So what I'm trying to figure out is how I get this working in production? I assume I first need to install redis on my server even though I have a remote Redis DB? Then I need to make a capistrano task, or something that make it run?
To connect to remote redis replace Resque.redis value with
uri = URI.parse('redis://redistogo_url:9918/')
Resque.redis = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
You don't need to install `redis' server just the normal gem
I have rails app 3.2.17, deployed in staging environment with unicorn 4.6.3 monitored by bluepill 0.0.66 using mongid 3.1.5
When I deploy for staging environment all works find including active_record except mongoid queries with result with following error:
Error during failsafe response:
Problem:
No configuration could be found for a session named 'default'.
Summary:
When attempting to create the new session, Mongoid could not find a session configuration for the name: 'default'. This is necessary in order to know the host, port, and options needed to connect.
Resolution:
Double check your mongoid.yml to make sure under the sessions key that a configuration exists for 'default'. If you have set the configuration programatically, ensure that 'default' exists in the configuration hash.
mongoid.yml file in deployed station
staging:
sessions:
default:
database: mydb
username: user
password: password
hosts:
- localhost:27017
options:
options:
myapp.pill file in deployed station
Bluepill.application('myapp', log_file: '/var/log/bluepill/myapp.log') do |app|
app.process('myapp-app') do |process|
process.pid_file = '/home/user/myapp/current/tmp/pids/unicorn.pid'
process.working_dir = '/home/user/myapp/current'
process.start_command = '/home/user/.gem/ruby/1.9.1/bin/bundle exec unicorn -c config/unicorn.rb -D -E staging'
process.stop_command = 'kill -QUIT {{PID}}'
process.restart_command = 'kill -USR2 {{PID}}'
process.uid = 'user'
process.gid = 'user'
process.start_grace_time 30.seconds
process.stop_grace_time 30.seconds
process.restart_grace_time 60.seconds
process.monitor_children do |child_process|
child_process.stop_command 'kill -QUIT {{PID}}'
child_process.checks(:mem_usage,
:every => 30.seconds,
:below => 1024.megabytes,
:times => [3,4]
)
child_process.checks(:cpu_usage,
:every => 30.seconds,
:below => 90,
:times => [3,4]
)
end
end
end
I suspect that mongoid is not receiving the RAILS_ENV, but I'm not sure, I check indentation in mongoid.yml. Should another reason that I not find out.
in /config/application.rb type
require 'mongoid'
Mongoid.load!(File.expand_path('mongoid.yml', './config'))
It may be do it via initializer.
I am quite new to redis. This rails application has a redis.rb file in config/initializers
uri = URI.parse(ENV["REDIS_URL"])
$redis = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
The redis url is on heroku config.
I can't just replace REDIS_URL with the REDIS_URL from heroku config.
I am getting a URI parse error
bad URI(is not URI?): (URI::InvalidURIError)
my question is where should I place the redis url ? where is it searching the env variable from?
I'm guessing you're getting this when doing rake. The problem is that when rake-ing, your environment variables aren't set, which leads to this error (info at https://devcenter.heroku.com/articles/rails-asset-pipeline). To overcome, use a conditional intializer instead, e.g.:
if ENV["REDISCLOUD_URL"]
uri = URI.parse(ENV["REDISCLOUD_URL"])
$redis = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
end
P.S. alternatively, use this but note that according to Heroku:
Using this labs feature is considered counter to Heroku best practices.
This labs feature can make your builds less deterministic and require
re-deploys after making config changes. Ideally your app should be able to
build without config.
I've been trying to figure what the issue is for couple days now but no luck, I'm running Rails 3.1.3 on Heroku Cedar with Unicorn and using Resque 1.20 for background jobs.
Redis add-on as been added and REDISTOGO_URL set, I have resque.rb initializer as
require 'resque'
Resque.after_fork = Proc.new { ActiveRecord::Base.establish_connection }
uri = URI.parse(ENV["REDISTOGO_URL"])
REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
Resque.redis = REDIS
I also tried
Resque.redis = Redis.connect(:url => ENV['REDISTOGO_URL'])
Also this, from the official RedisToGo site
ENV["REDISTOGO_URL"] ||= "redis://username:password#host:1234/"
uri = URI.parse(ENV["REDISTOGO_URL"])
Resque.redis = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password, :thread_safe => true)
Heroku worker as:
bundle exec rake resque:work QUEUE='*'
Everything works fine locally and in Heroku console; but when I try to queue from Rails
Resque.enqueue(EmailQueue, id)
I get
NoMethodError (undefined method `sadd' for nil:NilClass):
Please any help will be appreciated, thank you.
Figured it out, since I was on Unicorn; I had to set config/unicorn.rb as below to connect or Redis.
after_fork do |server, worker|
# Replace with MongoDB or whatever
if defined?(ActiveRecord::Base)
ActiveRecord::Base.establish_connection
Rails.logger.info('Connected to ActiveRecord')
end
# If you are using Redis but not Resque, change this
if defined?(Resque)
Resque.redis = ENV['REDISTOGO_URL']
Rails.logger.info('Connected to Redis')
end
end
Hopes this helps someone else.
ive been trying to get resque to work with heroku. i can successfully get it to work in development mode, however when i try pushing to heroku i get
Errno::ECONNREFUSED (Connection refused - Unable to connect to Redis on 127.0.0.1:6379):
i then read and followed http://blog.redistogo.com/2010/07/26/resque-with-redis-to-go/
i put the configurations listed in the site but got the following error
SocketError (getaddrinfo: nodename nor servname provided, or not known):
i put in my initializers/resque.rb
Resque.after_fork = Proc.new { ActiveRecord::Base.establish_connection }
ENV["redis://redistogo:11111111111111111#lab.redistogo.com:9254/"] ||= "redis://heroku_username:heroku_password#host:9254/"
uri = URI.parse(ENV["redis://redistogo:1111111111111111#lab.redistogo.com:9254/"])
Resque.redis = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
however it throws the error mentioned above. on my dev mode now, i get the error as well.
i tried using my heroku username (im using the add on from heroku), putting my password to heroku, and changing the port to 9254. however i keep getting the socket error now. what am i doing wrong?
help would be much appreciated. thank you
UPDATE.
#kevin
i tried
uri = URI.parse(ENV["my_url_string"] || "redis://localhost:9254/" )
REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
in a initializer/redis.rb as well but i get the following error
Errno::ECONNREFUSED (Connection refused - Unable to connect to Redis on 127.0.0.1:6379):
are the numbers in the error, ie 127.0.0.1:6379 significant? ive checked my redis gui app and also from heroku config that my port number is 9254
REDISTOGO_URL => redis://redistogo:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx#lab.redistogo.com:9254/
did you have any other configuration settings? thanks for helping!
FINAL UPDATE.
i fixed it. i can't believe it! my complete solution is
uri = URI.parse(ENV["REDISTOGO_URL"])
REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
Resque.redis = REDIS
verbatim. it works without explicitly setting the url because i guess heroku tries to set it up for me already
For my setup I have /config/initializers/redis.rb with these lines:
uri = URI.parse(ENV["REDISTOGO_URL"] || "redis://localhost:6379/" )
REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
My REDISTOGO_URL is defined in my heroku configuration. You should be able to validate that by typing:
heroku config --app my_app
You'll see in the output the value for REDISTOGO_URL
You should copy your REDISTOGO_URL directly from Redis To Go. You find it in by going to the instance in heroku and clicking on Add Ons -> Redis To Go.
Here are a couple pointers:
Verify you have your REDIS_TO_GO URL in your heroku config from the command line like I've demonstrated above.
Verify the REDIS_TO_GO URL is identical to the one assigned to that instance in the Add Ons -> Redis To Go config.
i fixed it. i can't believe it! my complete solution is
uri = URI.parse(ENV["REDISTOGO_URL"])
REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
Resque.redis = REDIS
verbatim. it works without explicitly setting the url because i guess heroku tries to set it up for me already
I got the same thing, so, basically Sidekiq was not grabbing the REDISCLOUD_URL from vars, it was grabbing REDIS_PROVIDER.
heroku config:set REDIS_PROVIDER=REDISCLOUD_URL
It worked like a charm.