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.
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 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 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)
I am trying to start Faye when starting my rails server. I have a faye.ru file in my app root that looks like:
require 'faye'
faye_server = Faye::RackAdapter.new(:mount => '/queue-listener', :timeout => 45)
run faye_server
And whenever I start my rails server, Faye / thin tries to open on the same port as my rails server. I can add something like:
Thread.new do
system("rackup faye.ru -s thin -E production")
end
into an initializer (found this on SO), but then thin starts on both the rails app port and then the default (9292) port. I think it fails to start on the rails port though. I am just confused about how to start up the thin / faye server on a separate port than the rails server. Any ideas?
You can do the following in development. In production I suggesting implementing it as a standalone server w/monitoring:
if Rails.env.development?
require 'eventmachine'
require 'rack'
require 'thin'
require 'faye'
Faye.logger = Logger.new(Rails.root.join('log/faye.log'))
Faye::WebSocket.load_adapter('thin')
thread = Thread.new do
EM.run {
thin = Rack::Handler.get('thin')
app = Faye::RackAdapter.new(mount: '/faye', timeout: 10)
thin.run(app, :Port => 8000) do |server|
## Set SSL if needed:
# server.ssl_options = {
# :private_key_file => 'path/to/ssl.key',
# :cert_chain_file => 'path/to/ssl.crt'
# }
# server.ssl = true
end
}
end
at_exit { thread.exit }
end
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.