简体   繁体   中英

Redis/Resque [Error connecting to Redis on localhost:6379 (ECONNREFUSED)]

I'm trying to connect my rails application to a redis-to-go server with Resque but i'm gettin the error:

Error connecting to Redis on localhost:6379 (ECONNREFUSED)

I tried a lot of possible solutions but any of them worked. I follow all the instructions HERE but nothing.

I have 2 initializers:

redis.rb

ENV["REDISTOGO_URL"] ||= "redis://redistogo:7d3dd2a11d0018445472de9d676360c3@albacore.redistogo.com:9408/"
uri = URI.parse(ENV["REDISTOGO_URL"])
REDIS = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)

resque.rb

Resque.redis = REDIS
Dir["#{Rails.root}/app/workers/*.rb"].each { |file| require file }


Resque.before_fork do
  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.connection.disconnect!
end

Resque.after_fork do
  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.establish_connection
end

And the rake file:

require 'rake/dsl_definition'
require 'resque/tasks'


task "resque:setup" => :environment do
  ENV['QUEUE'] ||= '*'
    Resque.before_fork do
        defined?(ActiveRecord::Base) and
         ActiveRecord::Base.connection.disconnect!
    end

    Resque.after_fork do
      defined?(ActiveRecord::Base) and
        ActiveRecord::Base.establish_connection
    end
end

namespace :resque do
  desc "let resque workers always load the rails environment"
  task :setup => :environment do
    ENV['QUEUE'] ||= '*'
    Resque.before_fork = Proc.new { ActiveRecord::Base.establish_connection }
  end

  desc "kill all workers (using -QUIT), god will take care of them"
  task :stop_workers => :environment do
    pids = Array.new

    Resque.workers.each do |worker|
      pids << worker.to_s.split(/:/).second
    end

    if pids.size > 0
      system("kill -QUIT #{pids.join(' ')}")
    end

    # god should handle the restart
  end
end

desc "Alias for resque:work (To run workers on Heroku)"
task "jobs:work" => "resque:work"

I added some code of a lot of parts so it's a little ugly right now, but before i tried to use redis-to-go i was using this locally with other configs and it works but now i want to deploy in heroku and the problems starts :c.

PS

If i try to do a manually insert/get to redis it works! (rails console)

irb(main):001:0> REDIS.set("foo", "bar")
=> "OK"
irb(main):002:0> REDIS.get("foo")
=> "bar"

try using the Redis.new(url: REDIS_URL) syntax rather than URI.parse

if Rails.env == 'production'
  $redis = Redis.new(url: ENV["REDISCLOUD_URL"])
else
  $redis = Redis.new
end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM