Redis / Resque [连接到本地主机上的Redis时出错:6379(ECONNREFUSED)]

时间:2013-12-17 22:02:16

标签: ruby-on-rails heroku resque redistogo

我正在尝试使用Resque将我的rails应用程序连接到redis-to-go服务器,但我得到了错误:

Error connecting to Redis on localhost:6379 (ECONNREFUSED)

我尝试了很多可能的解决方案,但其中任何一个都有效。我按照所有说明HERE但没有。

我有2个初始值设定项:

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

rake文件:

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"

我添加了一些很多部分的代码,所以它现在有点难看,但在我尝试使用redis-to-go之前我在本地使用其他配置并且它可以工作但现在我想在heroku中部署问题开始了:c。

PS

如果我尝试手动插入/获取redis,它可以工作! (rails console)

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

1 个答案:

答案 0 :(得分:0)

尝试使用Redis.new(url:REDIS_URL)语法而不是URI.parse

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