我正在尝试使用 Resque 将我的 rails 应用程序连接到 redis-to-go 服务器,但我遇到了错误:
Error connecting to Redis on localhost:6379 (ECONNREFUSED)
我尝试了很多可能的解决方案,但其中任何一个都有效。我按照此处的所有说明进行操作,但没有。
我有 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。
附言
如果我尝试手动插入/获取redis,它可以工作!(导轨控制台)
irb(main):001:0> REDIS.set("foo", "bar")
=> "OK"
irb(main):002:0> REDIS.get("foo")
=> "bar"